I have posted a simplified version of what I am trying to accomplish. The index page has two functions: to display the contents of the database table (each record having a link to the edit page along with its id#) and a form to insert new records into the database table. The second page has a form for updating the selected record. For some reason, this page is not picking up the form values to update the database with, although I am not getting any error messages. As a result, my update just wipes out the record.
I have tried echoing the query (all values are blank) and echoing other values along the way, but I can’t seem to pinpoint the source of the problem. Can anyone help me with this?
Here is the index.php page:
<?php
require_once('inc/connect_db.php');
$submit = $_POST['submit'];
// Add new record to database
if ($submit == 'Add Record') {
$fullname3 = addslashes($_POST['fullname']);
$age3 = addslashes($_POST['age']);
$date3 = addslashes($_POST['date']);
$query = "INSERT INTO test_updates
SET fullname = '$fullname3',
age = '$age3',
date = '$date3' ";
$result = mysql_query($query, $mysql_link);
$id3 = mysql_insert_id($result);
}
?>
<?php include_once('inc/header.php'); ?>
<div id = "wrapper" style="padding: 30px;">
<h1>Testing Update Database</h1>
<div id="database">
<p>
<form action="index.php" name="update_form" method="post" style="margin-left: 50px;">
<input type="submit" name="submit" class="button" value="Display Records" />
</form>
</p>
<?php
//display all information
if ($submit == 'Display Records') {
?>
<!--print out table containing the records-->
<table cellspacing="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</td>
<th>Date</th>
<th> </th>
</tr>
<?php
// select all the records
$query = "SELECT id, fullname, age, date
FROM test_updates
ORDER BY age ASC ";
$result = mysql_query($query, $mysql_link);
while ($record = mysql_fetch_array($result)) {
$id = $record['id'];
$fullname = stripslashes($record['fullname']);
$age = stripslashes($record['age']);
$date = stripslashes($record['date']);
$record_timestamp = strtotime($date);
$day = date('D, M j, Y', $record_timestamp);
?>
<tr>
<td style="text-align: center;"><?php print $id; ?></td>
<td style="text-align: left;"><?php print $fullname; ?></td>
<td style="text-align: center;"><?php print $age; ?></td>
<td style="text-align: center;"><?php print $date; ?></td>
<td><a href="edit_record.php?id=$id">Edit</a></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
</div>
<div id="form">
<form action="index.php" method="post">
<h3>Enter a Record</h3>
<p>Name: <input type="text" name="fullname" class="textbox" value="<?php print stripslashes($fullname3); ?>" /></p>
<input type="hidden" name="id" class="textbox" value="<?php print $id3; ?>" />
<p style="margin-left: 15px;">Age: <input type="text" name="age" class="textbox" value="<?php print stripslashes($name3); ?>" /></p>
<p style="margin-left: 10px;">Date: <input type="text" name="date" class="textbox" value="<?php print stripslashes($date3); ?>" /></p>
<p style="margin-left: 60px;"><input type="submit" name="submit" class="button" value="Add Record" /></p>
</form>
</div>
<div class="clear"></div>
</div> <!-- end of wrapper div -->
</body>
</html>
and here is the edit_record.php page:
<?php
require_once('inc/connect_db.php');
$id = $_GET['id'];
$submit = $_POST['submit'];
// test for id and submit variables
print("<p style=\\"font-weight: bold; color: green;\\">$id :: $submit</p>");
if ($submit == "Edit Record") {
$fullname = addslashes($_POST['$fullname']);
$age = $_POST['$age'];
$date = $_POST['$date'];
print("<p style=\\"font-weight: bold; color: green;\\">$id :: $fullname :: $age :: $date</p>");
$update_query = "UPDATE test_updates
SET fullname = '$fullname',
age = '$age',
date = '$date'
WHERE id = '$id' ";
$update_result = mysql_query($update_query, $mysql_link);
print("$update_query");
}
$query = "SELECT id, fullname, age, date
FROM test_updates
WHERE id = '$id' ";
$update_result = mysql_query($query, $mysql_link);
$row = mysql_fetch_row($update_result);
$id3 = $row[0];
$fullname3 = stripslashes($row[1]);
$age3 = $row[2];
$date3 = $row[3];
print("<p style=\\"font-weight: bold; color: green;\\">$id :: $fullname :: $age :: $date</p>");
?>
<?php include_once('inc/header.php'); ?>
<div id = "wrapper2">
<h1>Edit Records</h1>
<div id="form2">
<form action="edit_record.php?id=<?php print $id3; ?>" method="post">
<h3>Enter a Record</h3>
<p>Name:<input type="text" name="fullname" class="textbox" value="<?php print $fullname3; ?>" /></p>
<input type="hidden" name="id" class="textbox" value="<?php print $id3; ?>" />
<p style="margin-left: 15px;">Age: <input type="text" name="age" class="textbox" value="<?php print $age3; ?>" /></p>
<p style="margin-left: 10px;">Date: <input type="text" name="date" class="textbox" value="<?php print $date3; ?>" /></p>
<p style="margin-left: 60px;"><input type="submit" name="submit" class="button" value="Edit Record" /></p>
</form>
</div>
<div class="clear"></div>
<a href="index.php" style="margin-left: 300px;">Return to Home Page.</a>
</div> <!-- end of wrapper div -->
</body>
</html>