Having trouble UPDATING MySQL...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Having trouble UPDATING MySQL...

Post by Jim »

Every time I submit a form to the following page (editusers2.php) I get... nothing. No error, no problems. Just no results. Is there something wrong with the code?

Code: Select all

<?
include("config.php");

if($_POSTї'submit']) {

$sql = "update IamJim_users set user_name = '$new_user_name' and password = '$new_password' and user_email = '$new_user_email' where user_name = '$user_name'";
echo $sql;
$query = mysql_query($sql);

	if(!$query) {
		echo "Edit failed!";
		}
	}
	
?>
Thanks for help!
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Re: Having trouble UPDATING MySQL...

Post by f1nutter »

Maybe your getting no errors, because everything works! Your only printing error messages, not success!

Run the script and then have a look at the db with phpMyAdmin or whatever, to see if its been updated.

Also, try a few changes to your query: (note the use of single and double quotes)

Code: Select all

<?
include("config.php");

if($_POSTї'submit'])
{
 $sql = "update IamJim_users "
       . "set user_name = '" .$new_user_name. "' "
       . "and password = '" .$new_password. "' and "
       . "user_email = '" .$new_user_email. "' "
       . "where user_name = '" .$user_name. "'";

 echo $sql;
 $query = mysql_query($sql);

 if(!$query)
 {
  echo "Edit failed!";
 }
 else
 {
  echo "Success!";
 }
}
	
?>
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post by f1nutter »

Maybe there is something wrong with the code.

Is this the full script? If so where where you getting the variables from? Where does $new_user_name get set? Is it in the POST array? Add this before the query, and the other variables, and see what you get.

Code: Select all

$new_user_name = $_POSTї'new_user_name'];

echo $new_user_name;
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Another thing that is potentially deceptive about debugging scripts in browsers is that the erros aren't allways displayed by the browser.

So, check the source!

Cheers,
BDKR
abdul
Forum Newbie
Posts: 24
Joined: Thu Nov 14, 2002 7:35 am

Post by abdul »

I don't know "AND" works with MYSQL or not. But, it is always a good practice to write ANSI SQL for any database. Try this code..

Code: Select all

<? 
include("config.php"); 

if($_POST&#1111;'submit']) &#123; 

$sql = "update IamJim_users set user_name = '$new_user_name', password = '$new_password', user_email = '$new_user_email' where user_name = '$user_name'"; 
echo $sql; 
$query = mysql_query($sql); 

   if($query) 
     &#123; 
      echo "Table Updated..!"; 
      &#125; 
    else
     &#123; 
      echo "Edit failed!"; 
      &#125; 
   &#125; 
    
?>
Also, the best way to debug an SQL statement is to print it on the browser, copy it and run it directly on MYSQL client editor (Right clicking the mouse button will paste it on MySQL client under windows). It will give you a better error message.

All the best.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

deffinetly should have seen that. when listing values for a set directive, use the comma, not 'AND'. AND is for conditional use within a WHERE clause and other places that support it.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Works fine in PhpMyAdmin when I replace variables with actual names.
The script just won't seem to work it for me.

Maybe something is wrong with the values...
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Post by f1nutter »

f1nutter wrote: Is this the full script? If so where where you getting the variables from? Where does $new_user_name get set? Is it in the POST array? Add this before the query, and the other variables, and see what you get.

Code: Select all

$new_user_name = $_POST&#1111;'new_user_name'];

echo $new_user_name;
Better still use $_GET so you can see the variables in the address line for debugging, and when it works, go back to POST
Post Reply