Jump to content

Archived

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

simcoweb

Can't get the data to delete from DB

Recommended Posts

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

[code]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>';
}[/code]

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

[code]$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 />";[/code]

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?

Share this post


Link to post
Share on other sites
You need to supply your query a WHERE clause. Something like...
[code]
$sql = "DELETE FROM banned_ip WHERE ip = '$ip'";
[/code]

Share this post


Link to post
Share on other sites
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:

[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 />";[/code]

Share this post


Link to post
Share on other sites

×

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.