PDA

View Full Version : Deleting a row from a MySql Table.



jw06
June 28th, 2004, 01:29 PM
Im making an admin panel and when I try to delete a news post I hit the delete link and it should go through it all ok, and it does in sense. It shows no error, but it always says: You must have made a mistake in using this page. Do you see anything in there that is causing it to not get the id?



<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

if ($dbc = @mysql_connect ('localhost', 'root', '')) {
if (!@mysql_select_db ('news')) {
die ('Could not select the database because: <b>' . mysql_error() . '</b>');
}
} else {
die ('Could not connect to MySQLbecause: <b>' . mysql_error() . '</b>');
}

if (isset ($_POST['submit'])) {

//Define the query
$query = "DELETE FROM news_entries WHERE news_id={$_GET['id']}";
$r = mysql_query ($query); // Execute the query.

//Report result
if (mysql_affected_rows() == 1) {
print '<p>The Blog Entry Was Deleted</p>';
} else {
print "<p>Could not delete the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}
} else {
//Display the entry in a form
if (is_numeric ($_GET['id'])) {
//Define the query.
$query = "SELECT * FROM news_entries WHERE news_id={$_GET['id']}";
if ($r == mysql_query ($query)) {
$row = mysql_fetch_array ($r);

//Make the form
print '<form action="deleteentry.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 {
print "<p>Could not retrieve the entry because: <b>" . mysql_error() . "</b>.";
}
} else { // No Id Set
print '<p><b>You must have made a mistake in using this page.</b></p>';
}
} // End main IF
mysql_close();

?>

hamza84
June 28th, 2004, 03:54 PM
try assigning the $_GET['id'] in a variable, for instance, $id = $_GET['id'] and then try using that variable in the mysql delete statement.

Digitalosophy
June 28th, 2004, 04:27 PM
agreed.

also a great idea to trouble shoot is printing the SQL before executing it. this way you can see if your "id" is getting passed. :)

Hans Kilian
June 28th, 2004, 05:40 PM
A couple of things...

I'd use
if ($_SERVER['REQUEST_METHOD'] == 'POST')
to figure out if I'm being called with the post or get method.

In your SQL where you delete the entry, you use $_GET['id']. You're being called with the post method so it should be $_POST['id'] instead.

I hope that helps...

opel
July 1st, 2004, 06:06 PM
I am having a similar problem. I a have few questions though.

I have created a job vacancies board and every Job has a "jobID" in the database which is set to the primary Key.

I want to let the admin people enter the "job id" they want to delete. Looking at your script it seems to be what I want to do. However I just want to check that your "id" label is one of the elements of your table and I can alter the script to do what I want to do?

Thanks

ya3
July 1st, 2004, 07:40 PM
I could be wrong, but im pretty sure the code should have the quotation marks around variable names, like this:
"DELETE FROM `news_entries` WHERE `news_id` = {$_GET['id']}"

...thats my 0.02

Hans Kilian
July 1st, 2004, 08:24 PM
ya3, the quotes are not necessary. Actually, the only place I've seen them used is in MS products. All the other SQL databases I've used (DB2, MySQL, SAS) don't use quotes around column names.