Page 1 of 3

[How]Delete button

Posted: Wed Jul 15, 2009 9:12 am
by nitediver
Anyone could help me out with this...
im stuck i dont know which part is wrong...
==============================================
I have input my data, and it's already inside the database
Image

this is my page
still doesnt display the data...
Image

source code...

Code: Select all

<?
echo "<table width=\"90%\">";
require ("conn.php");
$query="select * from invoice order by date DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo ("
  <tr>
    <td>Name</td>
    <td>Number</td>
    <td>Date</td>
  </tr>
  <tr>
    <td>$result[nama]</td>
    <td>$result[nomor]</td>
    <td>$result[date]</td>
  </tr>
  ");
}
echo "</table>";
?>

Re: Exist in database but unable to display

Posted: Wed Jul 15, 2009 9:20 am
by Reviresco

Code: Select all

<tr>
    <td>$row[name]</td>
    <td>$row[number]</td>
    <td>$row[date]</td>
  </tr>

Re: Exist in database but unable to display

Posted: Wed Jul 15, 2009 5:50 pm
by Skara
That was helpful. I believe what he meant to say was

Code: Select all

<tr>
    <td>{$row['name']}</td>
    <td>{$row['number']}</td>
    <td>{$row['date']}</td>
  </tr>

Re: Exist in database but unable to display

Posted: Wed Jul 15, 2009 8:13 pm
by JAB Creations
Kudos to nitediver for an excellent post...not just code but screenshots as well.

Did you get it to work with the suggestions prior to my/this post?

[How]Delete button

Posted: Thu Jul 16, 2009 7:49 am
by nitediver
That was work, thanks all...
Now I have another problem, below is the page for displaying data from database
My goal is to make that delete button to work(like in mail inbox), could anyone give me example or advice, like where should i start?

Image

Re: [How]Delete button

Posted: Thu Jul 16, 2009 8:08 am
by jackpf
You have to give every checkbox a name postfixed with two square brackets "[]" (no quotes). That tells php to put the posted data into an array.

So then, once it's submitted, say you called the checkboxes delete[], with a value of the record's ID, you could do something like this.

Code: Select all

 
foreach($_POST['delete'] as $key => $value)
{
mysql_query("DELETE FROM table WHERE ID='{$value}';");
}
 
That's a basic example, and obviously needs securing, tailoring etc. But it should give you the basic idea. Post back if you need more help. And yeah I agree with JAB Creations; very pretty posts :P

Re: [How]Delete button

Posted: Thu Jul 16, 2009 6:33 pm
by JAB Creations
jackpf is half correct but I have a couple issues with his post...

...but if you want to do it correctly you'll want to construct a single DELETE query instead of creating a loop. The fewer MySQL queries the better. :wink:

...also be sure to escape all client data! $_COOKIE, $_POST, $_GET, $_SESSION, $_FILE....etc, anything that travels to the land of MySQL must first be purified! Let the ritual begin! :twisted:

What you should do is take Jack's example but create a string that will become part of the $query that you'll execute.

In example I would load up phpMyAdmin, delete several entries, and make note of how that single query is constructed. You know how to use concatenation I hope?

Re: [How]Delete button

Posted: Sun Jul 19, 2009 9:07 am
by nitediver
jackpf wrote:You have to give every checkbox a name postfixed with two square brackets "[]" (no quotes). That tells php to put the posted data into an array.

So then, once it's submitted, say you called the checkboxes delete[], with a value of the record's ID, you could do something like this.

Code: Select all

 
foreach($_POST['delete'] as $key => $value)
{
mysql_query("DELETE FROM table WHERE ID='{$value}';");
}
 
That's a basic example, and obviously needs securing, tailoring etc. But it should give you the basic idea. Post back if you need more help. And yeah I agree with JAB Creations; very pretty posts :P
Sorry but I still confuse, where to put that code...
Below my code

Code: Select all

<?
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=\"del\" value=\"Delete\">";
?>
 
