...

View Full Version : Resolved SQL Update - Success/Failure



weir-07
03-29-2009, 02:49 AM
Was wondering if you guys might be able to to help me out.

Currently stuck with part of a User Activation set up i have.


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "stage2b")) {
$updateSQL = sprintf("UPDATE accounts SET status=%s WHERE username=%s AND activate_code=%s",
GetSQLValueString($_POST['status'], "text"),
GetSQLValueString($_POST['username'], "text"),
GetSQLValueString($_POST['activatecode'], "text"));

mysql_select_db($database_STC, $STC);
$Result1 = mysql_query($updateSQL, $STC) or die(mysql_error());

//SUCCESS
if ($Result1 = mysql_query($updateSQL, $STC)) {
$updateGoTo = "regsystem.php?go=2complete"; }
//FAILURE
else $updateGoTo = "regsystem.php?go=2failure";
if (isset($_SERVER['QUERY_STRING']))
header(sprintf("Location: %s", $updateGoTo));

Basically i am trying to go to "2complete" if the UPDATE is a success - and go to "2failure" if it does not work.

Anybody got any ideas? I've had a look around these forums and been playing with it for a while - would be very great full to anybody would might be able to help! :)

NOTE: the script works fine whenever the right details are entered - however displays the Success page even when it doesn't update the account...

ShaneC
03-29-2009, 03:09 AM
Take out the $Result1 part. The function mysql_query returns a boolean, TRUE if it was a success and FALSE if it was a failure.

Exampe:



<?php

if( $mysql_query( $updateSQL, $STC ) ){
$updateGoTo = "regsystem.php?go=2complete";
}else{
$updateGoTo = "regsystem.php?go=2failure";
}

?>
What you are doing, though, is a bit redunant. In fact it may result in two queries being executed. Keep in mind that everytime you call mysql_query(), whether in an IF statement or not, it will execute that function.

Try this:



<?php

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "stage2b")) {
$updateSQL = sprintf("UPDATE accounts SET status=%s WHERE username=%s AND activate_code=%s",
GetSQLValueString($_POST['status'], "text"),
GetSQLValueString($_POST['username'], "text"),
GetSQLValueString($_POST['activatecode'], "text"));

mysql_select_db($database_STC, $STC);
$Result1 = mysql_query($updateSQL, $STC) or die(mysql_error());

//SUCCESS
// $Result1 will have been given a bool value based on the execution of the last query
if ($Result1 ) {
$updateGoTo = "regsystem.php?go=2complete"; }
//FAILURE
else $updateGoTo = "regsystem.php?go=2failure";
if (isset($_SERVER['QUERY_STRING']))
header(sprintf("Location: %s", $updateGoTo));

?>


Now having said that please be aware that in this script the $updateGoTo = "regsystem.php?go=2failure"; will never fire. That's because you have or die(mysql_error()); on your initial query. The OR statement is essentially saying "This returns true OR I do this." Take that out if you'd like $updateGoTo = "regsystem.php?go=2failure"; to fire when there is an error.

Hope this helps! For more on the mysql_query and what is returns, Check out the PHP Manual (http://us2.php.net/manual/en/function.mysql-query.php).

Added some examples

steelaz
03-29-2009, 03:15 AM
if ($Result1 = mysql_query($updateSQL, $STC))

Line above will always return true, unless there's problem with the database. What you need is mysql_affected_rows() (http://www.php.net/function.mysql-affected-rows).

weir-07
03-29-2009, 03:31 AM
if ($Result1 = mysql_query($updateSQL, $STC))

Line above will always return true, unless there's problem with the database. What you need is mysql_affected_rows() (http://www.php.net/function.mysql-affected-rows).

That did the trick! Thank you very much both of you!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum