Forum Moderators: coopster

Message Too Old, No Replies

can't seem to update mysql

         

generic

8:36 am on Apr 6, 2009 (gmt 0)

10+ Year Member



I'm trying to figure out how to get this little update query working for me, any help or comments would be appreciated.

Thanks in advance!

<?php
// edit user profile
$u = $_GET['u'];

// create update form
function createform() {
echo '<form action="cms.php?action='. $_GET['action'] .'&amp;u='. $_SESSION['uid'] .'" method="POST">
<p>First Name: <INPUT TYPE="TEXT" NAME="firstname" VALUE="'. $_SESSION['firstname'] .'" /></p>
<p>Last Name: <INPUT TYPE="TEXT" NAME="lastname" VALUE="'. $_SESSION['lastname'] .'" /></p>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="edit_profile" value="1">
</form>';
}

// check for form submit
if (($_POST['edit_profile'] == '1') && ($action == 'edit'))
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

$query = "UPDATE users SET firstname ='$firstname', lastname = '$lastname' WHERE id = ('$u')";
$result = mysql_query($query);
if (mysql_query($query))
{
echo '<h4>Success - Profile Updated</h4>
<p><a href="cms.php">Click here</a> to continue.</p>';
} else {
echo '<h4>Error - Profile Not Updated';
echo '<p>'. mysql_error() .'</p>';
createform();
} // end if

} else {

$query = "SELECT * FROM users WHERE id ='$u' LIMIT 1";
$result = mysql_query($query);
while($row=mysql_fetch_array($result))
{
echo '<h4>Edit User Profile</h4>';
createform();
} // end while
} // end main if

?>

Habtom

8:42 am on Apr 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This part of the code has got a few errors:

$query = "UPDATE users SET firstname ='$firstname', lastname = '$lastname' WHERE id = ('$u')";
$result = mysql_query($query);
if (mysql_query($query))

Corrected:

$query = "UPDATE users SET firstname ='$firstname', lastname = '$lastname' WHERE id = '$u'";
if (mysql_query($query))

generic

8:45 am on Apr 6, 2009 (gmt 0)

10+ Year Member



Thanks for the quick reply. I made the change but still no luck. Any other ideas?

Habtom

8:49 am on Apr 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo the $query line, and see if you have alright.

$query = "UPDATE users SET firstname ='$firstname', lastname = '$lastname' WHERE id = '$u'";
echo $query;
if (mysql_query($query))

generic

9:05 am on Apr 6, 2009 (gmt 0)

10+ Year Member



Woops, it was updating the db alright but when I returned to the script, it was calling the session info to populate the fields. Duh... :) So update is working fine now.

However, now it's not populating the fields in createform() anymore.

<?php
// edit user profile
$u = $_GET['u'];

// create update form
function createform() {
echo '<form action="cms.php?action='. $_GET['action'] .'&amp;u='. $_SESSION['uid'] .'" method="POST">
<p>First Name: <INPUT TYPE="TEXT" NAME="firstname" VALUE="'. $row['firstname'] .'" /></p>
<p>Last Name: <INPUT TYPE="TEXT" NAME="lastname" VALUE="'. $row['lastname'] .'" /></p>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="edit_profile" value="1">
</form>';
}

// check for form submit
if (($_POST['edit_profile'] == '1') && ($action == 'edit'))
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

$query = "UPDATE users SET firstname ='$firstname', lastname = '$lastname' WHERE id = '$u'";
$result = mysql_query($query);
if (mysql_query($query))
{
// update session
$_SESSION['firstname'] = $firstname;
$_SESSION['lastname'] = $lastname;

echo '<h4>Success - Profile Updated</h4>
<p><a href="cms.php">Click here</a> to continue.</p>';
} else {
echo '<h4>Error - Profile Not Updated';
echo '<p>'. mysql_error() .'</p>';
createform();
} // end if

} else {

$query = "SELECT * FROM users WHERE id ='$u' LIMIT 1";
$result = mysql_query($query);
while($row=mysql_fetch_array($result))
{
echo '<h4>Edit User Profile</h4>';
createform();
} // end while
} // end main if

?>

generic

9:16 am on Apr 6, 2009 (gmt 0)

10+ Year Member



Ok looks like I figured it out.

Thanks so much for your help with the update :) Cheers.