Jump to content

PHP & SQL Update


PHPnewby!

Recommended Posts

Hey guys,

I'm pretty new to php and am trying to create a page that allows member details to be updated within my "Members" table of my mysql database...according to the Username that is logged in!! (I want the user to be able to edit everything but their username). The key bits of code that I have so far are below. I am getting a parse error where I have marked ** and at this stage have just been trying to get the information to display within my form. "$editFormAction" and some areas of the form have not been declared as this code has been taken from elsewhere and I'm all confused...HELP!!!! THANK YOU VERY MUCH!!!!

<?php
session_start();
$User = $_SESSION['MM_Username'];
if (!$User) {
header("Location: memberfeatures.php");
}

$get_memberdetails_sql = "SELECT Firstname,Lastname,Emailaddress,Password,Moto FROM Members
WHERE Username = $User";
$get_memberdetails_res = mysql_query($get_memberdetails_sql) or die(mysql_error());

$Firstname = ($get_memberdetails_res,0,Firstname); **
$Lastname = ($get_memberdetails_res,0,Lastname);
$Emailaddress = ($get_memberdetails_res,0,Emailaddress);
$Password = ($get_memberdetails_res,0,Password);
$Moto = ($get_memberdetails_res,0,Moto);
?>

<form action="<?php echo $editFormAction; ?>" id="UpdateForm" name="UpdateForm" method="POST">
                      <p>User Name
                        <input name="UserNameUpdate" type="text" id="UserNameUpdate" value = "<?php echo $User;?>"/>"
                      </p>
                      <p>First Name
                        <input name="FirstNameUpdate" type="text" id="FirstNameUpdate" value = "<?php echo $Firstname;?>"/>"
                      </p>
                      <p>Last Name
                        <input name="SecondNameUpdate" type="text" id="SecondNameUpdate" value = "<?php echo $Secondname;?>"/>"
                      </p>
                      <p>E-mail Address
                        <input name="EmailUpdate" type="text" id="EmailUpdate" value = "<?php echo $Emailaddress;?>"/>"
                      </p>
                      <p>Password
                        <input name="PasswordUpdate" type="text" id="PasswordUpdate" value = "<?php echo $Password;?>"/>"
                      </p>
                      <p>Personal Moto
                        <input name="MotoUpdate" type="text" id="MotoUpdate" value = "<?php echo $Motto;?>"/>"
                      </p>
                      <p align="center">
                        <input name="Update" type="submit" id="Update" value="Update Details" />
                        <input type="hidden" name="MM_UPDATE" value="UpdateForm" />
                      </p>
                      <p align="center">&nbsp;</p>
                    </form>
Link to comment
https://forums.phpfreaks.com/topic/36060-php-sql-update/
Share on other sites

what exactly is this:
[code=php:0]$Firstname = ($get_memberdetails_res,0,Firstname);[/code]
trying to achieve. maybe my programming is old but i've never seen anything like that before.

Try running your query like this:
[code=php:0]
$get_memberdetails_sql = "SELECT* FROM Members WHERE Username = '{$User}'";
$get_memberdetails_res = mysql_fetch_array(mysql_query($get_memberdetails_sql));
[/code]

Then the other variables should look like this:
[code=php:0]
$Firstname = $get_memberdetails_res['Firstname'];
[/code]

Hope that clears some of it up for you.
Link to comment
https://forums.phpfreaks.com/topic/36060-php-sql-update/#findComment-171468
Share on other sites

Hi guys,

Thanks for the support so far! I have created the following two php pages and they appear to work with no error messages, but the new data is not being entered into the database in replace of the old. Any ideas? Do I need to add more information into the session to pass to do_modifyuser.php? (not sure how!??), or am I way off??? Thanks again!

[b]modifyuser.php:[/b]

<?php
session_start();
$User = $_SESSION['MM_Username'];
if (!$User) {
header("Location: memberfeatures.php");
}
require_once('Connections/TCO.php');
$get_memberdetails_sql = "SELECT* FROM Members WHERE Username = '{$User}'";
$get_memberdetails_res = mysql_fetch_array(mysql_query($get_memberdetails_sql));
$Firstname = $get_memberdetails_res['Firstname'];
*****etc******
?>

                  <td><form action="do_modifyuser.php" id="UpdateForm" name="UpdateForm" method="post">

  <?php echo "Your Username: ".$User; ?>
                    <p>First Name
                      <input name="FirstNameUpdate" type="text" id="FirstNameUpdate" value = "<?php echo $Firstname;?>"/>
                    </p>
*****etc******

[b]do_modifyuser.php:[/b]

<?php
session_start();
$User = $_SESSION['MM_Username'];
require_once('Connections/TCO.php');
$Firstname = $_POST["FirstNameUpdate"];
$Lastname = $_POST["SecondNameUpdate"];
$Emailaddress = $_POST["EmailUpdate"];
$Password = $_POST["PasswordUpdate"];
$Moto = $_POST["MotoUpdate"];
$sql = "UPDATE Members
Firstname = ".$FirstNameUpdate.", Lastname = ".$SecondNameUpdate.", Emailaddress = ".$EmailUpdate.", Password = ".$PasswordUpdate.", Moto = ".MotoUpdate." WHERE Username = $User";
header("Location: modified.php");
?>
Link to comment
https://forums.phpfreaks.com/topic/36060-php-sql-update/#findComment-172129
Share on other sites

this:

[code=php:0]$sql = "UPDATE Members
Firstname = ".$FirstNameUpdate.", Lastname = ".$SecondNameUpdate.", Emailaddress = ".$EmailUpdate.", Password = ".$PasswordUpdate.", Moto = ".MotoUpdate." WHERE Username = $User";[/code]

should be:

[code=php:0]$sql = mysql_query("UPDATE Members SET Firstname = ".$FirstNameUpdate.", Lastname = ".$SecondNameUpdate.", Emailaddress = ".$EmailUpdate.", Password = ".$PasswordUpdate.", Moto = ".MotoUpdate." WHERE Username = '{$User}'");[/code]

You forgot to add in the SET bit and i didn't see anywhere where you actually ran the query. Hope this helps.
Link to comment
https://forums.phpfreaks.com/topic/36060-php-sql-update/#findComment-172264
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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