Jump to content

update database query


sprint10s

Recommended Posts

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.