I've put a database together with 5 fields, id, author, title, date, body. id is the primary key set as autoincrement.
Currently I have a page that displays each row in the database and has an option at the bottom to add a new entry. I want to add the option to edit a current entry. I've spent the last 2 hours doing trial/error and reading forums and I don't really feel like I've made any headway.
My Main page
<?php
$con = mysql_connect("XXXXX","XXXXX","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("XXXXX", $con);
$result = mysql_query("SELECT * FROM news");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Author</th>
<th>Title</th>
<th>Date</th>
<th>Body</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['body'] . "</td>";
echo "<td><a href='file133.php?id=" . $row['id'] . "'>edit</a></td>";
echo "</tr>";
}
echo "</table>";
echo "<form action='file132.php' method='post'>
<table>
<tr><td>Author:</td><td> <input type='text' name='author'></td></tr>
<tr><td>Title:</td><td> <input type='text' name='title'></td></tr>
<tr><td>Body:</td><td> <input type='text' name='body' size='75'></td></tr>
<tr><td></td><td><input type='submit' value='Submit'></td></tr>
</table>
</form>";
mysql_close($con);
?>
Clicking Edit takes you to this page where you can enter in the new data
<?php
$con = mysql_connect("XXXXX","XXXXX","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (isset($_GET['id']) AND $_GET['id'] > 0)
{
mysql_select_db("XXXXX", $con);
echo "<form action='file134.php' method='post'>
<table>
<tr><td>Author:</td><td> <input type='text' name='author'></td></tr>
<tr><td>Title:</td><td> <input type='text' name='title'></td></tr>
<tr><td>Body:</td><td> <input type='text' name='body' size='75'></td></tr>
<tr><td></td><td><input type='submit' value='Submit'></td></tr>
</table>
</form>";
}
mysql_close($con);
?>
When you click submit it goes to this page. I know this code below is all wrong because it just displays the actual code from the 0) to the end. I just need some direction on how to make this functional.
<?php
$con = mysql_connect("XXXXX","XXXXX","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (isset($_GET['id']) AND $_GET['id'] > 0)
{
mysql_select_db("XXXXX", $con);
$mysql_query = "UPDATE news SET Author = '$_POST[author]', title = '$_POST[title]', date = NOW(),body = '$_POST[body]' WHERE id= $row['id'] ";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Record updated, click <a href='file130.php'>here</a> to return to the list of records.";
mysql_close($con);
?>











