0

When I click on the save button only all_university table updates but the all_colleges table has not been updated. How can I update two tables on one save button?

<?php
  if(isset($_POST['save'])) {
    $chk = implode(",", $_POST['company_name']);  
    $sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
    $sql = "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";
    $value = mysqli_multi_query($link,$sql);
    if($value == true){
        $msg .="<h5 style='color:green'>Successfull</h5>";
    } else {
        $msg .="<h5 style='color:red'>Error!</h5>";
    }
  }
?>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Jeff
  • 53
  • 7

3 Answers3

2

It doesn't matter how many queries you have. Just run them all one by one.
Besides, you should be using prepared statements.

<?php
if(isset($_POST['save'])) {
    $chk = implode(",", $_POST['company_name']);  

    $sql = "update all_university set placement = ? where university_name = ?";
    $stmt = $link->prepare($sql);
    $stmt->bind_param("ss", $chk, $_POST['university_name']);
    $stmt->execute();

    $sql = "update all_colleges set placement = ? where college_name = ?";
    $stmt = $link->prepare($sql);
    $stmt->bind_param("ss", $chk, $_POST['college_name']);
    $stmt->execute();

    $msg .="<h5 style='color:green'>Successfull</h5>";
}

DO NOT use mysqli_multi_query(). It will do you no good and won't work the way you think.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

If you want to use the multi-query function, you have to concatenate the two query strings:

$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."';";
$sql .= "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";

And then execute mysqli_multi_query.

Or, as Rory already mentioned, just query twice using the normal mysqli_query function.

But you should really look into prepared statements as you are vulnerable to SQL injection!

MrDarkLynx
  • 686
  • 1
  • 9
  • 15
-1

Separate both query with semicolon (;)

$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."';";
$sql = "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";

Then, Append Both Query.

$sql = "update all_university set placement = '$chk' where university_name = '".$_POST['university_name']."'";
$sql .= "update all_colleges set placement = '$chk' where college_name = '".$_POST['college_name']."'";

Execute it.

$value = mysqli_multi_query($link,$sql);

The mysqli_multi_query() function performs one or more queries against the database. The queries are separated with a semicolon.

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77