inserting records when multiple check box is selected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpfan
Forum Newbie
Posts: 14
Joined: Thu Aug 27, 2009 4:37 am

inserting records when multiple check box is selected

Post by phpfan »

Hi,

i have bug when inserting records into mysql when multiple check boxes are selected

Code: Select all

 
<?php
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="checkbox" name="id[]" value="1" />Test<br />
<input type="checkbox" name="id[]" value="2" />Business<br />
<textarea name="txt" rows="10" cols="30"></textarea> 
<select name="ddl" id="" class="text">
    <option value="select">select</option>
    <option value="TM-TEST">TM-TEST</option>
</select>
<input type="submit" value="submit" name="submit">
</form>
<?php
} else {
 
mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("smsdb");
$media_array = $_POST['media'];
foreach ($media_array as $one_media) {
$source .= $one_media.", ";
}
$media = substr($source, 0, -2);
 
$query = "INSERT INTO group_sms(group, message, sender_id)
VALUES( '$media','$_POST[txt]','$_POST[ddl]')";
$result = mysql_query($query);
}
?>
</body>
</html>
i have created a master table groups with 2 columns group_id & group_name to this groups i have created a sub_groups table where group_id is the foreign key and 2 more columns sub_group_id & sub_group_name when i select the group check box test the sub_group table has the records where the group_id refers, and remaining values should be inserted but im getting the error as
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\new.php on line 27
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: inserting records when multiple check box is selected

Post by jackpf »

I don't see any input called "media".
phpfan
Forum Newbie
Posts: 14
Joined: Thu Aug 27, 2009 4:37 am

Re: inserting records when multiple check box is selected

Post by phpfan »

i had changed it to " id ",
i got the solution the bug is that i have a column named group so in mysql "group" is a keyword so i changed it to "ngroup" now i have the result, anyways thanks for the support mate
Post Reply