Jump to content

PHP Update MySQL Record


banfw001
Go to solution Solved by Ch0cu3r,

Recommended Posts

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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>  
Link to comment
Share on other sites

 

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

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.