简体   繁体   中英

MySQLi condition not properly working

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

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.

SELECT * FROM device 

Or

SELECT columnA, columnB FROM device

Your first query is not valid because it doesn't say what columns it wants to select from device table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM