Insert multiple checkbox values of array into mysql db

Reply

Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Insert multiple checkbox values of array into mysql db

 
0
  #1
May 8th, 2008
  1. <?php
  2. include("conn.php");
  3. include("session.php");
  4.  
  5. ?>
  6. <html>
  7. <head>
  8. <title></title>
  9. </head>
  10.  
  11. <body>
  12. <form action="user.php" method="post">
  13. <table border="0" align="center" width="100%">
  14. <tr>
  15. <td align="center"><a href="editevent.php">Edit Event</a></td>
  16. <td align="center"><a href="addevent.php">Add Event</a></td>
  17. <td align="right"><a href="logout.php">Logout</a></td>
  18. </tr>
  19. <tr>
  20. <td colspan="3"><b>List of events</b></td></tr>
  21. <?php
  22. $str="SELECT * FROM eventdesc ";
  23.  
  24. $res=mysql_query($str);
  25.  
  26. if(mysql_num_rows($res)!=0)
  27. {
  28.  
  29. while($data=mysql_fetch_array($res,MYSQL_ASSOC))
  30. {
  31. echo '<tr> <td>'. $data['eventdesc'].'</td> <td> <input type="checkbox" name="chkbx[]" ></td></tr>';
  32.  
  33. }
  34. }
  35.  
  36. mysql_close();
  37. ?>
  38. <tr><td align="right"><input type="submit" Value="Display"></td></tr>
  39. </table>
  40. </form>
  41. </body>
  42. </html>

Here is my code for user.php

  1. <?php
  2. include("conn.php");
  3. include("session.php");
  4.  
  5.  
  6. $str= foreach($_POST["chkbx"] AS $key => $value){
  7. "INSERT INTO eventdesc(show) VALUES
  8. ('{$_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]}')");
  9. }
  10.  
  11. $res=mysql_query($str);
  12. if($res)
  13. echo 'Success';
  14. else
  15. echo 'Failure';
  16.  
  17. mysql_close($con);
  18. ?>

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?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 19
Reputation: phpuser is an unknown quantity at this point 
Solved Threads: 1
phpuser phpuser is offline Offline
Newbie Poster

Re: Insert multiple checkbox values of array into mysql db

 
0
  #2
Jul 1st, 2008
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 Quick reply to this message  
Join Date: May 2008
Posts: 19
Reputation: phpuser is an unknown quantity at this point 
Solved Threads: 1
phpuser phpuser is offline Offline
Newbie Poster

Re: Insert multiple checkbox values of array into mysql db

 
0
  #3
Feb 27th, 2009
Originally Posted by Suhacini View Post
  1. <?php
  2. include("conn.php");
  3. include("session.php");
  4.  
  5. ?>
  6. <html>
  7. <head>
  8. <title></title>
  9. </head>
  10.  
  11. <body>
  12. <form action="user.php" method="post">
  13. <table border="0" align="center" width="100%">
  14. <tr>
  15. <td align="center"><a href="editevent.php">Edit Event</a></td>
  16. <td align="center"><a href="addevent.php">Add Event</a></td>
  17. <td align="right"><a href="logout.php">Logout</a></td>
  18. </tr>
  19. <tr>
  20. <td colspan="3"><b>List of events</b></td></tr>
  21. <?php
  22. $str="SELECT * FROM eventdesc ";
  23.  
  24. $res=mysql_query($str);
  25.  
  26. if(mysql_num_rows($res)!=0)
  27. {
  28.  
  29. while($data=mysql_fetch_array($res,MYSQL_ASSOC))
  30. {
  31. echo '<tr> <td>'. $data['eventdesc'].'</td> <td> <input type="checkbox" name="chkbx[]" ></td></tr>';
  32.  
  33. }
  34. }
  35.  
  36. mysql_close();
  37. ?>
  38. <tr><td align="right"><input type="submit" Value="Display"></td></tr>
  39. </table>
  40. </form>
  41. </body>
  42. </html>

Here is my code for user.php

  1. <?php
  2. include("conn.php");
  3. include("session.php");
  4.  
  5.  
  6. $str= foreach($_POST["chkbx"] AS $key => $value){
  7. "INSERT INTO eventdesc(show) VALUES
  8. ('{$_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]}')");
  9. }
  10.  
  11. $res=mysql_query($str);
  12. if($res)
  13. echo 'Success';
  14. else
  15. echo 'Failure';
  16.  
  17. mysql_close($con);
  18. ?>

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?
Try with for loop like
  1. for($i=0;$i<count($_POST["chkbx"]);$i++)
  2. {
  3. "INSERT INTO eventdesc(show) VALUES
  4. ('".$_POST["chkbx"][$i]."');
  5.  
  6. $res=mysql_query($str);
  7. if($res)
  8. echo 'Success';
  9. else
  10. echo 'Failure';
  11. }
  12. mysql_close($con);
  13.  


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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 8517 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC