sprint10s Posted June 1, 2009 Share Posted June 1, 2009 Hi all, i have never done this before so im not sure what to do? I need to write an update query so when the user changes the info in a form and clicks the submit button it will take the information and update the correct fields in the database. below is my code thank you for any help you can give me. attached is my form file [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/160479-update-database-query/ Share on other sites More sharing options...
aschk Posted June 1, 2009 Share Posted June 1, 2009 Just do what you would do for an insert and instead make it an update... See http://dev.mysql.com/doc/refman/5.0/en/update.html Quote Link to comment https://forums.phpfreaks.com/topic/160479-update-database-query/#findComment-846872 Share on other sites More sharing options...
gevans Posted June 1, 2009 Share Posted June 1, 2009 I'll start with a little honesty, I'm not going to download and read that script. If you posted some code inside and tags I would probably have a had a browse. By the sounds of things you don't really need to show it anyway.... so; You have a form, and in your method you point it at the same or a different php page, either way is fine. To ensure the form has been submitted you want to check for the submit button, or even better a hidden value. I'm assuming you're using POST and not GET seeing as this is user info. <?php if(isset($_POST['sumbit'])) { //the form has been submitted //our next code block will go in here } So you know the form has been submitted, now you need to check what values to update. I'm again assuming that you already put the default values in the form, so anything that hasn't been changed will have the original field values in there. That makes life easier as you can just go through the form fields and update the database with its content. <?php if(isset($_POST['id'])) { $id = (int) $_POST['id']; //you've got the user id, so you know what row to update (more on that in a second) if(isset($_POST['name'])) { //name is set $name = mysql_real_escape_string($POST['name']);//mysql_real_escape_string() requires a mysql db connection to be active. You need to put the connection code in before you start checking the form } else { $name = '';//you have a few options here. If the name field isn't set you can do some error reporting, tell the user there was an error, or just set it to a default value and carry on. } if(isset($_POST['email'])) { $email = mysql_real_escape_string($_POST['email']); //I put this in so that you'll have two fields to add to your sql. it will make it easier for you to expand on } else { $email = ''; } //the next chunk of code will go in here } else { //no id, you don't know what to update, error!!! } So you've got a couple of fields from a form, cleaned them and stored them in a variable. Next you want to build your sql syntax and run the query (I'm again assuming that you connected to mysql and your db before checking the form). <?php $sql = "UPDATE `your_table` SET `name`='$name', `email`='$email' WHERE `id`=$id"; //if you didn't have the user id you wouldn't know what row to update, and without the WHERE clause in the query you would update everyrow in your table. $result = mysql_query($query) or trigger_error("There was a problem: " . mysql_error()); if($result) { //All good, your table was updated } else { //There was a problem (check my code make sure I didn't mess up) } That should give you the basics for what you want to do. Have a look through what's going on, and put it together in a way that suits you. Quote Link to comment https://forums.phpfreaks.com/topic/160479-update-database-query/#findComment-846877 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.