Jump to content

[SOLVED] How to Delete from MySQL Table based on what the user clicks


Recommended Posts

Hey Again,

 

I have a password protected CPanel with a Ban/Unban IP page. This page shows a table of the Banned IP's and The reason. It also has an 'unban' button next to it. That links to a file called 'ubs.php' and then 'ubs.php' uses $_GET to grab the IP Address and DELETE that row of data from the database WHERE IP = the variable containing the IP from the URL.

 

For some reason it doesn't work. Perhaps I could do this without redirecting them to another page???? Even so, with a page? Heres the some of the code:

 

ban/unban page

echo "

<h1>Ban/Unban IP Addresses from Access to the entire Site</h1>

<p>List of Banned IP's

<table width='588' height='51' border='1' cellpadding='8' cellspacing='0'>
  <tr> 
    <th width='174'>Banned IP Address</th>
    <th width='289'>Reason for Ban</th>
    <th width='69'>Unban?</th>
  </tr>";
  
mysql_select_db("runescapez_bans") or die(mysql_error());
$listget = mysql_query("SELECT * FROM bannedips ORDER BY IP ASC");

while($row = mysql_fetch_array($listget))
{
  echo "<td>" . $row['IP'] . "</td>";
  echo "<td>" . $row['Reason'] . "</td>";
  echo "<td>" . "<a href='ubs.php?ip=" . $row['IP'] . "'>Unban</a>" . "</td>";
  echo "</tr>";
  }

echo '</table><br><br>';

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

echo '

<h1>Ban IP</h1><br><br>
<form action="" method="post">
<font size="2" face="Arial, Helvetica, sans-serif">

IP Address to Ban: 
    <input name="IP" type="text" size="20">
    <br>
    <br>
    Reason for Ban: 
    <textarea name="Reason" cols="33" rows="5" wrap="VIRTUAL"></textarea>
    <br>
    <br>
    <input type="submit" name="submit" value="Ban IP">
    <input name="reset" type="reset" id="reset" value="Clear Form">

</font>
</form>
';

} else {
$IP = $_POST['IP'];
$IP = $_POST['Reason'];

mysql_query("INSERT INTO `bannedips` (IP, Reason) VALUES ('$IP', '$Reason')");
echo "IP Address Banned Successfully. If you have made an error, please remove the user from the Table Above.";
}

echo '<br><br>';
echo '<a href=logout.php>Log Out</a>';

 

ubs.php

mysql_select_db("runescapez_bans") or die(mysql_error());

$delip == $_GET["ip"];
mysql_query("DELETE FROM 'bannedips' WHERE 'IP' = '" . $delip . "'");
header("Location: ipbans.php");

 

I've hidden all the other code as it is irrelevant. Also, the table appears wrong; as the IP address is displayed, then a blank column, then the unban link, then on the NEXT row; the Reason appears with another link to unban.... Coudl you fix this too?

 

Thanks in Advance,

Dan.

You can fix the table by echo()'ing a tr while looping over the rows.

 

Anytime you have an interaction with the database that is failing, slap this onto the statement:

  or die(mysql_error());

That will kill the script and give you the mysql reason why.  After all, it's difficult to debug something if you don't have any information, isn't it?

 

mysql_query("DELETE FROM 'bannedips' WHERE 'IP' = '" . $delip . "'") or die(mysql_error());

 

I can tell you now though, that the reason it is dying is your single quotes around the table and column name; that is invalid MySQL syntax.  You place backticks, which is the un-shifted tilde key, around table and column names.

 

In MySQL, single quotes surround data.

hey, The table still shows up as different rows but the same data.

 

Also, there are no errors returned but it doesn't delete the data.

 

$delip == $_GET["ip"];
mysql_query("DELETE FROM `bannedips` WHERE `IP` =" . "'" . $delip . "'") or die(mysql_error());

Change

 

$delip == $_GET["ip"];

mysql_query("DELETE FROM `bannedips` WHERE `IP` =" . "'" . $delip . "'") or die(mysql_error());

 

to

 

$delip = $_GET["ip"];

mysql_query("DELETE FROM `bannedips` WHERE `IP` ='$delip'") or die(mysql_error());

 

<?php

mysql_select_db("runescapez_bans") or die(mysql_error());

if(isset($_GET['ip'])){

$delip=$_GET["ip"];

$d="DELETE FROM bannedips WHERE IP = '$delip' ";
$dr=mysql_query($d)or die(mysql_error());

header("Location: ipbans.php");
exit;
}else{};
?>

Thanks you guys; And oops about the == mistake :S.

 

It all works; my mistake with the table was that it posts both the IP and Reason to the IP field in the Database rather than posting IP to IP and Reason to Reason. :S.

 

Now for a little IP Block test..... :S

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.