<?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?

I think for multiple check boxes insert, you have to coding like this in your php file

$c=$_REQUEST;

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.

<?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?

Try with for loop like

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.

Will your method append so that all checkbox values are in one field or do you have to have a separate field for each checkbox in your database?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.