D_Guy13 D_Guy13 - 4 years ago 74
SQL Question

MySQL Query failing to run

I have been at this code for 3 days and still can't see why it doesn't want to run. But I am pretty sure that it is a very simple error.

if(isset($_POST['submit'])){
$title = $conn->real_escape_string($_POST['title']);
$author_id = $conn->real_escape_string(trim($_POST['authors']));
$content = $conn->real_escape_string(trim($_POST['content']));
$content_folder = date("Y");
$image = $conn->real_escape_string($_POST['image']);
$main_page = $conn->real_escape_string(trim($_POST['main_page']));
$category = $conn->real_escape_string($_POST['categories']);
$published = $conn->real_escape_string(trim($_POST['published']));
$keywords = $conn->real_escape_string(trim($_POST['keywords']));
$description = $conn->real_escape_string(trim($_POST['description']));


// Insert into DB
$insert_row = $conn->query("INSERT INTO articles (title, author_id, content, content_folder, main_image, main_page, category, published, keywords, descinfo)
VALUES($title, $author_id, $content, $content_folder, $image, $main_page, $category, $published, $keywords, $description)") or die(mysqli_error($conn));

if($insert_row){
header('Location: '.$url.'/a/index.php?page=add&msg=ok');
}else{
echo '<div clas="error">Something went wrong</div>';
die(mysqli_error($conn));
}
}


It gives me the Syntax Error 1064. Appreciate any help.

Answer Source

You're missing single quotes:

$insert_row = $conn->query("INSERT INTO articles (title, author_id, content, content_folder, main_image, main_page, category, published, keywords, descinfo) 
VALUES('$title', '$author_id', '$content', '$content_folder', '$image', '$main_page', '$category', '$published', '$keywords', '$description')");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download