La actualización de PHP mysql_query siempre devuelve verdadero

$result always return true, even though there is no parameter passed to the SQL query.

Everything else is all right and I have tested it in the database.

<?php
     require('dbConnection.php');
    $lon   = $_POST['lon'];
    $lat   = $_POST['lat'];
    $time  = $_POST['time'];
    $date = $_POST['date'];
    $eTime = $_POST['eTime'];
    $eDate = $date;
    $orderID = $_POST['orderID'];


    if($db_found){

        $query = "UPDATE `PostmanLocation` 
        SET `longitude`= '$lon',`latitude`= '$lat',`time`= '$time', `date`='$date'
        WHERE `postID`= '$name'";

        $result=0;

        $result = mysql_query($query) or die("MySQL error:".mysql_error());
        echo $result;
        if($result==1){

                $query = "
                UPDATE `Order` 
                SET `eTime`= '$eTime',`eDate`= '$eDate' 
                WHERE `orderID` = 'orderID'";

                 $result=0;
                $result = mysql_query($query);
                echo $result;
                if($result == 1){
                    $response["success"] = 1;

                } else{  
                    $response["success"] =0;   
                }

        }else{
            $response["success"] = 0;
        }
        echo json_encode($response);
     }
    ?>

preguntado el 26 de noviembre de 13 a las 23:11

If you mean that some of the variables in your query don't exist that's no big deal, blank strings will be sent to the database, so no error there. You should be testing for the existence of data with isset() o algo similar. -

If blank strings are sent to the database, will the table date be updated to blank? -

That should be the case. Blank strings passed will overwrite what already exists in those rows/columns. -

I think if the input variable has no data, the update will fail. Or if even blank strings can be updated, how can I tell if the update is successful or not? -

There is no overwrite in my database with the blank strings input. -

1 Respuestas

$result = mysql_query($query) or die("MySQL error:".mysql_error());

In the instruction above, $result will never be false : either mysql_query returns true, or the script dies.

If you rather want to check if your UPDATE query had any effect at all, you can use this:

Use mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

http://us3.php.net/manual/en/function.mysql-affected-rows.php

Also note that mysql_ functions are deprecated. You should switch to PDO or mysqli.

respondido 27 nov., 13:00

Buena llamada, trigger_error() could be used instead if that's undesirable. - Jaspe

@Matthew, Jasper, thank you both. But after I delete "or die("MySQL error:".mysql_error())", $result is still true. - user2965590

@user2965590 well, mysql_query returns FALSE on error, is there any error in your query? - Mateo

@Matthew, there is no error returned in the query. So what shall I do to decide when an update fails? - user2965590

@user2965590 I updated my answer, have a look. You can see if an UPDATE actually updated anything using mysql_affected_rows(). Does that answer your question ? - Mateo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.