无法通过引用传递参数 - MySQLi

时间:2014-02-17 16:35:42

标签: php html mysql mysqli insert

我的插入查询有问题。我试图从会话变量中获取用户ID,并将其与我通过表单输入的其他变量一起插入表中。

我已经尝试打印$ userid变量,它显示为1,这是正确的。 bind_param语句似乎不接受它。

我一直收到此错误

Cannot pass parameter 5 by reference in /*** on line 29

第29行是$ stmt-> bind_param行。

php代码:

<?php
sec_session_start();

if (login_check($mysqli) == true) : 

$table = "ticket";
$con = connect($table);


if(isset($_POST['submit'])){

     $stmt = $con->prepare('INSERT INTO `ticket` (`subject`, `description`, `assigned`, `status`, `user_id`, `priority_id`, `employee_id`) VALUES (?, ?, ?, ?, ?, ?, ?)');

        if (!$stmt) {
            throw new Exception($con->error, $con->errno);
        }

        $userid = $_SESSION['id'];

        $stmt->bind_param('sssssss', $_POST['post_subject'], $_POST['post_description'], $_POST['post_assigned'], 'Open', $userid, $_POST['post_priority'], $_POST['post_employee']);

        if (!$stmt->execute()) {
            throw new Exception($stmt->error, $stmt->errno);
        }

        mysqli_close($con);
    }
    else{
?>

这是表格:

<?php 
$sql = "SELECT * FROM priority"; 
$result = mysqli_query($con, $sql) or die (mysql_error()); 
$priority_id='';
while ( $row = mysqli_fetch_array($result)){
    $id=$row["id"];
    $priority=$row["priority"]; 
    $priority_id.="<OPTION VALUE=\"$id\">".$priority;
} 

$sql = "SELECT * FROM members"; 
$result = mysqli_query($con, $sql) or die (mysql_error()); 
$assigned_id='';
while ( $row = mysqli_fetch_array($result)){
    $id=$row["id"];
    $name=$row["name"]; 
    $assigned_id.="<OPTION VALUE=\"$id\">".$name;
}

?>

<div id="ticketSubmit"> 

    <form action="<?php $_PHP_SELF ?>" method="post">
      <fieldset>
        <legend>Post content</legend>
        <div>
          <label for="post_subject">
            <strong>Choose a subject</strong> for the post
          </label>
          <input id="post_subject" name="post[title]" type="text">
        </div>
        <div>
          <label for="post_description">
            <strong>Supply actual content</strong> for the post
          </label>
          <textarea id="post_description" name="post[description]"></textarea>
        </div>


      </fieldset>
      <fieldset>
        <legend>Post metadata</legend>
        <div class="inline"> 
          <label for="post_assigned">
            <strong>Choose who assigned</strong> the post
          </label>
            <select id="post_assigned" name="post[assigned]">
              <option> <? echo $assigned_id ?> </option>
            </select>

          <label for="post_category">
            <strong><span style="margin-left:28px">Choose which group</strong> the post is for
          </label>
            <input id="post_category" name="post[category]" type="text">

          <label for="post_priority">
            <strong><span style="margin-left:28px">Choose priority</strong> for the post
          </label>
          <select id="post_priority" name="post[priority]">
            <option> <? echo $priority_id ?> </option>
          </select>

        </div>

      </fieldset>
      <fieldset>
        <legend>Post privacy</legend>
        <div class="inline">
          <input id="post_allow_comments" name="post[allow_comments]" type="checkbox">
          <label for="post_allow_comments">
            <strong>Allow comments</strong> on the post
          </label>
        </div>
        <div class="inline">
          <input id="post_private" name="post[private]" type="checkbox">
          <label for="post_private">
            <strong>Make private</strong> so that only friends see it
          </label>
        </div>
      </fieldset>
      <p>
        <input name = "submit" type="submit" id="submit" value="Submit Ticket">
        or
        <a href="../index.php">cancel and go back</a>
      </p>
    </form>
</div>

1 个答案:

答案 0 :(得分:3)

您无法在'Open'来电中使用bind_parambind_param 要求每个参数都是参考。

您需要先将其存储在变量中。

$status = 'Open';
$stmt->bind_param('sssssss', $_POST['post_subject'], $_POST['post_description'], $_POST['post_assigned'], $status, $userid, $_POST['post_priority'], $_POST['post_employee']);