0

我正在使用此代码将值存储在数据库中,但是当我选择多个复选框时,只有一个 eid 值存储在数据库中(这应该不是我选择的复选框),并且 rid 值也存储为 0 在哪里错误?这是php代码

<?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();
            }
}


?>

这是复选框的代码

echo "<td><input id=\"select_candi{$i}\" onclick=\"javascript:func(this.id,{$_SESSION['uid']})\" type=\"checkbox\" name=\"check_candi[]\" value=\"{$data_set['eid']}\"/></td>"; 
4

3 回答 3

1

将你的 for 循环拉到插入查询附近,试试这个,

$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);

  }
}
于 2013-01-10T10:50:48.343 回答
1

将sql查询放在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);
  }
}
于 2013-01-10T10:55:21.620 回答
0

您不能将数组添加到数据库

"INSERT INTO selected_candidate (
                            eid,rid
                        ) VALUES (
                             '{$eid}','{$emplyrid}'
                        )"

在此查询中 $eid 将始终包含数组 $checkcandi_sel 的最后一个值,您将数组 $checkcandi_sel 转换为字符串并添加到数据库

或者

通过循环 sql 查询将其添加为多个条目。

于 2013-01-10T10:51:45.900 回答