Here's the code I'm using to add and delete stuff out of a table:
<?php if (isset($_GET['addjoke'])): // User wants to add a joke
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40">
</textarea></label><br />
<input type="submit" value="SUBMIT" />
</form>
<?php else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', 'mypasswd');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
// Select the jokes database
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' .
'database at this time.</p>');
}
// If a joke has been submitted,
// add it to the database.
if (isset($_POST['joketext'])) {
$joketext = $_POST['joketext'];
$sql = "INSERT INTO joke SET
joketext='$joketext',
jokedate=CURDATE()";
if (@mysql_query($sql)) {
echo '<p>Your joke has been added.</p>';
} else {
echo '<p>Error adding submitted joke: ' .
mysql_error() . '</p>';
}
}
// If a joke has been deleted,
// remove it from the database.
if (isset($_GET['deletejoke'])) {
$jokeid = $_GET['deletejoke'];
$sql = "DELETE FROM joke
WHERE id=$jokeid";
if (@mysql_query($sql)) {
echo '<p>The joke has been deleted.</p>';
} else {
echo '<p>Error deleting joke: ' .
mysql_error() . '</p>';
}
}
echo '<p> Here are all the jokes in our database: </p>';
// Request the ID and text of all the jokes
$result = @mysql_query('SELECT id, joketext FROM joke');
if (!$result) {
exit('<p>Error performing query: ' .
mysql_error() . '</p>');
}
// Display the text of each joke in a paragraph
// with a "Delete this joke" link next to each.
while ($row = mysql_fetch_array($result)) {
$jokeid = $row['id'];
$joketext = $row['joketext'];
echo '<p>' . $joketext .
' <a href="' . $_SERVER['PHP_SELF'] .
'?deletejoke=' . $jokeid . '">' .
'Delete this joke</a></p>';
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo '<p><a href="' . $_SERVER['PHP_SELF'] .
'?addjoke=1">Add a Joke!</a></p>';
endif;
?>
Can someone add to this code and make it so that there's a link to "Edit" and it will take you to a form (with the data already in it) and you can update it from there (by just making the changes and hitting an Update button)?
BTW, I'd like this feature to work just like the add/delete ones (see how they use the same page).
Please let me know if you need any more information... I've been trying to accomplish this now for 2 days and I can't seem to make my own code work right.
Thank you.