Jump to content

mySQL Update isn't erroring but isnt doing anything


2tonejoe

Recommended Posts

i am running a script that updates a mySQL database. It runs without error, but it doesn't actually update anything. I have added a few echo's for the variables I am receiving from the form and the changes are coming over it is just that they aren't updating in the db. . . here is my code:

<?php

include('/Library/WebServer/Documents/scripts/db-connnect.php');

$username = $_POST['name'];
$userfacility = $_POST['facility'];
$userposition = $_POST['position'];
$useremail = $_POST['email'];
$usercontact = $_POST['contact'];
$userpass = $_POST['pass1'];
$userid = $_POST['userid'];
$sql = "UPDATE `users` SET `user_name` = '$username', `user_facility` = '$userfacility', `user_position` = '$userposition', `user_email` = '$useremail', `user_contact` = '$usercontact', `user_pass` = '$userpass', `update_date` = NOW( ) WHERE `users`.`user_id` ='$userid' LIMIT 1 ;";

mysql_select_db('addison');
$check = mysql_query("SELECT user_name FROM users WHERE user_name = '$username'") or die ("query 1: " . mysql_error());
$mysql_num = mysql_num_rows($check);

echo $mysql_num;

if ($mysql_num != 1)
{
echo "<br /><br /><br /><h2><center>I can't add <b>".$username."</b> because there is no such user.</h2></center>\n";
echo '<br /><h4><center>Please use the <a href="http://website.com/index.php?page=update-a-user">User Update Form</a> or simply <a href="' . $_SERVER['HTTP_REFERER'] . '">Retry</a> it.</h4></center>';
}
else
{ 
echo $_POST['email'];
mysql_query( $sql);
echo "<br><br><center><h2>The user <b>".$username."</b> was updated succesfully!</h2></center>\n";
echo '<br /><center><h4><a href="' . $_SERVER['HTTP_REFERER'] . '">Go Back</a></center></h4>';
}

mysql_close($mysql_link);

?> 

 

anyone have a guess as to what is happening . .? When I insert data it works fine btw. . . . using the came connection file too.

You define a query string for the UPDATE but you never execute the query.  And you need to select the database BEFORE any of your queries.

 

$sql = "UPDATE users ....."; // define query string

$result = mysql_query($sql); // execute the query and update

ok. so I change the code to the following and it still does nothing. It does error and outputs "The user myself was updated successfully!

<?php

include('/Library/WebServer/Documents/scripts/db-connnect.php');

$username = $_POST['name'];
$userfacility = $_POST['facility'];
$userposition = $_POST['position'];
$useremail = $_POST['email'];
$usercontact = $_POST['contact'];
$userpass = $_POST['pass1'];
$userid = $_POST['userid'];
$sql = "UPDATE `users` SET `user_name` = '$username', `user_facility` = '$userfacility', `user_position` = '$userposition', `user_email` = '$useremail', `user_contact` = '$usercontact', `user_pass` = '$userpass', `update_date` = NOW( ) WHERE `users`.`user_id` ='$userid' LIMIT 1 ;";

mysql_select_db('addison');
$check = mysql_query("SELECT user_name FROM users WHERE user_name = '$username'") or die ("query 1: " . mysql_error());
$mysql_num = mysql_num_rows($check);

if ($mysql_num != 1)
{
echo "<br /><br /><br /><h2><center>I can't add <b>".$username."</b> because there is no such user.</h2></center>\n";
echo '<br /><h4><center>Please use the <a href="http://222.223.22.35/index.php?page=update-a-user">User Update Form</a> or simply <a href="' . $_SERVER['HTTP_REFERER'] . '">Retry</a> it.</h4></center>';
}
else
{ 
$queryexecute = mysql_query( $sql);
echo "<br><br><center><h2>The user <b>".$username."</b> was updated succesfully!</h2></center>\n";
echo '<br /><center><h4><a href="' . $_SERVER['HTTP_REFERER'] . '">Go Back</a></center></h4>';
}

mysql_close($mysql_link);

?> 

 

i am lost. sorry for the blank looks guys. i appreciate everyones help.

when you type $sql = "UPDATE...."

 

you have to type $variable=mysql_query($sql).

 

or just do what you did below and type $sql=mysql_query("UPDATE...")  it's still a mysql_query even if it's an update.

do this after the update statement

mysql_query($sql);

???

 

my mind is numb. i am not getting this. you mean directly after i define "$sql=my update" I have to do "mysql_query( $sql);"? as in the next line. . . . I do that and still nothing. . . does anyone have a link to an example or something that could help me understand a little better. . ?

try and tell me what happen

<?php

include('/Library/WebServer/Documents/scripts/db-connnect.php');

$username = $_POST['name'];
$userfacility = $_POST['facility'];
$userposition = $_POST['position'];
$useremail = $_POST['email'];
$usercontact = $_POST['contact'];
$userpass = $_POST['pass1'];
$userid = $_POST['userid'];
$sql = "UPDATE `users` SET 
	`user_name` = '".$username."', 
	`user_facility` = '".$userfacility."', 
	`user_position` = '".$userposition."', 
	`user_email` = '".$useremail."', 
	`user_contact` = '".$usercontact."', 
	`user_pass` = '".$userpass."', 
	`update_date` = NOW( ) 
	WHERE users.user_id ='".$userid."' LIMIT 1 ;";

mysql_select_db('addison');
$check = mysql_query("SELECT user_name FROM users WHERE user_name = '$username'") or die ("query 1: " . mysql_error());
$mysql_num = mysql_num_rows($check);

if ($mysql_num != 1)
{
echo "<br /><br /><br /><h2><center>I can't add <b>".$username."</b> because there is no such user.</h2></center>\n";
echo '<br /><h4><center>Please use the <a href="http://222.223.22.35/index.php?page=update-a-user">User Update Form</a> or simply <a href="' . $_SERVER['HTTP_REFERER'] . '">Retry</a> it.</h4></center>';
}
else
{ 
$queryexecute = mysql_query( $sql)or die (mysql_error());
echo "<br><br><center><h2>The user <b>".$username."</b> was updated succesfully!</h2></center>\n";
echo '<br /><center><h4><a href="' . $_SERVER['HTTP_REFERER'] . '">Go Back</a></center></h4>';
}

mysql_close($mysql_link);

?> 

try to change this condition to more specific one look

if ($mysql_num != 1)

 

and echo this $mysql_num and see wats the result

and u uername in your select i guess its better using id

 

and can you explain the if else that you have

if you are sure that the query is executing, first check that the WHERE clause is correct (you may be using an incorrect variable or something while putting the username string in there).

 

reason i ask is that userid is typically a number and it doesnt look like you put one in there

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.