Reply to Re: Newbie - Help please with printing $_POST array

Your name:

Reply:


Posted by Kimmo Laine on 05/18/06 15:47

"Dynamo" <Dynamo_member@newsguy.com> wrote in message
news:e4hncr02s63@drn.newsguy.com...
> Many Thanks,
>
> I had already tried that but it didn't work. All I got was an empty array
> (i.e.
> array()). Then I realised that because I hadn't actually "checked" any of
> the
> records on the previous page that of course the array WOULD be empty. So
> now
> having "checked" 3 records on the previos page I get the following result.
>
> Array
> (
> [1] => ON
> [61] => ON
> [62] => ON
> )
>
> So now I want to use the information stored in the $_POST array to delete
> records from mysql table. In the above example I would want the records
> where id
> = 1,61 and 62 to be deleted from the table named Catalogue. How would I
> incorporate the information stored in S_POST to complete the code below.
>
> $query1="DELETE FROM Catalogue WHERE id = ?";
> mysql_query($query1) or die("Failed Query of " . $query1);



$delete = '( id = ' . implode(' OR id = ', array_keys($_POST)) . ' ) ';

// implode takes all elements of the array and joins them with the given
string, so that "1,2,3" becomes " 1 OR id = 2 OR id = 3 " and then we add a
little something in both ends and it's good to go.

$query1="DELETE FROM Catalogue WHERE $delete";
mysql_query($query1) or die("Failed Query of " . $query1);

But may I just say a few things about your form. The html 4.01 specification
does not allow using numbers as names of input elements, so having input
like
<input type='checkbox' name='62' value='ON'> is wrong since 62 is a number.
It should at least start with an underscore or an alphabet. Next thing is
that you now have to use the keys of the $_POST array as values. That might
lead into trouble too. I suggest you start using something more like this:

<input type='checkbox' name='delete[]' value='<?php echo $row[0];?>'>

When you use a name like delete[], php creates an array of these elements
under $_POST['delete']. You gotta keep 'em separated from other variables
that come from the form and this is how you do it. Now your form has no
other elements, but in future you might end up doing something like that. So
it's good if you start doing things the right way now. ;)

Now your query would look like this:
$delete = '( id = ' . implode(' OR id = ', $_POST['delete'] ) . ' ) ';

All input elements should have a proper name, therefore your submit should
also be named, <input type="submit" name="submit" value="Test">

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация