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:

<form action="'.$u_a.'edituser" method="post" id="edit_user" enctype="multipart/form-data" >
<input name="id" value="" type="hidden" >
<input name="username" value="" type="text" >
<input name="password" value="" type="text" >
<input name="name" value="" type="text" >
<input name="fullname" value="" type="text" >
<input name="email" value="" type="text" >
<input name="phone" value="" type="text" >
<input name="mobile" value="" type="text" >
<input name="phone2" value="" type="text" >
<input name="country" value="" type="text" >
<input name="state" value="" type="text" >
<input name="postalcode" value="" type="text" >
<input value="Send" type="submit" >
</form>

Member Avatar for rajarajan2017

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

Hi,

it may be nothing but also check your syntax

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

should be

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

and so on

What do u mean with post error?

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

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:

<form action="edituser.php" method="post" >
<input name="id" value="someid" type="hidden" >
<input name="username" value="someusername" type="text" >
<input name="password" value="somepassword" type="text" >
<input name="name" value="somename" type="text" >
<input name="fullname" value="somefullname" type="text" >
<input name="email" value="someemail" type="text" >
<input name="phone" value="somephone" type="text" >
<input name="mobile" value="somemobile" type="text" >
<input name="phone2" value="somephone2" type="text" >
<input name="country" value="somecountry" type="text" >
<input name="state" value="somestate" type="text" >
<input name="postalcode" value="somepostalcode" type="text" >
<input value="Send" type="submit" >
</form>

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

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 !

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.