当我们尝试更新时,会显示所有输出(“成功步骤”)1,2,4,5。但是当我们尝试检查数据库时,只有第一个数据库能够进行更新。
$host = "172.16.1.16";
$user = "root";
$password = "root";
$dbname1 = "nocs";
$dbname2 = "nocs_backup";
这是第一个数据库
$db1 = new mysqli($host, $user, $password, $dbname1);
if($db1->connect_errno > 0){
die('Unable to connect to database' . $db1->connect_error);
}
else
{
$updateComponentSql = "UPDATE component
SET `Component_name`='$name', `Brand`='$brand',
`Model`='$model', `Type`='$type',
`Capacity`='$capacity',`Serial_no`='$serial',
`Description`='Installed'
WHERE `Component_id`='$d_id'
AND `computer_id`='$c_id'";
if (mysqli_query($db1, $updateComponentSql))
{
echo "<script type='text/javascript'>
alert('Succesful step 1');
</script>";
}
这是第二个
$db2 = mysqli_connect($host, $user, $password, $dbname2);
echo "<script type='text/javascript'>alert('Succesful step 2');</script>";
if($db2->connect_errno > 0)
{
die('Unable to connect to database' . $db2->connect_error);
echo "<script type='text/javascript'>alert('Successful step 3');</script>";
}
else
{
echo "<script type='text/javascript'>alert('Succesful step 4');</script>";
if (mysqli_query($db2, "UPDATE component
SET (`Component_name`='$name',
`Brand`='$brand', `Model`='$model',
`Type`='$type',`Capacity`='$capacity',
`Serial_no`='$serial',
`Description`='Installed'
WHERE `Component_id`='$d_id'
AND `computer_id`='$c_id)" ))
{
echo "<script type='text/javascript'>
alert('Succesful step 5');
</script>";
}
}
}
我们的智慧结束了。请帮助我们:(
答案 0 :(得分:0)
第二个UPDATE
查询中有无关的括号。
if (mysqli_query($db2, "UPDATE component
SET `Component_name`='$name', `Brand`='$brand', `Model`='$model',
`Type`='$type', `Capacity`='$capacity', `Serial_no`='$serial',
`Description`='Installed'
WHERE `Component_id`='$d_id' AND `computer_id`='$c_id" ))
{
echo "<script type='text/javascript'>alert('Succesful step 5');</script>";
} else {
echo "<script type='text/javascript'>alert(" . json_encode(mysqli_error($db2)) . ");</script>";
}