PDA

View Full Version : What Is Wrong With This Code?


Troy297
12-05-2006, 01:58 AM
Hey All,

I am making a PHP/MySQL member system script but I am having major trouble with the editing a user part (update & set in php).... It will perform the query and shows it successful but nothing is actually changed!!! Below is the code... and if anyone could help me out it would be really appreciated!

edituser.php
<form method='POST' action='edituser.php'>
<?php
session_start();
if ($_POST['eusername']) {
$eusername=$_POST['eusername'];
$query = mysql_query("SELECT username,passwrd,rank,email FROM users WHERE username = '$eusername'") or die(mysql_error());
$row = mysql_fetch_array($query);
$_SESSION["eusername"] = $row['username'];
$_SESSION["epasswrd"] = $row['passwrd'];
$_SESSION["eemail"] = $row['email'];
$_SESSION["erank"] = $row['rank'];
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=edituser2.php\">";
}
?>

<?php
$result = mysql_query("SELECT username FROM users ORDER BY username ASC");
echo '<select name="eusername">';
while($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['username'] . '">' . $row['username'] . '</option>';
}
echo '</select>';
?>
<p><input type="submit" name="submit" value="Edit">
</form>


edituser2.php
<?php
if ($_POST['submit']) {
$result = mysql_query("UPDATE users SET passwrd = '$edpasswrd',email = '$edemail',rank = '$edrank' WHERE username = '$edusername'") or die(mysql_error());
echo "<center><b>The users details has been successfully updated.</b></center>";
}
?>

<?php
echo "
<form method='POST' action='edituser2.php'>
<input type='hidden' name='edusername' value='$_SESSION[eusername]'>
Password:<br>
<input type='text' name='edpasswrd' value='$_SESSION[epasswrd]'><p>
Email:<br>
<input type='text' name='edemail' value='$_SESSION[eemail]'><p>
Rank:<br>
<select name='edrank'>
<option selected>User</option>
<option>Admin</option>
<option>Suspended</option>
</select><p>
<input type='submit' name='submit' value='Edit'>
</form>
";?>

Thanks For Any Help At All!!! :)

Spookster
12-05-2006, 02:15 AM
In edituser2.php I don't see where you are updating the variables in your UPDATE query. You need to pull the updated data from the form and put those values into the variables you are putting into your UPDATE query.

Troy297
12-05-2006, 03:07 AM
So.... to do that then what would my query look like? I am not very experienced in using PHP and MySQL together...... as far as I knew I thought I was updating the data...?

Doesn't the "SET passwrd = '$edpasswrd'......" input the form data into the database?

Thanks Again.

Fumigator
12-05-2006, 03:17 AM
That is what the SET statement does, but where are you assigning a value to $edpassword?

Troy297
12-05-2006, 03:25 AM
the form (in edituser2.php) is responsible for submitting all the "ed..." values..... am I using the values correctly? or should it be "$_POST[edpasswrd]" instead of just "$edpasswrd"?

and just to recap.... is my query correct? am I updating it correctly?

Spookster
12-05-2006, 03:38 AM
or should it be "$_POST[edpasswrd]" instead of just "$edpasswrd"?




Yes you should pull the value from the form. It will not automatically be in those variables ie $edpasswrd without you assigning a value to them ie

$edpasswrd = $_POST[edpasswrd];

Troy297
12-05-2006, 12:24 PM
Wow! Thank you so much! I have been working on that problem for over a week and it was such and easy fix...... Once I am finished you can view the end result here (http://www.quickscriptz.ca.kz).