Hi guys,
I'm back, with yet another stumbling block. I am trying to make a page that displays details of a DB record and has the ability to update that record. So far I have working code to pull a record from the DB and inject it into a form.
The part I can't get right is updating the record with the form data. My code seems to be ignoring the if (isset($_POST['submit'])) just before the Insert function.
Funny thing is, I also have a registration page, where I can create a record... this is just a copy of that with the extra code added which is required to pull the record from the DB, and an UPDATE function in the final query instead of INSERT.
Can anyone see why this would not work:
<?php
$check = mysql_query("SELECT * FROM users")
or die(mysql_error());
echo "<p align='center'> </p>";
echo "<p align='center'> </p>";
echo "<table border='1' align='center'>";
echo "<tr><th width='90' align='center'>Username</th>";
echo "<th width='120' align='center'>Name</th>";
echo "<th width='70' align='center'>Status</th></tr>";
while($users = mysql_fetch_array( $check )){
$username = $users['username'];
echo "<tr><td align='center'>";
echo "<a href='$username'>$username</a>";
echo "</td><td>";
echo $users['name_f'] . " " . $users['name_l'];
echo "</td><td>";
echo $users['status'];
echo "</td><td width='50' align='center'>";
echo "<a href='/edit_users.php?name=$username';>Edit</a>";
echo "</td></tr>";
}
echo "</table>";
if (!isset($_GET['name'])){
echo "<p align='center'><font face='Tahoma' size='6'>Please make a selection</font></p>";
die('No Selection');
}
else
{
$name=$_GET["name"];
$edit=mysql_query("SELECT * FROM users WHERE username = '$name'") or die(mysql_error());
while($user = mysql_fetch_array($edit)){
?>
<form action ="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<DIV ALIGN="center">
<table border="0" cellpadding="0" cellspacing="0" width="50%" id="AutoNumber1" height="185"><tr>
<td width="64%" align="right" height="35"><font face="Tahoma"><b><font size="4">Username: </font></b><input type="text" name="username" tabindex=1 maxlength="10" size="20" value="<?php echo $user["username"];?>"></td>
<td width="36%" align="left" height="35"><input TYPE="radio" NAME="status" tabindex=8 CHECKED value="active"><b><font face="Tahoma" size="4">Active</b></td></tr>
<tr><td width="64%" align="right" height="35"><font face="Tahoma"><b><font size="4">First Name: </b><input type="text" name="name_f" tabindex=2 maxlength="15" size="20" value="<?php echo $user["name_f"];?>"></td>
<td width="36%" align="left" height="35"><input TYPE="radio" NAME="status" value="admin"><font face="Tahoma" size="4"><b>Admin</b></td></tr>
<tr><td width="64%" align="right" height="35"><font face="Tahoma"><b><font size="4">Last Name: </b><input type="text" name="name_l" tabindex=3 maxlength="15" size="20" value="<?php echo $user["name_l"];?>"></td>
<td width="36%" align="left" height="35"><input TYPE="radio" NAME="status" value="inactive"><font face="Tahoma" size="4"><b>Inactive</b></td></tr>
<tr><td width="64%" align="right" height="35"><font face="Tahoma"><b><font size="4">Address 1: </b><input type="text" name="address1" tabindex=4 maxlength="20" size="20" value="<?php echo $user["address1"];?>"></td></tr>
<tr><td width="64%" align="right" height="35"><font face="Tahoma"><b><font size="4">Address 2: </b><input type="text" name="address2" tabindex=5 maxlength="20" size="20" value="<?php echo $user["address2"];?>"></td></tr>
<tr><td width="64%" align="right" height="35"><font face="Tahoma"><b><font size="4">Home Phone: </b><input type="text" name="h_phone" tabindex=6 maxlength="10" size="20" value="<?php echo $user["h_phone"];?>"></td></tr>
<tr><td width="64%" align="right" height="35"><font face="Tahoma"><b><font size="4">Mobile Phone: </b><input type="text" name="m_phone" tabindex=7 maxlength="10" size="20" value="<?php echo $user["m_phone"];?>"></td>
<tr><td width="64%" align="right" height="50"></td>
<td width="36%" height="50"><font face="Tahoma"><input type="submit" name="submit" tabindex=9 value="Update"></td></tr>
</table>
</DIV>
</form>
<?php
}
if (isset($_POST['submit'])) {
//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['name_f'] | !$_POST['name_l'] | !$_POST['address1'] | !$_POST['address2'] | !$_POST['h_phone'] | !$_POST['m_phone'] | !$_POST['status'] ) {
header("Location: required_fields.php");
die('fields');
exit;
}
$insert = "INSERT INTO users (username, password, status)
VALUES ('".$_POST['username']."', '".$_POST['pass']."','".$_POST['status']."')";
$add_member = mysql_query($insert);
// now we insert it into the database
$update = "UPDATE users SET (username, name_f, name_l, address1, address2, h_phone, m_phone, status WHERE user = $name)
VALUES ('".$_POST['username']."','".$_POST['name_f']."','".$_POST['name_l']."','".$_POST['address1']."','".$_POST['address2']."','".$_POST['h_phone']."','".$_POST['m_phone']."','".$_POST['status']."')";
$add_member = mysql_query($update);
echo "Submitted";
}
}
?>
I have dotted a couple of echos and die('with comment') in there just to see where my code is exiting, if at all.
Sorry for the amount of code, but I think it's necessary to understand where it all ties together.
Thank you.
P.