zed420 Posted August 24, 2009 Share Posted August 24, 2009 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 basically its this line that's not working. if (($result) == ($_POST['s_time'])) { 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 Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/ Share on other sites More sharing options...
trq Posted August 24, 2009 Share Posted August 24, 2009 Take a look at the manual entry for mysql_query. I'm not sure what you think it returns, but your code makes no sense at all. Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-904958 Share on other sites More sharing options...
5kyy8lu3 Posted August 24, 2009 Share Posted August 24, 2009 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 basically its this line that's not working. if (($result) == ($_POST['s_time'])) { 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 i'm not great with php but I think you need to get your results into a variable from your result set like such: $row = mysql_fetch_array($result); if ( $row['s_time'] == $_POST['s_time'] ) not sure that helps but i tried lol Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-904960 Share on other sites More sharing options...
zed420 Posted August 24, 2009 Author Share Posted August 24, 2009 All I'm trying to do is look into the database if there is a similar s_time as POSTED one, give an error msg else insert into databse. Zed Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-904961 Share on other sites More sharing options...
5kyy8lu3 Posted August 24, 2009 Share Posted August 24, 2009 <?php $query = "SELECT s_time,request_date FROM booking WHERE request_date = '$request_date'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); //you need to pull the results out so you can compare it to the request if ( $row['s_time'] == $_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')"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-904967 Share on other sites More sharing options...
zed420 Posted August 24, 2009 Author Share Posted August 24, 2009 Thanks for your reply, it still not having it Zed Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-904970 Share on other sites More sharing options...
trq Posted August 24, 2009 Share Posted August 24, 2009 All I'm trying to do is look into the database if there is a similar s_time as POSTED one, give an error msg else insert into databse. Define similar. Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-904982 Share on other sites More sharing options...
zed420 Posted August 24, 2009 Author Share Posted August 24, 2009 Sorry for not making myself clear, I'm POSTING hrs from a dropdown form, if the hr(s_time) from 8am to 9am has been booked(already in database) and someone tries to book this hr the error MSG should come up saying that it been booked please choose another. Zed Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-904988 Share on other sites More sharing options...
zed420 Posted August 24, 2009 Author Share Posted August 24, 2009 This code works but only for the first record meaning that you can not insert any more same records as the first one but after that any other record can be duplicated. why?? Thanks for your reply, it still not having it Zed Quote Link to comment https://forums.phpfreaks.com/topic/171615-query-not-working/#findComment-905053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.