Jump to content

PHP array help


AJLX

Recommended Posts

Hello All,

Hopefully this should be an easy one. I have the following select box:

 

echo '<td><input name="checkbox[]" type="checkbox"  id="checkbox[]" value='.$row['ID']."></td>";

 

I get these variables on a separate page and try and run them through a loop so that I can delete all of the select items. It is this bit that I'm struggling with at the moment. Here is what I have so far:

 

<?php
session_start();
include("inc/session.inc.php");
include("inc/conf2.inc.php");

    foreach($_POST['checkbox'] as $value);
     $sql_query = mysql_query("DELETE FROM messages WHERE ID = '$value'");
?>

 

At the moment this doesn't appear to be working. Does anyone have any ideas?

 

Regards

 

AJLX

Link to comment
https://forums.phpfreaks.com/topic/228650-php-array-help/
Share on other sites

this might help

 


HTML:

<input type="checkbox" name="stuff" value="a" checked="checked" />
<input type="checkbox" name="stuff" value="b" checked="checked" />
<input type="checkbox" name="stuff" value="c" checked="checked" />



PHP:

foreach($_GET['stuff'] as $val)
echo 'val='.$val;



RESULT:

val=a
val=b
val=c

 

Link to comment
https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1178932
Share on other sites

Try this and see what it says:

 

<?php
session_start();
include("inc/session.inc.php");
include("inc/conf2.inc.php");

print_r($_POST); echo("\n\n<br />");

    foreach($_POST['checkbox'] as $value){
       $sql_query = "DELETE FROM messages WHERE ID = '$value'";
       $sql_result = mysql_query($sql_query);
    }
?>

 

use "view page source" and copy/paste the results, maybe you will find the answer yourself ;)

Link to comment
https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1178945
Share on other sites

Hello guys,

 

Using ChemicalBliss's code I get the following returned:

Array ( )

Warning: Invalid argument supplied for foreach() in /home/ajlxcou1/public_html/ProjectA/delete_message.php on line 8

 

My guess would be that the array is empty. Which then means that the foreach statement has nothing to run, and therefor throws me back an error? Is this correct? I'm not asking you guys to write this for me, but a prod in the right direction is useful!

 

Regards,

 

AJLX

 

Link to comment
https://forums.phpfreaks.com/topic/228650-php-array-help/#findComment-1179265
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.