簡體   English   中英

錯誤:“字段列表”中的未知列“ x”

[英]Error: Unknown column 'x' 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);
        }
     } 

?>

數據庫

您使用錯誤的方式連接數據庫並獲取其數據。 因為您的數據庫可能使用SQL注入被黑客入侵,正確的方法是:

使用PDO

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

對於錯誤捕獲:

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();
}

和數據獲取:

$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();
}

使用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
}

還有你的問題

我認為問題出在您的查詢和綁定參數上,因此請嘗試按照我向您展示的方式使用正確的方法,然后向我們展示結果。 SQLFiddle

挺直的

當您遇到此類型錯誤時:{錯誤:“字段列表”中的未知列“ planner_id”}

解決問題的第一步將只是描述表[subject_directorio]

描述subject_directorio並檢查planner_id是否存在。 根據錯誤

subject_directorio不保存任何稱為planner_id的列

希望能幫助到你!!

不言而喻,您的表沒有一列planner_id 即使您看到了它,您也可能在列名的planner_id之前或之后有試用版空間。 仔細檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM