Hi All Can someone please tell me what am I doing wrong in this query??? Its inserting everthing even those date that are already there. So basicly its this line thats not working. if (($result) == ($_POST['s_time'])) { PHP: why??? Many thanks Zed function insert(){ $b_id = $_POST['b_id']; $dateTime = $_POST['dateTime']; $user_id = $_POST['user_id']; $s_time = $_POST['s_time']; $e_time = $_POST['e_time']; $request_date = $_POST['request_date']; $query = "SELECT s_time,request_date FROM booking WHERE request_date = '$request_date'"; echo "$query"; $result = mysql_query($query)or die(mysql_error()); if (($result) == ($_POST['s_time'])) { error_message("Sorry, this hour/s is already booked please choose another !"); }else{ $query = "INSERT INTO booking VALUES (NULL,'$dateTime','$user_id','$s_time','$e_time','$request_date')"; if (@mysql_query($query)) { comfirmed_booking(); } else { echo '<p>Error adding submitted Information: ' . mysql_error() . '</p>'; }//end of else }//end of if }//end of insert PHP:
Change if (($result) == ($_POST['s_time'])) { PHP: to if (!empty($result) && $result['s_time'] == $_POST['s_time']) { PHP:
Thanks for your reply your code does make sense but it's still same. It inserts the times that are already there. Zed
Oops, actually you need to call mysql_fetch_assoc first.. $row = mysql_fetch_assoc($result); if (!empty($row) && $row['s_time'] == $_POST['s_time']) { PHP: Place it after: $result = mysql_query($query)or die(mysql_error()); And remove this: if (($result) == ($_POST['s_time'])) {
Behaving really starnge, First record works fine meaning you can not insert another similar to it but after that all other records can be inserted and no error msg coming up????
Do echo "<pre>"; print_r($row); echo "</pre>"; PHP: and show us the output. Insert at correct location.
OK well obviously s_time is incorrect, right? You may want to look at your input and see if it's any good.. echo "<pre>";print_r($_POST); PHP: Btw, you also want to mysql_real_escape_string all your values here: (NULL,'$dateTime','$user_id','$s_time','$e_time','$request_date')"; needs to be '" . mysql_real_escape_string($dateTime) . "', '" . and so on. I think you can solve this now on your own.
Just offering an explanation since most here just did a "try this" routine. When you call mysql_query, the results returned is a MySQL Reasource, not an array or anything like that, but rather an object of it's own definition, so you can't compare to it directly. When you do $row = mysql_fetch_assoc($result); You're copying the data out of the resource into an Associative array (meaning you can reference the items in an array by their key instead of strictly numbers). Also you would want to check to see if $results === false and then row count (via mysql_num_rows), since your code would be fail if the Query failed and you didn't check to see if it failed first.