Reputation: 129
I am using this code for storing values in database ,but when i select multiple check-box only one eid value is getting stored in database ( which should be no of checkbox i have selected) and also rid value is stored as 0 where is the mistake? this is php code
<?php //insert selected candidate in table
//echo "checkcandi".$_POST['check_candi'];
if(isset($_POST['select_candidate']) && logged_in())
{
$checkcandi_sel = $_POST['check_candi'];
if(empty($checkcandi_sel))
{
echo("You didn't select any Candidates.");
}
else
{
$N = count($checkcandi_sel);
echo("You selected $N Candidate(s): ");
for($i=0; $i < $N; $i++)
{
echo($checkcandi_sel[$i] . " ");
$eid=$checkcandi_sel[$i];
echo "candi id".$eid;
}
}
//$rid=$value[1];
if(logged_in())
{
$emplyrid=$_SESSION['uid'];
echo "emplyrid".$emplyrid;
}
//$rid=$_GET['rid'];
//var_dump($emplyrid);
$query = "INSERT INTO selected_candidate (
eid,rid
) VALUES (
'{$eid}','{$emplyrid}'
)";
$result = mysql_query($query, $connection);
if ($result) {
$message = "<p style=\"color:green\">The Candidate selected and saved to your my seleted.</p>";
} else {
$message = "Sorry job was not applied.";
$message .= "<br />" . mysql_error();
}
}
?>
this is code for check-box
echo "<td><input id=\"select_candi{$i}\" onclick=\"javascript:func(this.id,{$_SESSION['uid']})\" type=\"checkbox\" name=\"check_candi[]\" value=\"{$data_set['eid']}\"/></td>";
Upvotes: 0
Views: 146
Reputation: 32740
You can not add array to database
"INSERT INTO selected_candidate (
eid,rid
) VALUES (
'{$eid}','{$emplyrid}'
)"
In this query $eid will always contain last value of the array $checkcandi_sel, you convert the array $checkcandi_sel to string and add to data base
OR
Add it as multiple entries by looping the sql query.
Upvotes: 0
Reputation: 4136
Put the sql query inside the foreach...
if(count($checkcandi_sel) > 0) {
for($i=0; $i < count($checkcandi_sel); $i++) {
$eid=$checkcandi_sel[$i];
$query = "INSERT INTO selected_candidate ( eid,rid) VALUES ('{$eid}','{$emplyrid}' )";
$result = mysql_query($query, $connection);
}
}
Upvotes: 1
Reputation: 5108
Pull your for loop near insert query and try this,
$N = count($checkcandi_sel);
if($N > 0) {
for($i=0; $i < $N; $i++)
{
$eid=$checkcandi_sel[$i];
$query = "INSERT INTO selected_candidate (
eid,rid
) VALUES (
'{$eid}','{$emplyrid}'
)";
$result = mysql_query($query, $connection);
}
}
Upvotes: 1