Jump to content

mysql not updating database table for current user


silverglade

Recommended Posts

hi, can anyone please tell me why this statement does not work please? I  get the following 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 username = 1' at line 2

 

the $currentUser for the session is "1". so I know that is working.

 

but to update the existing row for the current user profile data, this is not working below.

 

//Writes the information to the database
$result= mysql_query("INSERT INTO users (firstName,lastName,birthday,country,city, state, aboutMe)
VALUES ('$firstName', '$lastName', '$birthday', '$country', '$city','$state','$aboutMe') WHERE username =$currentUser") or die(mysql_error());  

 

there is some info for that user already in the database row, but the firstName, lastname, etc fields are empty.

thank you. I changed it to update. I am getting the following error now. the username is 'd'.

 

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 '(firstName,lastName,birthday,country,city, state, aboutMe) VALUES ('d', '' at line 1

 

and my new code for putting the data in for the user is

 

//Writes the information to the database
$result= mysql_query("UPDATE users (firstName,lastName,birthday,country,city, state, aboutMe)
VALUES ('$firstName', '$lastName', '$birthday', '$country', '$city','$state','$aboutMe') WHERE username = $currentUser") or die(mysql_error());  


// get the first (and hopefully only) entry from the result
$row = mysql_fetch_assoc($result);
echo $row['firstName']." <br />".$row['lastName']. "<br />".$row['birthday']."<br />".$row['country'].
"<br />".$row['city']."<br />".$row['state']."<br />".$row['aboutMe'];

well to save face I figured I would put in my solution. not sure if it's kosher but it works.

 

//update user profile info based on logged in username
					$result = mysql_query("UPDATE users SET firstName='$firstName', lastName='$lastName',                         birthday='$birthday',country='$country',city='$city',state='$state',aboutMe='$aboutMe' WHERE                          username='$currentUser'") 
					or die(mysql_error());  

/////////////////////////////////output user info
						$sql = "SELECT firstName, lastName, birthday, country, city, state, aboutMe FROM users WHERE 				                             username='$currentUser'";


						$result = mysql_query($sql);

						if (!$result) {
							echo "Could not successfully run query ($sql) from DB: " . mysql_error();
							exit;
						}

						if (mysql_num_rows($result) == 0) {
							echo "No rows found, nothing to print so am exiting";
							exit;
						}
	//ECHO OUT CURRENT PROFILE INFORMATION FOR LOGGED IN USER.
						echo "Current Profile Information. Fill out form to update.<p></p>";
						while ($row = mysql_fetch_assoc($result)) {
							echo "First Name: ".$row["firstName"]."<br />";
							echo "Last Name: ".$row["lastName"]."<br />";
							echo "Birthday: ".$row["birthday"]."<br />";
							echo "Country: ".$row["country"]."<br />";
							echo  "State/Region: ".$row["state"]."<br />";
							echo "City: ".$row["city"]."<br />";
						    
							echo "About Me: ".$row["aboutMe"]."<br />";
						}

						mysql_free_result($result);
				}//end submit

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.