User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 361,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,037 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1333 | Replies: 1
Reply
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Insert multiple checkbox values of array into mysql db

  #1  
May 8th, 2008
<?php 
include("conn.php");
include("session.php");

?>
<html>
<head>
<title></title>
</head>

<body>
<form action="user.php" method="post">
<table border="0" align="center" width="100%">
<tr>
<td align="center"><a href="editevent.php">Edit Event</a></td>
<td align="center"><a href="addevent.php">Add Event</a></td>
<td align="right"><a href="logout.php">Logout</a></td>
</tr>
<tr>
<td colspan="3"><b>List of events</b></td></tr>
<?php
$str="SELECT * FROM eventdesc ";

$res=mysql_query($str);

if(mysql_num_rows($res)!=0)
{
 
while($data=mysql_fetch_array($res,MYSQL_ASSOC))
{
 echo '<tr> <td>'.  $data['eventdesc'].'</td> <td> <input type="checkbox" name="chkbx[]" ></td></tr>';

}
}

mysql_close();
?>
<tr><td align="right"><input type="submit" Value="Display"></td></tr>
</table>
</form>
</body>
</html>

Here is my code for user.php

<?php
include("conn.php");
include("session.php");


$str= foreach($_POST["chkbx"] AS $key => $value){
"INSERT INTO eventdesc(show) VALUES
('{$_POST["chkbx"][0]} {$_POST["chkbx"][1]} {$_POST["chkbx"][2]} {$_POST["chkbx"][3]} {$_POST["chkbx"][4]} {$_POST["chkbx"][5]} {$_POST["chkbx"][6]} {$_POST["chkbx"][7]} {$_POST["chkbx"][8]} {$_POST["chkbx"][9]} {$_POST["chkbx"][10]} {$_POST["chkbx"][11]} {$_POST["chkbx"][12]} {$_POST["chkbx"][13]} {$_POST["chkbx"][14]} {$_POST["chkbx"][15]} {$_POST["chkbx"][16]} {$_POST["chkbx"][17]} {$_POST["chkbx"][18]} {$_POST["chkbx"][19]} {$_POST["chkbx"][20]} {$_POST["chkbx"][21]} {$_POST["chkbx"][22]} {$_POST["chkbx"][23]} {$_POST["chkbx"][24]} {$_POST["chkbx"][25]} {$_POST["chkbx"][26]} {$_POST["chkbx"][27]} {$_POST["chkbx"][28]} {$_POST["chkbx"][29]} {$_POST["chkbx"][30]} {$_POST["chkbx"][31]}')");
}

$res=mysql_query($str);
if($res)
 echo 'Success';
else
 echo 'Failure';

mysql_close($con);
?> 

and this the error am getting:
Parse error: syntax error, unexpected T_FOREACH in C:\Program Files\xampp\htdocs\exphp\Events\Myevents\user.php on line 6
what shall i do to slove this?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 11
Reputation: phpuser is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
phpuser phpuser is offline Offline
Newbie Poster

Re: Insert multiple checkbox values of array into mysql db

  #2  
4 Days Ago
I think for multiple check boxes insert, you have to coding like this in your php file

$c=$_REQUEST['chkbx'];

for($i=0;$i<count($c);$i++)
{
$val=$c[$i];
$sql="INSERT INTO eventdesc(show) VALUES($val);
$res=mysql_query($sql);
}

By this coding,you can insert all check boxes values in your DB which are checked.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC