Search code examples
phpmysqlsyntax-errorsql-insertmysql-error-1064

I got an SQL error, trying to insert a form to the database


I have no idea of what i am doing wrong. I'm trying to insert a form, to my table.

Insert failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-v, youtube-c, portfolio-link, picture-link) VALUES('test', 'test', '[email protected]' at line 1

<?php
ob_start();

$sub_firstname = trim($_POST["firstname"]);
$sub_lastname = trim($_POST["lastname"]);
$sub_email = trim($_POST["email"]);
$sub_youtube_v = trim($_POST["youtube-v"]);
$sub_youtube_c = trim($_POST["youtube-c"]);
$sub_channellink = trim($_POST["channel-link"]);
$sub_picturelink = trim($_POST["picture-link"]);

include('../dbconnect.php');

if (empty($sub_firstname) || empty($sub_lastname)) {
    header ("Location: ../panel.php?page=artikler.php&create=empty");
    exit;
}

$sub_firstname = $mysqli->real_escape_string($sub_firstname);
$sub_lastname = $mysqli->real_escape_string($sub_lastname);

if (!$mysqli->query("INSERT INTO submittedforms (firstname, lastname, email, youtube-v, youtube-c, portfolio-link, picture-link) VALUES('$sub_firstname', '$sub_lastname', '$sub_email', '$sub_youtube_v', '$sub_youtube_c', '$sub_channellink', '$sub_picturelink')")) {
 echo "Insert failed: (" . $mysqli->errno . ") " . $mysqli->error;
 die();
}else{
header("Location: ../panel.php?page=artikler.php&id=".$id."&create=success");
exit;
ob_flush();
}
?>

Solution

  • You cannot have a dash (-) in your column identifier unless you wrap it in ticks. Otherwise it is seen as a subtraction operator:

    INSERT INTO submittedforms (firstname, lastname, email, `youtube-v`, `youtube-c`, `portfolio-link`, `picture-link`
    

    It is best practice to not use dashes in your identifiers. You should use an underscore(_) instead.