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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

but I am . . .  or at least I thought I was....

 

 

right underneath my "$sql=UPDATE" there is "mysql_select_db('addison');" and then the execution is the fifth line from the bottom "mysql_query( $sql);".

 

Am I simply doing this wrong??

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

???

 

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. . ?

Link to comment
Share on other sites

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);

?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

The fact that you can INSERT and not UPDATE is a clear sign that something is wrong with your query.  as i said, check your WHERE statement

 

I agree. However. . . I can't find the problem. Can anyone notice what I am doing wrong??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.