Hi - This is my script and this is the way it works:
1) User views records - checks a record to update - submits
2) User taken to form to edit record - submits form - records updated
Simple. But, it doesn't work and I don't understand why... when I press Update in the form I am simply taken back to stage 1) and the record has not been updated...
Thanks.
<?php
$conn= mysql_connect( "localhost", "a", "b" ) or die (mysql_error());
$db = mysql_select_db( "rugby_project", $conn) or die (mysql_error());
if (isset($_POST['update']) && isset($_POST['upboy'])) {
$delstring = '';
foreach ($_POST['upboy'] as $val) {
$delstring .= "'" . $val . "',";
}
$delstring = rtrim($delstring, ',');
$delquery = "SELECT * FROM phone_numbers WHERE userID IN (" . $delstring . ")";
$result = mysql_query($delquery) or die("Could not Edit User");
$row = mysql_fetch_array($result);
if ($result) // If the checkbox next to a record has been checked then it goes to this form to update that record:
{
$self = $_SERVER['PHP_SELF'];
$userID = $_POST['userID'];
$number = $_POST['number'];
$password = $_POST['password'];
if( (!$userID) or (!$number) or (!$password) )
{
$form ="<table width=\"271\" border=\"1\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\" bordercolor=\"#000000\">";
$form.="<tr bgcolor=\"#CCCCCC\"><td colspan=\"2\"><font size=\"1\" face=\"Verdana\"><center>";
$form.="Please Update Current User Details";
$form.="</td></font>";
$form.="</tr><tr>";
$form.="<td><font size=\"2\" face=\"Verdana\"><form action=\"$self\" name=\"adduser\" onsubmit=\"return valupdate()\"";
$form.=" method=\"post\">Username:</td>";
$form.="<td><input type=\"text\" name=\"userID\" value=\"{$row["userID"]}\">";
$form.="</td></tr><tr><td><font size=\"2\" face=\"Verdana\">Number:</td>";
$form.="<td><input type=\"text\" name=\"number\" value=\"{$row["number"]}\">";
$form.="</td></tr><tr><td><font size=\"2\" face=\"Verdana\">Password:</td>";
$form.="<td><input type=\"password\" name=\"password\" value=\"{$row["password"]}\">";
$form.="<tr><td><font size=\"2\" face=\"Verdana\">Password:<font size=\"1\" face=\"Verdana\"> repeat </td>";
$form.="<td><input type=\"password\" name=\"password2\">";
$form.="<tr><td colspan=\"2\"><center><input type=\"submit\" value=\"Update\">";
$form.="</tr></table></form>";
echo($form);
}
else
{ // When Update is pressed the UPDATE query is run:
$sql = "UPDATE phone_numbers SET userID='$userID',number='$number',password='$password' WHERE current_record_id='$current_id";
$result = mysql_query($sql,$conn) or die (mysql_error());
if($result)
{
echo( "The Details of $userID have been Updated" );
}
}
}
}
else
{
// This is the first table the user sees: Here they can check a box next to record to update it using the form above ^
$sql = "select userID, number, password from phone_numbers order by userID";
$rs = mysql_query( $sql,$conn);
$self = $_SERVER['PHP_SELF'];
$rows = mysql_num_rows($rs);
echo( "<table // header etc etc.... //
if(mysql_num_rows($rs))
{
echo( "<form name=\"viewuser\" method=\"post\" action=\"" . $self . "\">" );
while( $row = mysql_fetch_array( $rs ) ){
$userID=$row["userID"];
$number=$row["number"];
$password=$row["password"];
echo '<tr><td><font size="2" face="Verdana">' . $userID . '</td>';
echo '<td><font size="2" face="Verdana">' . $number . '</td>';
echo '<td><font size="2" face="Verdana">' . $password . '</td>';
echo '<td><center><input type="checkbox" name=" upboy[] " value="' . $userID . '" /></td>';
}
echo '<tr><td colspan="4"><center><input type="submit" name="update" onclick="validate();" value="Edit"/> <input type="reset" name="reset" value="Reset" /></td></tr></form>';
}
else
{
echo( "This table has no records yet" );
}
}
?>