@JAB Creations
Sorry, I dont know about concatenation(im new in php), but I'll try to find out.

Re: [How]Delete button

Posted: Sun Jul 19, 2009 9:57 am
by jackpf
Put it wherever you want to delete your records.

Presumably whatever file your form action points to.

Re: [How]Delete button

Posted: Sun Jul 19, 2009 7:31 pm
by JAB Creations
Concatenation...

Code: Select all

<?php
$e1 = '123';
$e4 = '456';
 
$ez = $e1.$e4;
 
echo $ez; //outputs '123456
?>
As far as using DELETE in MySQL like almost anything else if you can do it in a single query you'll want to construct your MySQL query and then execute it...

Code: Select all

DELETE FROM chat WHERE id='19' OR id='20' OR id='21' OR id='22' OR id='23'
So Jack was close with the for loop however what you want to do instead is concatenate the 'or id='' bit.

You can also build a variable over time. Keep in mind I'm pretty sure if the variable isn't yet set you'll have to do a regular assignment (=) however if it exists you can use the concatenation assignment (not sure if that's what it's called but it looks like .= which means "add to the existing variable's string instead of = which would replace any string in the variable if it exists).

I haven't checked this though if it's broken I'm subscribed so let me know...

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 chat WHERE ".$del;
 
echo '<div>'.$query.'</div>';
?>

Re: [How]Delete button

Posted: Mon Jul 20, 2009 3:36 am
by jackpf
You could just put $del = null; at the top, and then it would be set so wouldn't show any errors.

Re: [How]Delete button

Posted: Mon Jul 20, 2009 6:36 am
by JAB Creations
True...I'm pretty sure in the semi-recent past I did something like $del = '';. I think logic inside the foreach helps reenforce things a bit though you're right I think, better to define it before the loop. :)

Re: [How]Delete button

Posted: Mon Jul 20, 2009 6:40 am
by jackpf
Yeah. Just less code tbh.

Re: [How]Delete button

Posted: Mon Jul 20, 2009 8:34 am
by nitediver
Thanks a lot, I'll try it...

Re: [How]Delete button

Posted: Mon Jul 20, 2009 4:00 pm
by McInfo
Here is my suggestion for doing proper validation and building the query with little code. The validation assumes that an id can contain only digits (0-9).

Code: Select all

<?php
header('Content-Type: text/plain');
 
// Loads some example good and bad POST data
$_POST['delete'][] = '0';
$_POST['delete'][] = '32';
$_POST['delete'][] = 'bad';
$_POST['delete'][] = '2_bad';
$_POST['delete'][] = '43';
$_POST['delete'][] = '';
$_POST['delete'][] = '58';
 
if (isset($_POST['delete']) && is_array($_POST['delete'])) {
   
    print_r($_POST);
    // Array
    // (
    //     [delete] => Array
    //     (
    //         [0] => 0
    //         [1] => 32
    //         [2] => bad
    //         [3] => 2_bad
    //         [4] => 43
    //         [5] =>
    //         [6] => 58
    //     )
    // )
   
    // Returns an array of elements that match the regular expression
    //    \A = Beginning of the string
    //     [ = Start of character class definition
    //   0-9 = Character range starting with 0 and ending with 9
    //     ] = End of character class definition
    //     + = One or more (applies to the character class)
    //    \z = End of the string
    $to_delete = preg_grep('/\A[0-9]+\z/', $_POST['delete']);
   
    // Removes the temptation to access dirty data
    unset($_POST['delete']);
   
    print_r($to_delete);
    // Array
    // (
    //     [0] => 0
    //     [1] => 32
    //     [4] => 43
    //     [6] => 58
    // )
   
    // Checks that there are some valid ids
    if (count($to_delete) > 0) {
       
        $query = 'DELETE FROM `table` WHERE `id` IN (' . implode(', ', $to_delete) . ')';
       
        echo $query; // DELETE FROM `table` WHERE `id` IN (0, 32, 43, 58)
       
    }
}
?>
Edit: This post was recovered from search engine cache.