简体   繁体   English

错误:“字段列表”中的未知列“ x”

[英]Error: Unknown column 'x' in 'field list'

I'm having some trouble inputting some data into a table. 将某些数据输入表时遇到麻烦。

I'm retrieving some values from a form and inputting them to a table, but this error shows up every time: 我正在从表单中检索一些值并将其输入到表中,但是每次都会显示此错误:

Error: Unknown column 'planner_id' in 'field list' 错误:“字段列表”中的未知列“ planner_id”

<?php
session_start(); 
include 'conexion_data.php';
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

    $teacherid = $_POST["teacherid"];
    $plannerid = $_POST["plannerid"];
    $yeargroup = $_POST["yeargroup"];
    $subject = $_POST["subject"];
    $planner_event = htmlspecialchars($_POST["event_comment"]);
    $event_date = $_POST["event_date"];
echo "$teacherid $plannerid $yeargroup  $planner_event $event_date <br/><br />";


    if (empty($event_date) or empty($planner_event)) {
        echo "One of the fields was left blank! <br />";
    } else {
        $sql = "INSERT INTO subject_directorio (planner_id, teacher_id, subject, yeargroup, date, comment ) VALUES ('$plannerid', '$teacherid', '$subject', '$yeargroup', '$event_date', '$planner_event')";
        if (!mysqli_query($con,$sql)) {
            die('Error: ' . mysqli_error($con));
        } else {
        /* header('Location: user_area.php'); */
        echo "Data was inputed to DB";
            mysqli_close($con);
        }
     } 

?>

Database 数据库

You are using wrong way how to connect to database and fetch its data. 您使用错误的方式连接数据库并获取其数据。 Because you database may be hacked using SQL Injection The right way how to do this is: 因为您的数据库可能使用SQL注入被黑客入侵,正确的方法是:

Using PDO 使用PDO

$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);

For error catching: 对于错误捕获:

try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

And data fetching: 和数据获取:

$id = 5;
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    

    $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
    $stmt->execute(array('id' => $id));

    while($row = $stmt->fetch()) {
        print_r($row);
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

Using Mysqli 使用Mysqli

$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // do something with $row
}

And your problem 还有你的问题

I think problem is in your query and binding params to it.So try to use proper way as I shown you, and then show us results. 我认为问题出在您的查询和绑定参数上,因此请尝试按照我向您展示的方式使用正确的方法,然后向我们展示结果。 SQLFiddle SQLFiddle

It's very straight 挺直的

while you are getting this type error :{Error: Unknown column 'planner_id' in 'field list'} 当您遇到此类型错误时:{错误:“字段列表”中的未知列“ planner_id”}

Troubleshoot first step will be Just Describe The Table [subject_directorio] 解决问题的第一步将只是描述表[subject_directorio]

Desc subject_directorio and check planner_id is exist or not. 描述subject_directorio并检查planner_id是否存在。 According to to the error 根据错误

subject_directorio not holding any column called as planner_id subject_directorio不保存任何称为planner_id的列

Hope it helps!! 希望能帮助到你!!

It's self explanatory that your table doesn't have a column planner_id . 不言而喻,您的表没有一列planner_id Even if you see that it has, you may have trialing spaces before or after planner_id in the column name. 即使您看到了它,您也可能在列名的planner_id之前或之后有试用版空间。 Check carefully. 仔细检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM