将我的代码更改为mysqli程序中的预准备语句,其中在提交到我的php文件后显示此错误。
致命错误:无法通过引用传递参数8
这是我的PHP代码。
我刚刚将我的插入数据中的代码复制到没有数组的db,但是这个php代码会获得数组。
<?php
include 'admin/db/database_configuration.php';
if(isset($_POST['submit'])){
if (empty($_POST['title'])){$job_title = 'NULL'; } else{ $job_title ="'". mysqli_real_escape_string($conn, $_POST['title']) . "'";}
if (empty($_POST['desc'])){$job_desc = 'NULL'; } else{ $job_desc ="'". mysqli_real_escape_string($conn, $_POST['desc']) . "'";}
$qualifications ="";
if(isset($_POST["quali"]) && is_array($_POST["quali"])){
$qualifications = implode("\n", $_POST["quali"]);
}
if (empty($_POST['name_cont'])){$name_contact = 'NULL'; } else{ $name_contact ="'". mysqli_real_escape_string($conn, $_POST['name_cont']) . "'";}
if (empty($_POST['contact'])){$contact_num = 'NULL'; } else{ $contact_num ="'". mysqli_real_escape_string($conn, $_POST['contact']) . "'";}
if (empty($_POST['email_add'])){$email_cont = 'NULL'; } else{ $email_cont ="'". mysqli_real_escape_string($conn, $_POST['email_add']) . "'";}
$stmt = $conn->prepare("INSERT INTO `tbljoba` (job_title, job_desc, job_qualifications, cont_name, contact_info, employer_email, job_status) VALUES(?,?,?,?,?,?,?)") or die(mysqli_error($conn));
$stmt->bind_param("sssssss", $job_title, $job_desc, $qualifications, $name_contact, $contact_num, $email_cont, 'pending'); //bind to param
if($stmt->execute()){
$stmt->close();
$conn->close();
echo '<script>alert("Successfully Sent")</script>';
echo '<script>window.location = "employer_contact_us.php"</script>';
}else{
echo '<script>alert("Error")</script>';
}
}
$conn->close();
?>
在这行中我遇到了错误
$stmt->bind_param("sssssss", $job_title, $job_desc, $qualifications, $name_contact, $contact_num, $email_cont, 'pending'); //bind to param
答案 0 :(得分:1)
错误在'pending'
来电中bind_param
。
bind_param
的所有参数必须通过引用传递。字符串是原始值,不能通过引用传递。
您可以通过创建变量并将其作为参数传递来解决此问题:
$status = 'pending';
$stmt->bind_param("sssssss", $job_title, $job_desc, $qualifications, $name_contact, $contact_num, $email_cont, $status); //bind to param
或者,如果状态始终挂起,则可以将其硬编码到查询中。
// add 'pending' into the VALUES part of the query
$stmt = $conn->prepare("INSERT INTO `tbljoba` (job_title, job_desc, job_qualifications, cont_name, contact_info, employer_email, status) VALUES(?, ?, ?, ?, ?, 'pending')") or die(mysqli_error($conn));
// no need to bind 'pending'
$stmt->bind_param("ssssss", $job_title, $job_desc, $qualifications, $name_contact, $contact_num, $email_cont); //bind to param