I took the script below from Spoono, and edited it slightly to how I wanted it. The script should do three things:
[1] Display a list of rows in the database, with a link on the unique ID.
[2] Populate the form fields with the row info when selected.
[3] Update the row with the new information from the form when submit is pressed.
1 and 2 are working fine, but the row is not being updated when I hit submit - by adding some echo statements in there, it seems that the hitting of the submit button is not even being recognised, but I could be wrong about that. Any comments would be really appreciated:
Code: Select all
<?php include 'connect.php'; ?>
<?
//connect to mysql
//then connect as user
mysql_connect($host,$user,$pass);
//select which database you want to edit
mysql_select_db($db_name);
//If cmd has not been initialized
if(!isset($cmd))
{
$result = mysql_query("select id, Forename, Surname, CurrentGrade, LicenseExpiry, Phone, Mobile, Email from members order by id");
//print table headers
$fields_num = mysql_num_fields($result);
echo "<h5>Table: {$table}</h5>";
echo "<div id=table><table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>";
//run the while loop that grabs all the Member Details
while($r=mysql_fetch_array($result))
{
$id=$r["id"];//take out the id
$Forename=$r["Forename"];//take out the forename
$Surname=$r["Surname"];//take out the surname
$CurrentGrade=$r['CurrentGrade'];
$LicenseExpiry=$r['LicenseExpiry'];
$Phone=$r['Phone'];
$Mobile=$r['Mobile'];
$Email=$r['Email'];
//make the ID a link
echo "<tr><td><a href='edit.php?cmd=edit&id=$id'>$id</td>";
echo "<td>$Forename</td>";
echo "<td>$Surname</td>";
echo "<td>$CurrentGrade</td>";
echo "<td>$LicenseExpiry</td>";
echo "<td>$Phone</td>";
echo "<td>$Mobile</td>";
echo "<td>$Email</td>";
echo "</tr>";
}
echo "</table></div>";
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM $table WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<div style="clear: both;"> </div>
<input type="hidden" id="DPC_TODAY_TEXT" value="today">
<input type="hidden" id="DPC_BUTTON_TITLE" value="Open calendar...">
<input type="hidden" id="DPC_MONTH_NAMES" value="['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']">
<input type="hidden" id="DPC_DAY_NAMES" value="['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']">
<form action="edit.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
<fieldset>
<legend>Personal & Contact Details</legend>
<ol>
<li>
<label for="Forename">Forename: <em>required</em></label>
<input id="Forename" name="Forename" class="text" type="text" VALUE="<?php echo $myrow["Forename"] ?>" />
</li>
<li>
<label for="Surname">Surname: <em>required</em></label>
<input id="Surname" name="Surname" class="text" type="text" VALUE="<?php echo $myrow["Surname"] ?>" />
</li>
<li>
<label for="DateOfBirth">Date of Birth:</label>
<input id="DateOfBirth" name="DateOfBirth" class="text" type="text" datepicker="true" datepicker_format="YYYY-MM-DD" VALUE="<?php echo $myrow["DateOfBirth"] ?>" />
</li>
<li>
<label for="Address">Address:</label>
<textarea id="Address" type="text" name="Address" datepicker="true"><?php echo $myrow["Address"] ?></textarea>
</li>
<li>
<label for="Postcode">Postcode:</label>
<input id="Postcode" name="Postcode" class="text" type="text" VALUE="<?php echo $myrow["Postcode"] ?>" />
</li>
<li>
<label for="Phone">Telephone:</label>
<input id="Phone" name="Phone" class="text" type="text" VALUE="<?php echo $myrow["Phone"] ?>" />
</li>
<li>
<label for="Mobile">Mobile:</label>
<input id="Mobile" name="Mobile" class="text" type="text" VALUE="<?php echo $myrow["Mobile"] ?>" />
</li>
<li>
<label for="Email">Email:</label>
<input id="Email" name="Email" class="text" type="text" VALUE="<?php echo $myrow["Email"] ?>" />
</li>
<li>
<label for="Occupation">Occupation:</label>
<input id="Occupation" name="Occupation" class="text" type="text" VALUE="<?php echo $myrow["Occupation"] ?>" />
</li>
</ol>
</fieldset>
<fieldset>
<legend>Karate Information</legend>
<ol>
<li>
<label for="CurrentGrade">Current Grade:</label>
<input id="CurrentGrade" name="CurrentGrade" class="text"
type="text" VALUE="<?php echo $myrow["CurrentGrade"] ?>" />
</li>
<li>
<label for="DateAcheived">Date Acheived</label>
<input id="DateAcheived" name="DateAcheived" class="text"
type="text" datepicker="true" datepicker_format="YYYY-MM-DD" VALUE="<?php echo $myrow["DateAcheived"] ?>" />
</li>
<li>
<label for="LicenseNumber">License Number:</label>
<input id="LicenseNumber" name="LicenseNumber" class="text"
type="text" VALUE="<?php echo $myrow["LicenseNumber"] ?>" />
</li>
<li>
<label for="LicenseExpiry">License Expiry Date:</label>
<input id="LicenseExpiry" name="LicenseExpiry"
class="text textSmall" type="text" datepicker="true" datepicker_format="YYYY-MM-DD" VALUE="<?php echo $myrow["LicenseExpiry"] ?>" />
</li>
</ol>
</fieldset>
<fieldset>
<legend>Other Information</legend>
<ol>
<li>
<label for="MedicalProblem">Medical Information:</label>
<textarea id="MedicalProblem" type="text" name="MedicalProblem"><?php echo $myrow["MedicalProblem"] ?></textarea>
</li>
<li>
<label for="Medication">Medication Taken:</label>
<textarea id="Medication" type="text" name="Medication"><?php echo $myrow["Medication"] ?></textarea>
</li>
<li>
<label for="CriminalRecord">Criminal Record (details if applicable):</label>
<textarea id="CriminalRecord" type="text" name="CriminalRecord"><?php echo $myrow["CriminalRecord"]?> </textarea>
</li>
</ol>
</fieldset>
<fieldset class="submit">
<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit" />
</fieldset>
</form>
<? }
?>
<?
if ($_POST["$submit"])
{
$Forename = $_POST["Forename"];
$Surname = $_POST["Surname"];
$CurrentGrade = $_POST['CurrentGrade'];
$DateAcheived = $_POST['DateAcheived'];
$LicenseNumber = $_POST['LicenseNumber'];
$LicenseExpiry = $_POST['LicenseExpiry'];
$DateOfBirth = $_POST['DateOfBirth'];
$MedicalProblem = $_POST['MedicalProblem'];
$Medication = $_POST['Medication'];
$CriminalRecord = $_POST['CriminalRecord'];
$Address = $_POST['Address'];
$Postcode = $_POST['Postcode'];
$Phone = $_POST['Phone'];
$Mobile = $_POST['Mobile'];
$Email = $_POST['Email'];
$Occupation = $_POST['Occupation'];
$sql = "UPDATE $table SET Forename='$Forename',Surname='$Surname',CurrentGrade='$CurrentGrade',DateAcheived='$DateAcheived',LicenseNumber='$LicenseNumber',LicenseExpiry='$LicenseExpiry',DateOfBirth='$DateOfBirth',MedicalProblem='$MedicalProblem',Medication='$Medication',CriminalRecord='$CriminalRecord',Address='$Address',Postcode='$Postcode',Phone='$Phone',Mobile='$Mobile',Email='$Mobile',Occupation='$Occupation' WHERE id=$id";
$result = mysql_query($sql);
echo "Thank you! Information updated.";
}
}
?>