silverglade Posted February 20, 2012 Share Posted February 20, 2012 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 INSERT queries don't use a WHERE clause. UPDATE queries can, though. Quote Link to comment Share on other sites More sharing options...
silverglade Posted February 20, 2012 Author Share Posted February 20, 2012 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']; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 That syntax isn't correct for an UPDATE query. UPDATE table SET field1 = 'value1', field2 = 'value2' WHERE field = 'value' Quote Link to comment Share on other sites More sharing options...
silverglade Posted February 20, 2012 Author Share Posted February 20, 2012 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 Quote Link to comment Share on other sites More sharing options...
silverglade Posted February 20, 2012 Author Share Posted February 20, 2012 thank you for helping Pikachoo Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.