Can NOT compare

Discussion in 'PHP' started by zed420, Aug 24, 2009.

  1. #1
    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:

     
    zed420, Aug 24, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change
        if (($result) == ($_POST['s_time'])) {
    PHP:
    to

    
        if (!empty($result) && $result['s_time'] == $_POST['s_time']) {
    PHP:
     
    premiumscripts, Aug 24, 2009 IP
  3. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks for your reply your code does make sense but it's still same. It inserts the times that are already there.
    Zed:confused:
     
    zed420, Aug 24, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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'])) {
     
    premiumscripts, Aug 24, 2009 IP
  5. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    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????:confused:
     
    zed420, Aug 24, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Do
    echo "<pre>"; print_r($row); echo "</pre>";
    PHP:
    and show us the output. Insert at correct location.
     
    premiumscripts, Aug 24, 2009 IP
  7. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    thats what I am getting

    Array
    (
    [s_time] => 1
    [request_date] => 1251241199
    )
     
    zed420, Aug 24, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    premiumscripts, Aug 24, 2009 IP
  9. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    kblessinggr, Aug 24, 2009 IP