[How]Delete button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

so what the "excution" trigger to delete the record?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [How]Delete button

Post by jackpf »

I don't understand.

If you're trying to delete multiple records, google "php multiple checkbox" or something similar.

Otherwise, you need to explain a bit better...
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

you said that i dont need button...

so how to delete my record,
i mean how to tell php to delete already checked record?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [How]Delete button

Post by jackpf »

No, I said you don't need the foreach loop.

Look, to delete one record:

Code: Select all

$id = (int) mysql_real_escape_string($_POST['delete']);
 
mysql_query("DELETE FROM `yourtable` WHERE `ID`='$id';") or die(mysql_error());
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: [How]Delete button

Post by JAB Creations »

I recommend two inputs, one with a min and the other with a max value of the item's value. So if they are messages 20-30 set min to 20 and max to 30.

Code: Select all

<input name="delete" type="checkbox" value="1" /><input name="delete" type="checkbox" value="2" /><input name="delete" type="checkbox" value="3" /><input name="delete" type="checkbox" value="4" />
The name=delete in the input is going to create an array accessible in PHP as $_POST['delete']. Change the input name (on EVERY delete checkbox) to something else (like bob) and delete becomes bob ($_POST['bob']).

Then you should be able to do a quick foreach loop and determine if the value is set. You know by the post min and max what the range is. That's just my quick and dirty quick conceptual suggestion. I'm sure there is a better way.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [How]Delete button

Post by jackpf »

Hey Jab, I think you need square brackets on the end of the names ;)

But I'm still not sure what this guys wants to do. Are you deleting one or multiple records? Your code seems half and half...and it doesn't work.

I think you need to check out some tutorials imo.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

I try to doing search like jack told me,
and i found, but i've changes few codes...

Now it's work :)

Code: Select all

<?
require("conn.php");
$do = "select * from invoice";
$qwery = mysql_query($do);
?>
<html><body>
<style type="text/css">
#list {border: #000000 1px solid; border-top:none;}
#head {border: #000000 1px solid;}
</style>
 
<form action="dialit.php" method="POST">
 
<?php
print ("
<table width=\"80%\" id=\"head\">
  <tr>
    <td width=\"5%\"></td>
    <td width=\"20%\">Name</td>
    <td width=\"20%\">Number</td>
    <td width=\"20%\">Date</td>
  </tr>
</table>
");
while ($result = mysql_fetch_array($qwery))
{
print ("
<table width=\"80%\" id=\"list\">
  <tr>
    <td width=\"5%\"><input type=checkbox name=del[] id=del value=$result[id]></td>
    <td width=\"20%\">$result[name]</td>
    <td width=\"20%\">$result[number]</td>
    <td width=\"20%\">$result[date]</td>
  </tr>
</table>
");
}
 
?>
<input type="submit" name="delete" value="Delete" id="delete">
<?php
$id = $_POST[del];
$multi = count($id);
 
if($_POST['delete'])
{
 
for ($i=0; $i<$multi; $i++)
{
 
$dodel = "delete from invoice where id=$id[$i]";
$qwerydel = mysql_query($dodel);
 
}
 
if ($qwerydel)
{
print "Record Deleted<br>";
echo "<meta http-equiv=\"refresh\" content=\"0;URL=dialit.php\">";
}
 
}
?>
</form>
</body></html>
But when i try to revert > PHP inside HTML
it doesnt work why?
like this...

Code: Select all

<table width=\"80%\" id=\"list\">
  <tr>
    <td width=5%><input type=checkbox name=del[] id=del value=$result[id]></td>
    <td width=20%><? print "$result[name]";</td>
..............
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [How]Delete button

Post by jackpf »

Have you turned on error reporting? Looks like you're embedding PHP tags...
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

sorry jack ive been away so long,

now im going to make this "check all" work...
could u help me...
should it be better if use pagination?
Image
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [How]Delete button

Post by jackpf »

Lol wb.
What's not working?

And hmm...that depends on how many items you're likely to have. If it's more than about 25 then yeah...I'd personally paginate it.
Post Reply