Sure wish the "remember me" was the default setting for logging in. Save a lot of time and trouble. Let me do this again.
I'm running php5 and mysql5 I have edit.php page that takes info from table and populate fields. When submitted it goes to update.php and as the name implies it updates the table with altered info.
If you change just one tiny thing on edit.php page -> like one number in a phone number -> and hit submit it updates fine. But if you load edit.php and hit update without changing anything the update query fails.
I use mysql_affected_rows() to show success or failed to update message. Again no change it fails with no query error given with
You didn't ask any questions, nor did you show us any (useful and/or valid) code, so... in case you were expecting us to be able to help you in some way, you might want to do both of those.
Additionally, note that the entire mysql extension is severely outdated and has been deprecated in favor of newer extensions such as MySQLi or PDO.
I wonder if your code might be checking the number of affected rows rather than the success/failure of your query? Remember that an update query which changes nothing will affect no rows.
Yeah, I just ordered a book from Amazon that is about Php 5, Mysql 5 and MySQLi. My edit/update pages didn't give me any trouble with Php & MySQL 4, so something in MySQL has changed. I've had Php 5 on my server but MySQL 5 is new so that must have something to do with why all of a sudden my website is misbehaving.
$query = "UPDATE table SET f_name='$f_name', l_name='$l_name', phone='$phone', email='$email' WHERE user_id='$user_id'";
$result = mysql_query($query) or die("Query: $query\n<br />MySQL Error: " . mysql_error());
if (mysql_affected_rows() == 1)
{
echo "Update Successful";
}
else
{
echo "Update Failed";
}
The edit page form is being populated with the existing database information. Even if nothing is changed it should just re-enter the information since there is no script to check and see if the posting information is the same as what is already in the database and if so turn it away. Unless this is a new automatic "feature" in MySQL 5.
That depends on what you are trying to do - you're the one writing this, after all. Judging from the messages you display, am I right in guessing that you're wanting to check for an error? There may be a function for that.
Whether or not you consider it a success or failure is entirely up to what you expect to happen. If a row should be updated but non is, that sounds like failure. If someone "updates" their mailing info without changing anything, I would not consider that a failure -- just obsessive behavior on the part of the user. In the latter case, you should probably, as Weedpacket suggested, check for an error condition when the query runs rather than checking to see if any rows are affected.
also consider that checking if the user changed any fields, before querying mysql, could save you the trouble of trying to figure it out afterwards (as well as a round-trip to the DB).
... something which could be done on the client side of things, meaning you could even eliminate the trip to the server altogether.
And a server-side check can also eliminate the database hit, if you cache the current values in the session at the same time you send them to the client.
Lets say that someone has already entered information into the database. They then decide to edit that information. But once they get to the edit form and read what what they had already entered and decide to just leave it as is -> but rather than clicking on a button that takes them back to the starting page, they click on the update button they'll get an error message because PHP 5 will not reenter the same information.
Checking a single entry like say (cat) is easy, but checking to see if the contents of a POSTed textarea is identical to what is in the mysql field is not.
$q = SELECT duty FROM duties WHERE duty_id='$duty_id'";
$r = mysqli_query($dbc, $q) or die(mysqli_error($dbc));
if ($r === $_SESSION['duty']) { // also tried ($r == $_SESSION['duty'])
echo "Information is the same so it does not need updating. <a href='start.php'>Return to Starting Point</a>";
} else {
$qu = "UPDATE duties SET duty='" . $_SESSION['duty'] . "' WHERE duty_id='$duty_id'";
$ru = mysqli_query($dbc, $qu) or die(mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1) {
echo "Updated!";
} else {
echo "ERROR! The same information cannot be re-entered.";
}
}
I've tried this and several other ways of checking to see if the information is the same but it breezes right past them and goes straight for the update query and the error message.
If the purpose of PHP 5 not re-entering in the same data in an update is to save time and resources, but you have to perform an additional query to test the MySQL data against the posted variable data, then what have you gained? It seems this would take up more time and/or resources than just simply re-entering the same data.
Bookmarks