【问题标题】:Cannot pass parameter by reference - MySQLi [duplicate]无法通过引用传递参数-MySQLi [重复]
【发布时间】:2014-03-17 01:35:28
【问题描述】:

我的插入查询有问题。我正在尝试从会话变量中获取用户 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>

【问题讨论】:

  • 第 29 行是什么?如果我查看您的代码,第 29 行似乎是“?>”,我非常怀疑这是错误的位置
  • 第29行是$stmt->bind_param
  • 我认为您有一个名为sec_session_start() 的函数在其他地方被调用?如果不是,那么您打算使用session_start(); ;-)
  • 是的,我知道,它在包含的 functions.php 中。

标签: php html mysql mysqli insert


【解决方案1】:

您不能在 bind_param 通话中使用 'Open'bind_param 要求每个参数都是一个引用。

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

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

【讨论】:

  • 14 seconds 击败你,但会投票赞成 ;-)
  • @Fred-ii-:我在发布此消息后立即注意到您的评论! :-D
  • 我今天早上确实看到了that answer,并在我的一个标签中打开了它;-) @RocketHazmat 这就是为什么我“快速抽签”的原因。
  • @Fred-ii-:我在过去的项目中遇到过同样的问题,所以我对Cannot pass parameter X by reference 错误很熟悉 :-)
  • @RocketHazmat 这就是我们“学习”的方式 ;-) SO 是一个无价的“工具”。
猜你喜欢
  • 2011-07-31
  • 2013-01-12
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2018-02-21
  • 2015-10-09
  • 2013-05-16
相关资源
最近更新 更多