【问题标题】:The update only happens in the 1st database and not with the 2nd更新仅发生在第一个数据库中,而不发生在第二个数据库中
【发布时间】:2015-07-10 01:33:33
【问题描述】:

当我们尝试更新时,会显示所有输出(“成功步骤”)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>";
            }   
        }

}

我们已经束手无策了。请帮助我们:(

【问题讨论】:

  • 您的第二个UPDATE 有语法错误。去掉括号。
  • 检查mysqli_affected_rows($db2)也许根本就没有匹配的记录。还要在某处打印实际查询(如果必须打印到浏览器,最好打印到日志文件),可能参数与您期望的不同。另见:dev.mysql.com/doc/refman/5.1/en/query-log.html
  • 顺便说一句:all the outputs ("Successful step") 1,2,4,5 are shown - 您确定显示第 5 步警报吗?因为正如所指出的那样 is 您的查询中有一个错误,而 mysqli_query() 永远不会在此返回 true。
  • @VolkerK 是的。尽管第二个数据库中没有更改,但正在显示第 5 步通知。它真的让我感到困惑,以及为什么它会执行回声部分。有什么方法可以知道 if 语句返回了什么?
  • 你可能想安装一个调试器,netbeans.org/kb/docs/php/debugging.html

标签: php mysql


【解决方案1】:

您在第二个 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>";
}

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 2017-06-19
    • 2013-02-27
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多