banfw001 Posted November 28, 2013 Share Posted November 28, 2013 Hi, I am having a bit of a problem with a simple task and I was wondering if someone would be able to help. I have a php system which allows members to sign up, stores their information in a mysql database, then they can sign in and do all sorts of things. The area I have a problem with is I'm trying to make an update page so that they can edit their details stored in the mysql database. My code is as follows. <?php session_start(); include_once "base.php"; //connects to database $username = mysql_real_escape_string($_SESSION['Username']); // get users username from session $forename = mysqL_real_escape_string($_SESSION['Forename']; //gets already stored forename $newforename = mysql_real_escape_string($_POST['newforename']);//post for new forename $registerquery = mysql_query("INSERT INTO users WHERE Username = '".$username."'(Forename) VALUES('".$newforename."')"); //finds row for the user and updates the forename column with new record ?> <form method="post" action="index.php" name="registerform" id="registerform"> <fieldset> <label for="newforename">Forename:</label><input type="text" name="newforename" id="newforename" /><br /> <input type="submit" name="register" id="register" value="Register" /> </fieldset> </form> Unfortunately this code does not update the database at all, however it does not crash or produce any errors! I would really appreciate any help you can give with this. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 28, 2013 Share Posted November 28, 2013 INSERT adds a new record not update a record. You want to use an UPDATE query $registerquery = mysql_query("UPDATE users SET Forename='$newforename' WHERE Username = '$username'"); //finds row for the user and updates the forename column with new record Quote Link to comment Share on other sites More sharing options...
banfw001 Posted November 29, 2013 Author Share Posted November 29, 2013 INSERT adds a new record not update a record. You want to use an UPDATE query $registerquery = mysql_query("UPDATE users SET Forename='$newforename' WHERE Username = '$username'"); //finds row for the user and updates the forename column with new record Thanks for your fast response, however this did not work either. Any further help would be appreciated Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted November 29, 2013 Solution Share Posted November 29, 2013 Change your code to <?php session_start(); include_once "base.php"; //connects to database if(isset($_POST['register'])) { $username = mysql_real_escape_string($_SESSION['Username']); // get users username from session $forename = mysqL_real_escape_string($_SESSION['Forename']; //gets already stored forename $newforename = mysql_real_escape_string($_POST['newforename']);//post for new forename $registerquery = mysql_query("UPDATE users SET Forename='$newforename' WHERE Username = '$username'"); // check if the query has return an error if(!$registerquery) { // display the error die('Problem with changing the forename: ' . mysql_error()); } else { // check the query did change anything if(mysql_affected_rows()) { echo 'Forename has been updated!'; $_SESSION['Forename'] = $newforename; // update forename in the session } else { echo 'Forename has not been updated'; } } } ?> <form method="post" action="index.php" name="registerform" id="registerform"> <fieldset> <label for="newforename">Forename:</label><input type="text" name="newforename" id="newforename" /><br /> <input type="submit" name="register" id="register" value="Register" /> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
banfw001 Posted November 29, 2013 Author Share Posted November 29, 2013 Change your code to <?php session_start(); include_once "base.php"; //connects to database if(isset($_POST['register'])) { $username = mysql_real_escape_string($_SESSION['Username']); // get users username from session $forename = mysqL_real_escape_string($_SESSION['Forename']; //gets already stored forename $newforename = mysql_real_escape_string($_POST['newforename']);//post for new forename $registerquery = mysql_query("UPDATE users SET Forename='$newforename' WHERE Username = '$username'"); // check if the query has return an error if(!$registerquery) { // display the error die('Problem with changing the forename: ' . mysql_error()); } else { // check the query did change anything if(mysql_affected_rows()) { echo 'Forename has been updated!'; $_SESSION['Forename'] = $newforename; // update forename in the session } else { echo 'Forename has not been updated'; } } } ?> <form method="post" action="index.php" name="registerform" id="registerform"> <fieldset> <label for="newforename">Forename:</label><input type="text" name="newforename" id="newforename" /><br /> <input type="submit" name="register" id="register" value="Register" /> </fieldset> </form> That's great, thanks so much! Is there a way I can adjust the $update query so that it updates multiple columns like I had originally, so take for instance surname, address, tel number, etc... Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 29, 2013 Share Posted November 29, 2013 Yes. You'd format the query like $registerquery = mysql_query("UPDATE users SET Forename='$newforename', Surname='$newsurname', Address='$newaddress', Telnumber='$newtelnumber' WHERE Username = '$username'"); Quote Link to comment Share on other sites More sharing options...
banfw001 Posted November 29, 2013 Author Share Posted November 29, 2013 Yes. You'd format the query like $registerquery = mysql_query("UPDATE users SET Forename='$newforename', Surname='$newsurname', Address='$newaddress', Telnumber='$newtelnumber' WHERE Username = '$username'"); Thanks a lot! Your helps appreciated. 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.