By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

delete two values...

P: 47
hello sir... i had a problem... how can i delete two values?? before this i delete in one value only and it's work.. but how can i delete if i had two value...?? this is my coding for one value...

[PHP]if(isset($_POST['del_id']) && sizeof($_POST['del_id'])>0){
for($i=0;$i<count($_POST['del_id']);$i++) {
$del = mysql_query("DELETE FROM `stud_sub` WHERE `matric_no`='".$_POST['del_id'][$i]."'");//
}
}[/PHP]

[HTML] <input name="del_id[]" type="checkbox" value="<?php echo $row_reg['matric_no']; ?>" />[/HTML]
Oct 28 '08 #1
Share this Question
Share on Google+
4 Replies


dlite922
Expert 100+
P: 1,584
I don't see anything in your logic. You might have an error in your HTML. Do a view source and see if all the check boxes don't have the same ID.

On another note, I've optimized your PHP loop for efficiency and performance and included SQL Injection preventions.

[PHP]
$idArr = isset($_POST['del_id']) ? $_POST['del_id'] : "";

for( $i=0; $i < isset($idArr[$i]); $i++ )
{
$cleanID = mysql_real_escape_string($idArr[$i]); // this prevents SQL Injection
if(!mysql_query("DELETE FROM `stud_sub` WHERE `matric_no`='" . $cleanID . "'"))
{
die("Error Occured In SQL: " . mysql_error());
}
else
{
die("ID's Deleted!");
}
}
[/PHP]

Let me know if any questions,





Dan
Oct 28 '08 #2

zabsmarty
P: 25
or you will use for each loop when getting value in array

Expand|Select|Wrap|Line Numbers
  1. foreach ($_POST['del_id'] as $chk)
  2. {
  3. $sqldelall = "delete from std_sub  where matric_no =".$chk;
  4. mysql_query ($sqldelall, $db_link) or die (mysql_error() .FETCH_DATA );
  5. }
Oct 28 '08 #3

Markus
Expert 5K+
P: 6,050
or you will use for each loop when getting value in array

Expand|Select|Wrap|Line Numbers
  1. foreach ($_POST['del_id'] as $chk)
  2. {
  3. $sqldelall = "delete from std_sub  where matric_no =".$chk;
  4. mysql_query ($sqldelall, $db_link) or die (mysql_error() .FETCH_DATA );
  5. }
Use code tags when posting, Zab.
Oct 28 '08 #4

Atli
Expert 5K+
P: 5,058
or you will use for each loop when getting value in array

Expand|Select|Wrap|Line Numbers
  1. foreach ($_POST['del_id'] as $chk)
  2. {
  3. $sqldelall = "delete from std_sub  where matric_no =".$chk;
  4. mysql_query ($sqldelall, $db_link) or die (mysql_error() .FETCH_DATA );
  5. }
Now what if I were to post this as the "del_id"?
Expand|Select|Wrap|Line Numbers
  1. 1 OR matric_no != 1;
You would end up with an awfully empty table...

My point being:
NEVER use any user input in a SQL query without validating it first. Especially not when you are dealing with DELETE or UPDATE queries.

If you are expecting a number, make sure you are getting a number.
If you are expecting a string, run it through mysql_real_escape_string first.

See this article if you need some more convincing.
Oct 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.