Khushi Patel
Khushi Patel

Reputation: 93

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

Upvotes: 0

Views: 30

Answers (2)

Nigel Ren
Nigel Ren

Reputation: 57121

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.

Upvotes: 1

Nerdi.org
Nerdi.org

Reputation: 923

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.

Upvotes: 0

Related Questions