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
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
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
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
Share on other sites

Perfect I had a couple of html issues.

1) I had post[] instead of post. In my mind it made sense to be posting an array.

2) I had $rows[iD] instead of $row[iD]

 

All working now. Special thanks to ChemicalBliss.

 

Regards,

 

AJLX

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.