[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

[How]Delete button

Post 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>";
?>
Last edited by nitediver on Thu Jul 16, 2009 7:49 am, edited 1 time in total.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Exist in database but unable to display

Post by Reviresco »

Code: Select all

<tr>
    <td>$row[name]</td>
    <td>$row[number]</td>
    <td>$row[date]</td>
  </tr>
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: Exist in database but unable to display

Post 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>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Exist in database but unable to display

Post 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?
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

[How]Delete button

Post 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
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 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
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 »

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?
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post 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.
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 »

Put it wherever you want to delete your records.

Presumably whatever file your form action points to.
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 »

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>';
?>
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 could just put $del = null; at the top, and then it would be set so wouldn't show any errors.
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 »

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. :)
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 »

Yeah. Just less code tbh.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: [How]Delete button

Post by nitediver »

Thanks a lot, I'll try it...
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: [How]Delete button

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 2:57 pm, edited 1 time in total.
Post Reply