What I want to do is very simple, but I'm pretty new at PHP and its a
little hard for me.
I have one page, where there are a rows of checkboxes. A button
selects all checkboxes, and then presses delete.
When delete is pressed, I want to go to a next page and run a sql
command for every single box thats checked. The checkboxes store a
value, and multiple boxes can be checked.
On the second page, whch is the form actin, I simply want to iterate
through and pull which values are checked and run a sql command for
each value.
I have no idea how to do this. I assume you willl have to use a for
each statement, but am having trouble programming this.
Someone please help! 5 4172
"ameshkin" <am**********@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
What I want to do is very simple, but I'm pretty new at PHP and its a
little hard for me.
I have one page, where there are a rows of checkboxes. A button
selects all checkboxes, and then presses delete.
When delete is pressed, I want to go to a next page and run a sql
command for every single box thats checked. The checkboxes store a
value, and multiple boxes can be checked.
On the second page, whch is the form actin, I simply want to iterate
through and pull which values are checked and run a sql command for
each value.
I have no idea how to do this. I assume you willl have to use a for
each statement, but am having trouble programming this.
Someone please help!
Name the checkboxes using an array:
<input type="checkbox" name="delete[]" value="100" /100
<input type="checkbox" name="delete[]" value="200" /200
Then in the form handler iterate thru the array:
foreach($_POST['delete'] as $key =$val){
// SQL magic to delete $val
}
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
It would be better to just execute one query, rather than executing a
query on each iteration. First, form the query from the submitted data,
then execute:
<?php
// sanitize input
foreach ( $_POST['delete'] as $key =$val )
$_POST['delete'] = intval($val);
// init query
$qry = 'DELETE FROM `table` WHERE `id` = ';
$id = implode(' OR `id` = ', $_POST['delete']); // extend string from
submitted ids
$qry .= $id; // concatenate
$sql = mysql_query($qry); // run query
// do what you need with the sql resource
?>
On Dec 11, 12:13 am, "ameshkin" <amir.mesh...@gmail.comwrote:
What I want to do is very simple, but I'm pretty new at PHP and its a
little hard for me.
I have one page, where there are a rows of checkboxes. A button
selects all checkboxes, and then presses delete.
When delete is pressed, I want to go to a next page and run a sql
command for every single box thats checked. The checkboxes store a
value, and multiple boxes can be checked.
On the second page, whch is the form actin, I simply want to iterate
through and pull which values are checked and run a sql command for
each value.
I have no idea how to do this. I assume you willl have to use a for
each statement, but am having trouble programming this.
Someone please help!
$_POST['delete'] = intval($val);
Sorry, the above should actually be:
foreach ( $_POST['delete'] as $key =$val )
$_POST['delete'][$key] = intval($val);
Curtis wrote:
It would be better to just execute one query, rather than executing a
query on each iteration. First, form the query from the submitted data,
then execute:
<?php
// sanitize input
foreach ( $_POST['delete'] as $key =$val )
$_POST['delete'] = intval($val);
// init query
$qry = 'DELETE FROM `table` WHERE `id` = ';
$id = implode(' OR `id` = ', $_POST['delete']); // extend string from
submitted ids
$qry .= $id; // concatenate
$sql = mysql_query($qry); // run query
// do what you need with the sql resource
?>
On Dec 11, 12:13 am, "ameshkin" <amir.mesh...@gmail.comwrote:
>>What I want to do is very simple, but I'm pretty new at PHP and its a little hard for me.
I have one page, where there are a rows of checkboxes. A button selects all checkboxes, and then presses delete.
When delete is pressed, I want to go to a next page and run a sql command for every single box thats checked. The checkboxes store a value, and multiple boxes can be checked.
On the second page, whch is the form actin, I simply want to iterate through and pull which values are checked and run a sql command for each value.
I have no idea how to do this. I assume you willl have to use a for each statement, but am having trouble programming this.
Someone please help!
Two problems -
First of all, even your corrected code can incorrectly delete the record
with id = 0. You need verify the intval actually matches the string.
Assuming the id is numeric, a better test would be
$vals = array();
foreach ( $_POST['delete'] as $key =$val )
if (strval(intval(val)) != val)
$vals[] = val;
Even though you might not have a key of 0 right now, things change. And
this is just the type of bug which will cause dozens of hours of
troubleshooting at a later time.
If id is not numeric, then this is not needed.
Also, much easier:
$qry = 'DELETE FROM table WHERE id IN (' .
implode(',', $_POST['DELETE']) . ')' ;
And if it's a non-numeric
$vals = array();
foreach ( $_POST['delete'] as $key =$val )
if (strval(intval(val)) != val)
$vals[] = "'" . mysql_real_escape_string(val_ . "'";
Of course, the op should also be doing other validation on the incoming
data as necessary.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. js*******@attglobal.net
==================
I'm glad I started participating here, even when I try to help out, I
seem to learn a bit more. :-)
I just had a question about a couple lines:
if (strval(intval(val)) != val)
$vals[] = val;
-- Did you mean to have val without the $ preceding?
Also:
if (strval(intval(val)) != val)
$vals[] = "'" . mysql_real_escape_string(val_ . "'";
-- Same issue with "val". Also, the concatenation in the
mysql_real_escape_string function call doesn't make sense. In addition,
the closing parenthesis is missing.
Curtis
On Dec 12, 5:38 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Curtis wrote:
It would be better to just execute one query, rather than executing a
query on each iteration. First, form the query from the submitted data,
then execute:
<?php
// sanitize input
foreach ( $_POST['delete'] as $key =$val )
$_POST['delete'] = intval($val);
// init query
$qry = 'DELETE FROM `table` WHERE `id` = ';
$id = implode(' OR `id` = ', $_POST['delete']); // extend string from
submitted ids
$qry .= $id; // concatenate
$sql = mysql_query($qry); // run query
// do what you need with the sql resource
?>
On Dec 11, 12:13 am, "ameshkin" <amir.mesh...@gmail.comwrote:
>What I want to do is very simple, but I'm pretty new at PHP and its a little hard for me.
>I have one page, where there are a rows of checkboxes. A button selects all checkboxes, and then presses delete.
>When delete is pressed, I want to go to a next page and run a sql command for every single box thats checked. The checkboxes store a value, and multiple boxes can be checked.
>On the second page, whch is the form actin, I simply want to iterate through and pull which values are checked and run a sql command for each value.
>I have no idea how to do this. I assume you willl have to use a for each statement, but am having trouble programming this.
>Someone please help!Two problems -
First of all, even your corrected code can incorrectly delete the record
with id = 0. You need verify the intval actually matches the string.
Assuming the id is numeric, a better test would be
$vals = array();
foreach ( $_POST['delete'] as $key =$val )
if (strval(intval(val)) != val)
$vals[] = val;
Even though you might not have a key of 0 right now, things change. And
this is just the type of bug which will cause dozens of hours of
troubleshooting at a later time.
If id is not numeric, then this is not needed.
Also, much easier:
$qry = 'DELETE FROM table WHERE id IN (' .
implode(',', $_POST['DELETE']) . ')' ;
And if it's a non-numeric
$vals = array();
foreach ( $_POST['delete'] as $key =$val )
if (strval(intval(val)) != val)
$vals[] = "'" . mysql_real_escape_string(val_ . "'";
Of course, the op should also be doing other validation on the incoming
data as necessary.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Amer Neely |
last post by:
I've got a dynamically built form with checkboxes for each element ( a
list of file names in a directory). I need to grab only those checkboxes
that are checked, so I can then delete those files.
...
|
by: prash.marne |
last post by:
hi ,
i am having a simple form
-----------------------------------------------------------------------
-----------------------------------------------------------------------
--------------...
|
by: =?Utf-8?B?UGV0ZXJX?= |
last post by:
I have a TabControl on a Windows form in which I have various tab pages each
with a DataGridView, the first column of which is a
DataGridViewCheckBoxColumn and subsequent columns being...
|
by: cyberdawg999 |
last post by:
Greetings all in ASP land
I have overcome one obstacle that took me 2 weeks to overcome and I
did it!!!!!
I am so elated!! thank you to all who invested their time and energy towards helping me...
|
by: jodleren |
last post by:
Hi all!
I have a problem - I have a list of checkboxes, with certain data...
the point is, that some of them are set, and the important thing for
mere is to get those values, which have changed...
|
by: ttamilvanan81 |
last post by:
Hai i have using the checkbox in for loop.
I need the urgent help from anyone,
for example in the loop there is having 5 checkbox if i checked 3 of the ckeckboxes and 2 of the checkboxes are...
|
by: tdahsu |
last post by:
I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
..
..
..
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |