douxie2029 2018-04-12 14:23
浏览 36
已采纳

MySQLi条件不正常工作

When user try to register, I am checking first table device, if there any device_id available I want provide user trial=0 and if there no any device_id, I want insert device_id in that table and want user trial=1, but currently its always set trial=1 and inserting device_id in table device. My current code is like below

$serial = $POST["serial"];
    $fcm = $POST["fcm"];
    $trial  = 0;
    $trial_sql = "SELECT FROM device WHERE device_id = $serial";
        $trial_result = mysqli_query($conn, $trial_sql);
        if (mysqli_num_rows($trial_result) == 0) {
         $device_sql = "INSERT INTO device(device_id) VALUES('$serial')";
         if($conn->query($device_sql)){
             $trial = 1;
            }   
    }
    $sql = "INSERT INTO user(name, email, password, device_id, trial, fcm) VALUES('$name', '$email', '$password', '$serial', $trial, '$fcm')";
    if($conn->query($sql)) {
        $response["code"] = 1;
    }
    return json_encode($response);

Let me know if someone can correct my mistake. Thanks

  • 写回答

2条回答 默认 最新

  • duanpen9294 2018-04-12 14:49
    关注

    You both need to specify a column to select as well as include quotes round the device_id...

    $trial_sql = "SELECT * FROM device WHERE device_id = '$serial'";
    

    The quotes would have not been a problem if you used prepared statements.

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败