dsfs64664 2016-07-30 08:11
浏览 25
已采纳

PHP&MySql:无法更新数据库

I'm working on a project where I need to set the boolean value isHere to true(1) when it finds the corresponding ID. However I can't seem to update the database. I can select from the database though.

I have two problems. First:

$handle = @fopen("../smartriders.txt", "r");

    if ($handle) 
        {
        while (($smartRider = fgets($handle, 255)) !== false) 
        { 

            if($update = $conn->query("UPDATE members SET isHere = 1 WHERE SmartRiderID = ?", $smartRider))
            {
                print("update successful");
            }
        }
        if (!feof($handle)) {
            echo "Error: unexpected fgets() fail
";
        }
        fclose($handle);
    }

This yields a warning:

Warning: mysqli::query() expects parameter 2 to be integer, string given in index.ph on line 14.

line 14:

if($update = $conn->query("UPDATE members SET isHere = 1 WHERE SmartRiderID = ?", $smartRider))

the second problem: Even if I manually type the smartRider that is in the database, nothing gets updated.

Any help would really be appreciated.

  • 写回答

2条回答 默认 最新

  • dsigg21445 2016-07-30 08:37
    关注

    If you refer to mysqli_query manual - you will see that second argument of this function is int $resultmode.

    See - it is int. And it's purpose is to define a structure of returned result. So, passing your $smartRider to query() is useless.

    query function doesn't work with prepared statements.

    For prepared statements is prepare():

    $stmt = $mysqli->prepare('UPDATE members SET isHere = 1 WHERE SmartRiderID = ?');
    $stmt->bind_param('s', $smartRider);
    $res = $stmt->execute();
    

    For cheking result of execute() see http://php.net/manual/en/mysqli-stmt.execute.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?