0

I want to update multiple rows

this my html code :

<form action="" method="POST">
<input type="text" name="name" placeholder=""/>
<input type="text" name="phone" placeholder=""/>
<input type="text" name="blood" placeholder=""/>
<input type="text" name="id" placeholder=""/>
<input type="text" name="email" placeholder=""/>
<input type="submit" name="sub" value=""/>
</form>

and this my php code :

  if(isset($_POST['sub'])) {
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $blood = $_POST['blood'];
    $id = $_POST['id'];
    $email = $_POST['email'];
    if($name or $phone or $blood or $id or $email) {
      $res_11 = mysql_query("UPDATE `general_info` SET `name`='$name',`phone`='$phone',`blood`='$blood',`id_2`='$id',`email`='$email'  WHERE `id`='1'") or die(mysql_error());
    }
  }else{
    echo "ERROR";
  }

i have table like this

name | phone | blood

and i want if some body update just one row update

if write just new phone update just phone in database

3
  • 1
    your PHP contains syntax errors. Plus, stop using deprecated mysql_* functions Commented Jun 15, 2015 at 10:06
  • php.net/manual/en/… Commented Jun 15, 2015 at 10:06
  • @raptor thank you i use mysqli_* but i just but this code to ask how to update multiple rows Commented Jun 15, 2015 at 10:10

2 Answers 2

1

Try this Code:

 if(isset($_POST['sub'])) {
     $subQuery = "";
     if(isset($_POST['name']) && $_POST['name'] != "") {
         $subQuery .= " name = '". $_POST['name'] ."',";
                $name = $_POST['name'];
     }
     if(isset($_POST['phone']) && $_POST['phone'] != "") {
         $subQuery .= " phone = '". $_POST['phone'] ."',";
                 $phone = $_POST['phone'];
     }
     if(isset($_POST['blood']) && $_POST['blood'] != "") {
         $subQuery .= " blood = ". $_POST['blood'] .",";
                 $blood = $_POST['blood'];
     }

     if(isset($_POST['email']) && $_POST['email'] != "") {
         $subQuery .= " email= '". $_POST['email'] ."',";
                 $email = $_POST['email'];
     }
     if(isset($_POST['id']) && $_POST['id'] != "") {
         $subQuery .= " id_2 = '". $_POST['id'] ."',";
                 $id = $_POST['id'];
     }

    if($name or $phone or $blood or $id or $email) {
      $res_11 = mysql_query("UPDATE `general_info` SET ". substr($subQuery, 0, -1) ."  WHERE `id`='1'") or die(mysql_error());
    }
  }else{
    echo "ERROR";
  }
Sign up to request clarification or add additional context in comments.

1 Comment

@user4965442 Now check it. I have updated the answer.
0

I think this is what you trying to do. But don't use mysql use PDO or mysqli.

if (isset($_POST['sub'])) {
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $blood = $_POST['blood'];
    $id = $_POST['id'];
    $email = $_POST['email'];
    //check if not empty
    if ($name != "" && $phone != "" && $blood != "" && $id != "" && $email != "") {
        $res_11 = mysql_query("UPDATE `general_info` SET `name`='$name',`phone`='$phone',`blood`='$blood',`id_2`='$id',`email`='$email'  WHERE `id`='1'") or die(mysql_error());
    }
} else {
    echo "ERROR";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.