Why is it saying done when its not done

hi all

i want to update table columns from ajax popup

$qrydate2="update cart_table set modify_date='$mddate' where session_id='$session_id' and name='$uname'";
                    if(mysql_query($qrydate2))
                    {
                    echo "done";
                    }
                    else
                    {
                        echo "not done";
                    }

Everytime it echo’s result as “done”

But when i check the database records, then i see records are not updating.

So why is it saying “done” when “its not done” ??

how to debug it ??

when i run it directly in phpmyadmin the records are updating correctly.

why is it saying “done” when “its not done” ??

vineet

“Done” in your code simply says that the query has executed without errors, it doesn’t suggest that any records have been updated. Would need to see more of the code to be able to figure out why not, along with the values of the data being passed into it.

When you run from phpmyadmin, you will be entering the values directly into the query rather than using variables as you do above, so the first thing is to check that the variables contain what you expect them to contain. For example if that date is going into a date-format column, does it contain a valid date?

Also look at moving on to mysqli or (my preference) PDO to access the database as the old-style calls you are using are removed from the current PHP version.

2 Likes

Hi droopsnoot

i thought its showing done after updating records.

thanks for clearing the confusion.

so i want to echo “done” only if records have been updated then i will have to do a “select query” again or is there any other way also ??

vineet

If you switch to PDO, you can use rowCount to find out how many rows your query affected, so I’m guessing in your case you would want to check whether it updated zero rows or one row. I don’t know how to do it with the older functions, if it’s possible.

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

$qrydate2="update cart_table set modify_date='$mddate' where session_id='$session_id' and name='$uname'";
mysql_query($qrydate2);
                    if (mysql_affected_rows() > 0)
                    {
                    echo "done";
                    }
                    else
                    {
                        echo "not done";
                    }

That being said: Please switch to a modern externsion to work with mysql, since mysql itself is deprecated and doesn’t support a lot of good stuff like prepared statements. Also, use prepared statements and don’t just drop variables in queries. You can either switch to mysqli or PDO, the latter being my personal preference.

2 Likes

thanks scallioxtx

vineet

no it isn’t - it was deprecated in July 2013 and removed completely in December 2015 so it is no longer deprecated as it no longer exists.

Just to clear it up for the OP:

The mysql_* extension which you’ve used was removed from PHP as of version 7. If you try to use your code on any PHP server running version 7 of PHP or newer it won’t work.

The mysql_* extension was superceeded a few years ago with the mysqli_* extension (note the “i” in there). Any code needing to access a MySQL database should now be using the mysqli_* extension or the alternative which is PDO. Whether you use the mysqli_* extenion or PDO you should always use prepared statements when sending sanitized/validated user submitted data to the database.

User submitted data is any data recieved into a PHP script via any of the following super-global arrays:

  • $_POST
  • $_GET
  • $_FILES
  • $_COOKIE
  • $_REQUEST

I can’t remember off-hand if either the $GLOALS or $_ENV super-global arrays can be altered by the user so it’s probably best to also treat data in them two as well as suspect

PDO has the advantage over mysqli_* of the ability to use named parameters

$_ENV can certainly be manipulated by the user since HTTP headers are put in there.
$GLOBALS can only be manipulated by the user if register_globals is enabled which has been removed in PHP 5.4. That being said, I don’t know of anything $GLOBALS can solve that can’t also be solved in a nice way. My advice would be to stay clear of $GLOBALS.

since when was over 12 years considered to be a few years?

Both mySQLi and PDO were introduced in July 2004 as replacements for the old mysql_ interface.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.