954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with php / sql UPDATE returns error !!

I have no problem connecting to database, and the form is okay i guess. ¿Do anybody see the problem in here? It keeps returning me ERROR!

PHP code:

<?php

mysql_connect("$host", "$mysql_user", "$mysql_pass")or die("Error connecting."); 
mysql_select_db(users)or die("Can't access to database.");

if($id) {
	$sql = 'UPDATE members SET ';
	$sql .= 'username="'.$username.'",';
	$sql .= 'email="'.$email.'",';
	$sql .= 'name="'.$name.'",';
	$sql .= 'fullname="'.$fullname.'",';
	$sql .= 'sex="'.$sex.'",';
	$sql .= 'phone="'.$phone.'",';
	$sql .= 'phone2="'.$phone2.'",';
	$sql .= 'mobile="'.$mobile.'",';
	$sql .= 'country="'.$country.'",';
	$sql .= 'state="'.$state.'",';
	$sql .= 'postalcode="'.$postalcode.'",';
	$sql .= ' WHERE id="'.$id.'"';
	if (mysql_query($sql)) {
		echo 'Done!';
	} else {
		echo 'ERROR!';
	}
	mysql_close();
	
} else {
	echo 'There is no id';
};

?>


HTML FORM CODE:

mickeyci
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

What was the error shown?

Before the below statement add echo to check whether variable values are received.

echo $username;
echo $email;

if($id) {


Also check

$result = mysql_query($sql) or die ("Error in query: $query. ".mysql_error());
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

post error

raju_boini525
Junior Poster in Training
53 posts since Aug 2009
Reputation Points: 10
Solved Threads: 7
 

Hi,

it may be nothing but also check your syntax

$sql .= 'username="'.$username.'",';

should be

$sql .= "username=" . $username;


and so on

whiteyoh
Posting Pro in Training
479 posts since Jun 2009
Reputation Points: 15
Solved Threads: 15
 

What do u mean with post error?

mickeyci
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Echo your query to see if this is OK.
@whiteyoh - i think your code will return a mysql error because you're not enclosing variables value. your query will look like: UPDATE members SET username = lulu ; this way, mysql will look for a column named lulu to compare it with username

silviuks
Junior Poster in Training
96 posts since Apr 2006
Reputation Points: 10
Solved Threads: 15
 

Hi, i tried the RAJARAJAN advice and all the variables echoed correctly, the WHITEYO advice returns me error... so i tried sending the mysql_error to get the kind of error, and then this happens whit the next code...

<?php
mysql_connect("$host", "$mysql_user", "$mysql_pass")or die("Error connecting."); 
mysql_select_db(users)or die("Error connecting to database.");

$sql = 'UPDATE members SET username="'.$username.'", email="'.$email.'", name="'.$name.'", fullname="'.$fullname.'", sex="'.$sex.'", phone="'.$phone.'", mobile="'.$mobile.'", phone2="'.$phone2.'", country="'.$country.'", state="'.$state.'", postalcode="'.$postalcode.'", WHERE id="'.$id.'"';
if (mysql_query($sql)) {
	echo 'Update done!';
} else {
	print (mysql_error());
}
mysql_close();
?>


The last code returns me the next error:

" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id="74"' at line 1 ".

I tried looking up at the error and change the code for:

<?php
mysql_connect("$host", "$mysql_user", "$mysql_pass")or die("Error connecting."); 
mysql_select_db(users)or die("Error connecting to database.");

$sql = 'PDATE members SET username="'.$username.'", email="'.$email.'", name="'.$name.'", fullname="'.$fullname.'", sex="'.$sex.'", phone="'.$phone.'", mobile="'.$mobile.'", phone2="'.$phone2.'", country="'.$country.'", state="'.$state.'", postalcode="'.$postalcode.'", WHERE id="'.$id.'"';
if (mysql_query($sql)) {
	echo 'Update done!';
} else {
	print (mysql_error());
}
mysql_close();
?>


This only changed 'UPDATE' for 'PDATE', and returns me the next error:

" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PDATE members SET username="someusername", email="someemail", name="somename", ful' at line 1 "

The html form looks like this:


AT ALL IT KEEPS ME RETURNING ME ERROR... IM GETTING STUCKED IN HERE! HELP ME PLEASE!

mickeyci
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Oh my !! i get the error...

postalcode="'.$postalcode.'",

so, the comma in the last row... was that fckn comma... , so it shoul looks like this:

postalcode="'.$postalcode.'"


THANK YOU !

mickeyci
Newbie Poster
4 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

can u give me ur full coad with database coad i want to see it

anojs
Newbie Poster
7 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: