简体   繁体   中英

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'

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

Using 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

$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

It's very straight

while you are getting this type error :{Error: Unknown column 'planner_id' in 'field list'}

Troubleshoot first step will be Just Describe The Table [subject_directorio]

Desc subject_directorio and check planner_id is exist or not. According to to the error

subject_directorio not holding any column called as planner_id

Hope it helps!!

It's self explanatory that your table doesn't have a column planner_id . Even if you see that it has, you may have trialing spaces before or after planner_id in the column name. Check carefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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