Jump to content

Some quick help. Dealing with SQL/Inputs


Demonic

Recommended Posts

	$erm = mysql_query("SELECT * FROM logs");
	$ID = mysql_num_rows($erm);
	if(!isset($_POST['s'])){
			echo ("<form method='post'>");
	while($ermz = mysql_fetch_array($erm)){
		echo ("<input type='checkbox' name='$ermz[id]' />$ermz[dee]<br />");
	}
	echo ("<input type='submit' name='s' value='delete' /></form>");
	}else{
		for($i=1;$i<=$ID;$i++){
			if(isset($_POST[$i])){
				mysql_query("DELETE FROM logs WHERE id = '$i' ") or die("ERROR");
				echo ("done");
			}
		}
	}

 

Alright when i have 5 rows in the DB and I use the script above it deletes 4 inputs and keeps the last one.  Is there something im doing wrong where It doesn't delete all inputs.

Link to comment
https://forums.phpfreaks.com/topic/37251-some-quick-help-dealing-with-sqlinputs/
Share on other sites

I'm not sure what you mean but give this a try...

 

<?php
$erm = mysql_query("SELECT * FROM logs");
if(!isset($_POST['s'])){
echo ("<form method='post'>");
while($ermz = mysql_fetch_array($erm)){
	echo ("<input type='checkbox' name='delete[]' value='$ermz[id]' />$ermz[dee]<br />");
}
echo ("<input type='submit' name='s' value='delete' /></form>");
}else{
for($i = 0; $i < count($_POST['delete']); ++$i) {
	mysql_query("DELETE FROM logs WHERE id = '{$_POST['delete'][$i]}' ") or die("ERROR");
}
echo ("done");
}
?>

 

btw.. PLEASE PLEASE PLEASE PLEASE INVEST IN VARIABLE NAMES THAT MEAN SOMETHING, I didn't correct it for you, but please, for the sake of everyone's sanity... thanks ;)

 

Also this script is vulnerable to sql injection, someone could create their own form and put in a malicious value for one of the delete entries, you might wanna look into that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.