0

My PHP code below is for a hospital setting, I want the app to only insert the data if room number is equal to 1, if room number is equal to 2 or any other number I want it to return an error. Could somebody show me how to fix this? Thanks

if($room_number = '1'){
    $mysql_qry = "insert into patients2
    (patient_name, doctor_name, check_in_date, room_number, bed_number, notes, time) 
    values ('$patient_name', '$doctor_name', '$check_in_date', '$room_number', '$bed_number', '$notes', '$time')";
}

else if($room_number != '1'{
        echo "Error: " . $mysql_qry . "<br>" . $conn->error;
}                  

if($conn->query($mysql_qry) === TRUE) {
    echo "Insert successful";
}
3
  • 1
    make it correct from if($room_number = '1') to if($room_number == '1') first your not assigning the value you are comparing the values Commented Apr 4, 2017 at 11:29
  • try with $room_number == '1' , = is assigment operator Commented Apr 4, 2017 at 11:29
  • Thanks for the response guys, I've got it sorted Commented Apr 4, 2017 at 11:41

3 Answers 3

2

You should just execute your query in your first condition, otherwise display an error :

if($room_number == '1') {
    $mysql_qry = "insert into patients2
    (patient_name, doctor_name, check_in_date, room_number, bed_number, notes, time) 
    values ('$patient_name', '$doctor_name', '$check_in_date', '$room_number', '$bed_number', '$notes', '$time')";

    if($conn->query($mysql_qry) === TRUE) {
        echo "Insert successful";
    } else {
        echo "Error: " . $mysql_qry . "<br>" . $conn->error;
    }
}

else {
    echo "Error: Room number value must be 1.";
}   

Also be careful to your line if($room_number = '1'), in this case you are assigning the value into your variable with one = and not comparing it with two ==

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thanks, the code works now, I will accept your answer. Thank you for the help!
Glad to help you ;-)
0
if($room_number == '1'){
    $mysql_qry = "insert into patients2
    (patient_name, doctor_name, check_in_date, room_number, bed_number, notes, time) 
    values ('$patient_name', '$doctor_name', '$check_in_date', '$room_number', '$bed_number', '$notes', '$time')";
}
else
{
 echo "Error: " . $mysql_qry . "<br>" . $conn->error;
}

Comments

0

Firstly, your condition is wrong. You are not checking if $room_number equals 1, but you are assigning string "1" to that variable. You need 2 = symbols to do a compare, or 3 = for strict comparisson. Please read more about this here: http://php.net/manual/en/language.operators.comparison.php

Secondly, you need to adapt your logic. You are checking if room number equals 1, then in else block check if room number is not 1, redundant check. The else will be executed only if room number is not equal 1, so no need to check this again. And then at the end, you execute the query regardless of the $room_number value. Move that bottom if, into the body of the first if. Like so:

<?php
if ($room_number === "1") {
    $mysql_qry = "insert into patients2
        (patient_name, doctor_name, check_in_date, room_number, bed_number, notes, time) 
        values ('$patient_name', '$doctor_name', '$check_in_date', '$room_number', '$bed_number', '$notes', '$time')";

    if($conn->query($mysql_qry) === TRUE) {
        echo "Insert successful";
    } else {
        echo "Error: " . $mysql_qry . "<br>" . $conn->error;
    }
} else {
    echo "Room number does not equal 1";
}

Now, we check if the $room_number is string("1"), and if it is, we create the query and execute it. If execution was successful we print the success message, or the query error otherwise. If the $room_number is not exactly "1" we display an error message about it.

1 Comment

Thanks for the reply and the help, the question was answered above!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.