当我单击保存按钮时,只有all_university
表更新,但all_colleges
表尚未更新。如何在一个保存按钮上更新两个表?
<?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>";
}
}
?>
答案 0 :(得分:2)
您拥有多少查询并不重要。只要一个一个地运行它们 此外,您应该使用准备好的陈述。
<?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>";
}
不要使用mysqli_multi_query()。它对你没有好处,也不会像你想象的那样工作。
答案 1 :(得分:0)
如果要使用多查询功能,则必须连接两个查询字符串:
$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']."'";
然后执行mysqli_multi_query
。
或者,正如Rory已经提到的那样,只需使用普通的mysqli_query
函数进行两次查询。
但你应该真正研究预备语句,因为你很容易受到 SQL注入的攻击!
答案 2 :(得分:-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']."'";
执行它。
$value = mysqli_multi_query($link,$sql);
mysqli_multi_query()函数执行一个或多个查询 数据库。查询用分号分隔。