Agent47
Agent47

Reputation: 79

Add and edit in one form php

I have two files .. addclient.php & Editclient.php.

I want to combine in one php form. can u please help me to do this

<?php 
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
echo "add";
  } else if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "edit"
  }  
?>

if $id already exist in the table than the row is updated otherwise it's an INSERT. If you only have an $id : show the form with existing data in it. If it's not a $id isn't populated : show an empty form.

Upvotes: 0

Views: 2544

Answers (5)

Barry
Barry

Reputation: 1

try this

if(isset($id) && !empty($id)){
    echo "Update";
}else{
    echo "Add";
}

Upvotes: -1

M. T.
M. T.

Reputation: 539

<?php
  if(isset($_GET['ID'])) {
    $ID= $_GET['ID'];
    // Use MySQL with $ID to check if the user is existing, and this string will either be 0 or 1
    $existing = 0; // 0 = New 1 = Existing
    //Add
    if($existing== 0) {

    } 
    //Edit
    if($existing == 1) {

    }    
  }
?>

Upvotes: 1

Krish R
Krish R

Reputation: 22721

Can you try this,

<?php 
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "Edit";
  } else {              
    echo "Add"
  }  
?>

Upvotes: 0

Nouphal.M
Nouphal.M

Reputation: 6344

Try this

<?php 
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "update";
  }else{
    $id = $_GET['id'];
    echo "add"
  }  
?>

Upvotes: 0

Ali
Ali

Reputation: 3461

<?php
    if(isset($_GET['id'])) {
         $id = intval($_GET['id']);
         $stmt = $mysqli->prepare("SELECT id FROM table WHERE id = ?");
         $stmt->bind_param('i', $id);
         $stmt->execute();
         if($stmt->num_rows > 0) {
               // UPDATE
               // Text field with the id
               echo '<input type="text" name="id" value="'. $id. '"/>';
         } else {
               // INSERT
               // Text field with no id
               echo '<input type="text" name="id"/>';
         }
    }
?>

This validates the integer and makes sure it's one, queries the table for that specific id, if there is more than one row with that id then you need to update, else you need to insert.

Upvotes: 1

Related Questions