Jump to content


Photo

Can't get the data to delete from DB


  • Please log in to reply
2 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 26 July 2006 - 11:34 PM

I have this code in a script that parses the data from a form and inserts it into the MySQL database.

echo '<form action="unban.php" method="POST">';
while($row = mysql_fetch_array($result))
{
  	echo "<br><table width='400' border='1' padding='3'><tr><td align='center'>";
    echo '<input type="checkbox" name="banip[]" value="' . $row['banned_ip'] . '" /> &nbsp; &nbsp;';
    echo $row['banned_ip'] . "<br />\n";
    echo "</td></tr></table><br />";
  }
if (mysql_num_rows($result) == 0)
{
 echo "<p><center>There are currently no banned IP addresses in the database.";
}
else
{
echo "<p><center>Here are the current banned IP addresses. Select the checkbox next to any IP you wish to unban and click below<br>";
echo '<br><input type="submit" name="unban" value="UnBan Selected"></form>';
 }

This is the code i'm trying to use to delete the items upon hitting submit IF they are checkmarked:

$ip = $_POST['banip'];
// this will erase the banned IP addresses chosen by the administrator
mysql_connect($dbhost, $dbuser, $dbpass) or die('No database connection!');
mysql_select_db($dbname) or die('Cannot find that darn database!');
// now we run our query and deletion based upon what is checked
foreach($_POST['banip'] as $ipkey => $ip)
{
    $sql = "DELETE FROM banned_ip (`banned_ip`) VALUES ('$ip')";
    mysql_query($sql);
}
// if successful we do this
echo "<center><font face='Verdana' size='2'>IP address $ip has been reinstated.<br />";

The first section of code works just peachy. It displays the IP addresses that i've selected to be banned along with a checkbox next to it. the second section extracts that value and uses it to delete the checkmarked item. It gives me a successful message that the IP address has been reinstated but when I check again the IP address was not deleted. Ideas?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 July 2006 - 11:38 PM

You need to supply your query a WHERE clause. Something like...
$sql = "DELETE FROM banned_ip WHERE ip = '$ip'";


#3 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 27 July 2006 - 12:06 AM

Ok, got it. I modified that line as you laid out but still got no results. That's because I kept that one part ('banned_ip') in there thinking it was necessary. I deleted it and now it works fine. Thanks for the help!

Finished code:

foreach($_POST['banip'] as $ipkey => $ip)
{
    $sql = "DELETE FROM banned_ip WHERE banned_ip = '$ip'";
    mysql_query($sql);
}
// if successful we do this
echo "<center><font face='Verdana' size='2'>IP address $ip has been reinstated.<br />";





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users