Page 2 of 3

Re: [How]Delete button

Posted: Mon Jul 20, 2009 9:13 pm
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! :)

Re: [How]Delete button

Posted: Wed Jul 22, 2009 9:34 am
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...

Re: [How]Delete button

Posted: Wed Jul 22, 2009 9:37 am
by jackpf
I think you need to escape them quotes.

Re: [How]Delete button

Posted: Sat Jul 25, 2009 7:56 am
by nitediver

Code: Select all

if ($i !=0) { $del .= ' or id=''.mysql_real_escape_string($_POST['delete']).''';"
Like this?

Re: [How]Delete button

Posted: Sat Jul 25, 2009 8:03 am
by jackpf
Try this:

$del .= " or id='".mysql_real_escape_string($_POST['delete'])."'";

Re: [How]Delete button

Posted: Sun Jul 26, 2009 8:27 am
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'])."'";

Re: [How]Delete button

Posted: Sun Jul 26, 2009 10:08 am
by jackpf
You haven't put a period after the quote.

Re: [How]Delete button

Posted: Thu Aug 06, 2009 10:22 am
by nitediver
unfortunately still error...

Re: [How]Delete button

Posted: Thu Aug 06, 2009 10:26 am
by jackpf
What's the error and your code?

Re: [How]Delete button

Posted: Fri Aug 14, 2009 9:02 am
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)
 

Re: [How]Delete button

Posted: Fri Aug 14, 2009 10:06 am
by jackpf
What's the error?

Re: [How]Delete button

Posted: Sun Aug 16, 2009 9:26 am
by nitediver
Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\php\invoice\do.php on line 4
DELETE FROM invoice WHERE

Re: [How]Delete button

Posted: Sun Aug 16, 2009 10:42 am
by jackpf
isn't $_POST delete your submit button??

Re: [How]Delete button

Posted: Thu Aug 20, 2009 9:07 am
by nitediver
yeah, my button name is delete...

Re: [How]Delete button

Posted: Thu Aug 20, 2009 9:11 am
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.