[英]Inserting PHP Session Variables into MySQL Database
I am having issues with my PHP code. 我的PHP代码有问题。 I am trying to insert data into a mysql database using two session variables that I will need at a later time in the form.
我试图使用稍后将需要在表单中的两个会话变量将数据插入mysql数据库。 However whenever I submit the form I am returned with a "Unknown column in 'field list'" error.
但是,每当我提交表单时,都会返回“字段列表中的未知列”错误。 The code is lengthy but you will likely need all of it to understand the issue.
代码很长,但是您可能需要全部代码才能理解问题。
<?php
session_start();
// Check for hazards and put them in an array if there is one selected
if($_SERVER['REQUEST_METHOD'] == 'POST') {
require ('../mysqli_connect.php'); //connect to the db
//Check for offender first name
if (empty($_POST['pris_firstname'])) {
$errors[] = 'You forgot to enter offender first name.';
} else {
$prisf=$_POST['pris_firstname'];
}
//Check for offender last name
if (empty($_POST['pris_lastname'])) {
$errors[] = 'You forgot to enter offender last name.';
} else {
$prisl=$_POST['pris_lastname'];
}
//Check for offender date of birth
$dob = ($_POST['pris_dateofbirth']);
//Check for offender phone number
if (empty($_POST['pris_phonenum'])) {
$errors[] = 'You forgot to enter offender Phone Number.';
} else {
$prisphone=trim($_POST['pris_phonenum']);
}
//Check for offender address
if (empty($_POST['pris_address'])) {
$errors[] = 'You forgot to enter offender Address.';
} else {
//$prisaddress=trim($_POST['pris_address']);
foreach($_POST["pris_address"] as $value) {
$prisaddress .= $value . '\n';
}
}
//Check for offender next of kin first name
if (empty($_POST['pris_kinfirstname'])) {
$errors[] = 'You forgot to enter next of kin first name.';
} else {
$kinfirst=trim($_POST['pris_kinfirstname']);
}
//Check for offender next of kin last name
if (empty($_POST['pris_kinlastname'])) {
$errors[] = 'You forgot to enter next of kin last name.';
} else {
$kinlast=trim($_POST['pris_kinlastname']);
}
//Check for offender next of kin phone number
if (empty($_POST['pris_kinphone'])) {
$errors[] = 'You forgot to enter next of kin area code.';
} else {
$kinphone=trim($_POST['pris_kinphone']);
}
if (empty($_POST['pris_kinrelation'])) {
$errors[] = 'You forgot to enter next of kin relation.';
} else {
$kinrelation=trim($_POST['pris_kinrelation']);
}
//Check for offender next of kin address
if (empty($_POST['pris_kinaddress'])) {
$errors[] = 'You forgot to enter next of kin street address.';
} else {
foreach($_POST["pris_kinaddress"] as $value2) {
$kinaddress .= $value2 . '\n';
}
}
if (empty($errors)) { //if everyhing is ok
$q = "INSERT INTO prisoner_profile (pris_status,
pris_firstname,
pris_lastname,
pris_dateofbirth,
pris_phonenum,
pris_address,
pris_kinfirstname,
pris_kinlastname,
pris_kinphone,
pris_kinaddress,
pris_kinrelation
) VALUES (
'$status',
".$_SESSION['pris_firstname'].", ".$_SESSION['pris_lastname'].",
'$dob',
'$prisphone',
'$prisaddress',
'$kinfirst',
'$kinlast',
'$kinphone',
'$kinaddress',
'$kinrelation'
)";
$r = @mysqli_query ($dbc, $q); //Run the query.
Hope someone can help! 希望有人能帮忙!
The error is pretty much self-explanatory, it means that you have got a column name wrong in your database. 该错误几乎是不言自明的,这意味着您在数据库中输入的列名错误。 I recomend you echo out the error for your query just for this case as:
我建议您仅针对这种情况回显出查询错误:
$r = mysqli_query ($dbc, $q) or die (mysqli_error());
One of the columns that are listed in your INSERT
statement does not actually exist in the prisoner_profile
. INSERT
语句中列出的列之一实际上不存在于prisoner_profile
。 Check your table schema. 检查您的表架构。
The one obvious issue I can see here is that you haven't handled the escape characters in your query, and you have used a few \\n
characters in your code. 我在这里可以看到的一个明显问题是,您尚未在查询中处理转义符,并且在代码中使用了一些
\\n
字符。
Use mysqli_real_escape_string
to handle that when inputting the data to the database. 在将数据输入数据库时,使用
mysqli_real_escape_string
进行处理。
Something like 就像是
$q = mysqli_real_escape_string($q);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.