How do I fix "Unknown column in 'field list'"

Ong Zhen Yuan picture Ong Zhen Yuan · Oct 11, 2015 · Viewed 8.5k times · Source

I am currently doing a school project which involves creating a website for users to input events. But I am currently facing this problem of:

Cannot insert event title Error Code: Unknown column 'event_title' in 'field list' ]

this is my coding:

<?php  

$e_title = $_POST['title'];
$e_venue = $_POST['venue'];
$e_date1 = $_POST['date1'];
$e_date2 = $_POST['date2'];
$e_time1 = $_POST['time1'];
$e_time2 = $_POST['time2'];
$e_remarks = $_POST['remarks'];
$e_blurbs = $_POST['blurbs'];
$e_organiser = $_POST['organiser'];

if ($e_title=='') {
exit ("Event Field is empty. Click to <a href=addevent.php> Go Back</a>");
}

$conn = @mysql_connect ('localhost', 'root','');
if (!$conn) die ("<p>No MYSQL Connection</p>");
$conn = mysql_select_db('events');
if (!$conn) die("<p>Cannot select database</p>");

$sql = "INSERT into `event` (event_title, event_venue, event_date1, event_date2, event_time1,
event_time2, event_remarks, event_blurbs, event_organiser) VALUES ( '$e_title', '$e_venue', '$e_date1', '$e_date2', '$e_time1','$e_time2', '$e_remarks', '$e_blurbs', '$e_organiser')"; 


$result = @mysql_query ($sql);

if(!$result){
exit( "<p>Cannot insert event title</p>  Error Code: <p>". mysql_error()."</p>");
}
else {
echo "event saved successfully!";
}

?>

this is my coding in phpmyadmin database "events" ...I'm currently using XAMPP

CREATE TABLE EVENT(
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    title TEXT,
    venue TEXT,
    date1 DATE NOT NULL
    date2 DATE NOT NULL
    time1 TIME 
    time2 TIME
    remarks TEXT,
    blurbs TEXT,
    organiser TEXT
)

Answer

mynawaz picture mynawaz · Oct 11, 2015

The columns in your table do not start with event_ whereas the columns in your INSERT list do. Remove the event_ from all columns like this

$sql = "INSERT into `event` (title, venue, date1, date2, time1,
time2, remarks, blurbs, organiser) VALUES ( '$e_title', '$e_venue', '$e_date1', '$e_date2', '$e_time1','$e_time2', '$e_remarks', '$e_blurbs', '$e_organiser')"; 

You should consider using mysql_* functions, these will be removed in future versions. Also sanitize the data passed from a form before using in any database operation to avoid SQL injection attacks