MySQL Forums
Forum List  »  PHP

Figured out how to read, but how do I delete?
Posted by: Edward Comarzan
Date: June 08, 2005 10:23PM

Ok, I got this thing to read but If I continue to add data it makes every entry showup. I want it to show only the last entry I put. So to fix this problem I found some code to show me how to delete but I seem to be getting an error. I tweaked what I thought might work but im stuck in a rut again.

<body>
<?php

// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

// Connect and select.
if ($dbc = @mysql_connect ('localhost', '******', '*******')) {

if (!@mysql_select_db ('little')) {
die ('<p>Could select the database because: <b>' . mysql_error() . '</b></p>');
}

} else {
die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
}

if (isset ($_POST['submit'])) { // Handle the form.

// Define the query.
$query = "DELETE FROM ts1news WHERE ts1_title={$_POST['id']} LIMIT 1";
$r = mysql_query ($query); // Execute the query.

// Report on the result.
if (mysql_affected_rows() == 1) {
print '<p>The entries has been deleted.</p>';
} else {
print "<p>Could delete the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}

} else { // Display the entry in a form.

// Check for a valid entry ID in the URL.
if (is_numeric ($_GET['id']) ) {

// Define the query.
$query = "SELECT * FROM ts1news WHERE ts1_title={$_GET['id']}";
if ($r = mysql_query ($query)) { // Run the query.

$row = mysql_fetch_array ($r); // Retrieve the information.

// Make the form.
print '<form action="delete_entry.php" method="post">
<p>Are you sure you want to delete this entry?</p>
<p><h3>' . $row['title'] . '</h3>' .
$row['entry'] . '<br />
<input type="hidden" name="id" value="' . $_GET['id'] . '" />
<input type="submit" name="submit" value="Delete this Entry!" /></p>
</form>';

} else { // Couldn't get the information.
print "<p>Could retrieve the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}

} else { // No ID set.
print '<p><b>You must have made a mistake in using this page.</b></p>';
}

} // End of main IF.

mysql_close(); // Close the database connection.

?>
</body>

Options: ReplyQuote


Subject
Written By
Posted
June 08, 2005 07:50AM
Figured out how to read, but how do I delete?
June 08, 2005 10:23PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.