1

I have a problem with my insert query. I'm trying to get the user ID from the session variable and insert it into the table along with my other variables that is input via a form.

I have tried printing the $userid variable, and it shows up as 1, which is correct. The bind_param statement just seems to not accept it.

I keep getting this error

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

Line 29 is the $stmt->bind_param line.

The php code:

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

This is the form:

<?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>
slopedoo
  • 47
  • 6

1 Answers1

3

You can't use 'Open' in your bind_param call. bind_param requires that each parameter is a reference.

You need to store that in a variable first.

$status = 'Open';
$stmt->bind_param('sssssss', $_POST['post_subject'], $_POST['post_description'], $_POST['post_assigned'], $status, $userid, $_POST['post_priority'], $_POST['post_employee']);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Beat you by [`14 seconds`](http://stackoverflow.com/questions/21834455/cannot-pass-parameter-by-reference-mysqli#comment33048080_21834455) lol but will upvote ;-) – Funk Forty Niner Feb 17 '14 at 16:41
  • @Fred-ii-: I noticed your comment right after I posted this! :-D – gen_Eric Feb 17 '14 at 16:42
  • 1
    I actually saw [`that answer`](http://stackoverflow.com/a/21824445/) given this morning, and left it open in one of my tabs ;-) @RocketHazmat that's why I was "Quick on the Draw", as it were. – Funk Forty Niner Feb 17 '14 at 16:43
  • @Fred-ii-: I had this same issue in a past project, so I'm familiar with the `Cannot pass parameter X by reference` error :-) – gen_Eric Feb 17 '14 at 16:45
  • 1
    @RocketHazmat And that's how we "learn" ;-) SO is an invaluable "tool". – Funk Forty Niner Feb 17 '14 at 16:45