| | |
Insert multiple checkbox values of array into mysql db
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2008
Posts: 90
Reputation:
Solved Threads: 0
PHP Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?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?
•
•
Join Date: May 2008
Posts: 19
Reputation:
Solved Threads: 1
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.
$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.
•
•
Join Date: May 2008
Posts: 19
Reputation:
Solved Threads: 1
•
•
•
•
PHP Syntax (Toggle Plain Text)
<?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 Syntax (Toggle Plain Text)
<?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?
php Syntax (Toggle Plain Text)
for($i=0;$i<count($_POST["chkbx"]);$i++) { "INSERT INTO eventdesc(show) VALUES ('".$_POST["chkbx"][$i]."'); $res=mysql_query($str); if($res) echo 'Success'; else echo 'Failure'; } mysql_close($con);
in this way it will work.
Last edited by peter_budo; Feb 27th, 2009 at 10:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
![]() |
Similar Threads
- Variable passed to each() is not an array (PHP)
- Storing dynamic form values in Arrays for display & insert (PHP)
Other Threads in the PHP Forum
- Previous Thread: Payment gateways?( urgent)
- Next Thread: Result without while loop
Views: 8517 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl customizableitems database date development directory display download dynamic echo email error file files folder form forms forum function functions google headmethod href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail malfunctioning menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube





