1

我的 PHP 代码有问题。我正在尝试使用稍后在表单中需要的两个会话变量将数据插入到 mysql 数据库中。但是,每当我提交表单时,都会返回“'字段列表'中的未知列”错误。代码很长,但您可能需要全部代码才能理解问题。

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

希望有人能帮忙!

4

3 回答 3

3

该错误几乎是不言自明的,这意味着您的数据库中的列名错误。我建议您仅针对这种情况回显您的查询错误:

$r = mysqli_query ($dbc, $q) or die (mysqli_error());

于 2013-02-05T19:35:52.293 回答
2

您的语句中列出的列之一INSERT实际上并不存在于prisoner_profile. 检查您的表架构。

于 2013-02-05T19:35:25.460 回答
1

我在这里看到的一个明显问题是您没有处理查询中的转义字符,并且您\n在代码中使用了一些字符。

用于mysqli_real_escape_string在将数据输入数据库时​​进行处理。

就像是

$q = mysqli_real_escape_string($q);
于 2013-02-05T19:37:29.403 回答