[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

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 »

Implode works too though I've never thought about using it except when other members show examples of it in use. Good stuff! :)
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

I change my database a bit, I give it new field for "id", in case for the primary key(is it right?).
My latest database...
Image

Display page

Code: Select all

<?
error_reporting(E_ALL);
echo ("<form action=\"do.php\" method=\"post\" name=\"delete\">");
echo ("
<table width=\"50%\">
  <tr>
    <td width=\"5%\"></td>
    <td width=\"20%\">Name</td>
    <td width=\"10%\">Number</td>
    <td width=\"20%\">Date</td>
  </tr>
</table>
<br>
");
echo "<table width=\"50%\">";
require ("conn.php");
$query="select * from invoice order by date DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo ("
 
  <tr>
    <td width=\"5%\"><input type=\"checkbox\"></td>
    <td width=\"20%\">$row[name]</td>
    <td width=\"10%\">$row[number]</td>
    <td width=\"20%\">$row[date]</td>
  </tr>
  ");
}
echo "</table>";
echo "<input type=\"submit\" name=\"delete\" value=\"Delete\">";
echo ("</form>");
?>
Submit page...

Code: Select all

<?php
include "conn.php";
 
$i = 0;
foreach($_POST['delete'] as $key => $value)
{
 if ($i !=0) { $del .= ' or id=''.mysql_real_escape_string($_POST['delete']).''';
 else {$del = 'id=''mysql_real_escape_string($_POST['delete']).'' ';}
$i++;
}
 
$query = "DELETE FROM chat WHERE ".$del;
 
echo '<div>'.$query.'</div>';
 
?>
 
It display error,
Error = Parse error: syntax error, unexpected '<' in E:\xampp\htdocs\php\invoice\do.php on line 4
here...

Code: Select all

if ($i !=0) { $del .= ' or id=''.mysql_real_escape_string($_POST['delete']).''';
Which part is wrong, is there any variable that doesn't match with my config...

Honestly I dont understand, and im stuck...
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 think you need to escape them quotes.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

Code: Select all

if ($i !=0) { $del .= ' or id=''.mysql_real_escape_string($_POST['delete']).''';"
Like this?
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 »

Try this:

$del .= " or id='".mysql_real_escape_string($_POST['delete'])."'";
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

Another error...

Code: Select all

<?php
$i = 0;
foreach($_POST['delete'] as $key => $value)
{
if ($i !=0) 
{ 
$del .= " or id='".mysql_real_escape_string($_POST['delete'])."'";
}else{
$del .= "id='"mysql_real_escape_string($_POST['delete'])."'";
}
$i++;
}
 
$query = "DELETE FROM invoice WHERE ".$del;
 
echo '<div>'.$query.'</div>';
?>
Now error goes to this line...

Code: Select all

$del .= "id='"mysql_real_escape_string($_POST['delete'])."'";
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 »

You haven't put a period after the quote.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

unfortunately still error...
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 »

What's the error and your code?
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

Code: Select all

  1. <?php
   2. $i = 0;
   3. foreach($_POST['delete'] as $key => $value)
   4. {
   5. if ($i !=0)
   6. {
   7. $del .= " or id='".mysql_real_escape_string($_POST['delete'])."'";
   8. }else{
   9. $del .= "id='".mysql_real_escape_string($_POST['delete'])."'";
  10. }
  11. $i++;
  12. }
  13.  
  14. $query = "DELETE FROM invoice WHERE ".$del;
  15.  
  16. echo '<div>'.$query.'</div>';
  17. ?>
now error goes to this line..

Code: Select all

 
foreach($_POST['delete'] as $key => $value)
 
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 »

What's the error?
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\php\invoice\do.php on line 4
DELETE FROM invoice WHERE
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 »

isn't $_POST delete your submit button??
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

yeah, my button name is delete...
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 »

Well that's not an array. Why are you trying to loop through your submit button?

If you're just deleting one record, then you don't need a loop. Otherwise you need to use something like checkboxes.
Post Reply