Page 1 of 1

Deleteing Rows

Posted: Sun Apr 11, 2004 8:01 pm
by AlbinoJellyfish
I made a deletion script.

Code: Select all

<?PHP
$delete = $_POST['delete'];
if ($delete = 'delete') {
$db = mysql_connect("localhost", "root"); 
mysql_select_db("news" , $db) or die("Couldn't open $db: ".mysql_error()); 
    $sql = "DELETE FROM news WHERE id=$id";	

    $result = mysql_query($sql);

    echo "$sql Record deleted!<p>";
	} else {
	echo '<a href="../index.php">Click here to continue</a>';
	}
?>
It actually doesnt even delete. It just adds another line of blanks.

Posted: Sun Apr 11, 2004 8:06 pm
by AlbinoJellyfish
Fixed a few errors, but still the same.
<?PHP
$delete = $_POST['delete'];
$id = $_POST['id'];
if ($delete = 'delete') {
$db = mysql_connect("localhost", "root");
mysql_select_db("news" , $db) or die("Couldn't open $db: ".mysql_error());
$sql = "DELETE FROM news WHERE id='$id'";

$result = mysql_query($sql);

echo "$sql Record deleted!<p>";
} else {
echo '<a href="../index.php">Click here to continue</a>';
}
?>

Posted: Sun Apr 11, 2004 8:25 pm
by tim

Re: Deleteing Rows

Posted: Sun Apr 11, 2004 8:38 pm
by m3rajk
try this variation
AlbinoJellyfish wrote:I made a deletion script.

Code: Select all

<?PHP
if ($_POST['delete'] == 'delete') { // check note 1 at end

mysql_connect($hostname, $login, $pass) or die('cannot connect to db server'); // check note 2 at end

mysql_select_db($database , $db) or die("Couldn't open $database');  // check note 3 at end

$sql = "DELETE FROM news WHERE id='{$_POST['id']}'";	//  check note 4 at end

mysql_query($sql, $db); // note 5

if(mysql_errno($db)){ // note 6

echo '<h1 class"delerr">Statement "'.$sql.'" returned:<br />'.mysql_error($db).'</h1>'; // note 7

}else{     echo "<p>Record deleted!<p>"; } // note 8

?>
It actually doesnt even delete. It just adds another line of blanks.
note 1: check for deletion directly from post. no need to set a variable unless using it multiple times. also not the == instead of =. the dif is that = sets and == tests equality

note 2: note use of password. this is neded for root to be more than read-only. also, never use root, if there's an error you can screw up soooo much. always use some other user

note 3: this selects the db. it also make it so you dont need to test that you have it unless you want to use specific error messages

note 4: this is where you set up the comand. in a non-read only setting. also note that i have '' around the entry for safety. also note i assume the id is in the post array

note 5: you use $db elsewhere, so use it here. keep one method. the removal of the result is because you dont use it and there's a better way for you to know why something might have gone wrong

note 6: everything is fine is 0, thus false is ok and what you want

note 7: this returns a useable error

note 8: this is the ok message

Posted: Sun Apr 11, 2004 8:50 pm
by d3ad1ysp0rk
Where does $id come from?

Posted: Mon Apr 12, 2004 12:48 pm
by JAM
LiLpunkSkateR wrote:Where does $id come from?
Had the same thought... The id is perhaps malfunctioning?

Code: Select all

<?PHP
 echo $_POST['id']; // debug to see that it's actually there...
exit;
// original code...
if ($_POST['delete'] == 'delete') {
// ...
Alternate:

Code: Select all

if (!empty($_POST['delete']) and !empty($_POST['id'])) {
Hope I made sence...