0

I've been trying to insert some data into my database for an events page. I have an html form and a seperate script, as seen below and the submit seems to go through for the ename id and imgsrc values but nothing past that. Anything more and I get a 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 'when, descr, positions) VALUES (test, 1 ,www.vzdc.org,2017-1-20 23:59:00' at line 1I've done some reasearch but maybe it's just a weird error on my end? I'm fairly new to mysql and I would love some help! Thanks, code below.

<!-- HTML form -->
 <form id="newevent" action="insertevent.php" method="post">
                <p>Event Name:</p><input name="ename" type="text" width="100">
                <p>ID:</p><input name="id" type="text" size="5">
                <p>Banner Link:</p><input name="imgsrc" type="text" size="50">
                <p>Description</p><input name="descr" type="text" height="1000px" >
                <p>Date / Time (yyyy-mm-dd HH:MM:SS):</p><input name="when" type="text">
                <p>Positions (ONE per line)</p><textarea name="positions" form="newevent" rows="10" cols="50"></textarea><br>
                <input value="Add Event" type="submit">
            </form>

/* PHP script on insertevent.php */
<?php
$link = mysqli_connect("localhost", "root", "xxx", "xxx");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$ename = mysqli_real_escape_string($link, $_POST['ename']);
$id = mysqli_real_escape_string($link, $_POST['id']);
$imgsrc = mysqli_real_escape_string($link, $_POST['imgsrc']);
$when = mysqli_real_escape_string($link, $_POST['when']);
$descr = mysqli_real_escape_string($link, $_POST['descr']);
$positions = mysqli_real_escape_string($link, $_POST['positions']);

// attempt insert query execution
$sql = "INSERT INTO events (ename, id, imgsrc, when, descr, positions) VALUES (`$ename`, $id , `$imgsrc`, `$when`, `$descr`, `$positions`)";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

2 Answers 2

2

Don't use back-ticks for binding variables to your query, use single ticks instead. You can use back-ticks for the table and column name:

INSERT INTO `events` (`ename`, `id`, `imgsrc`, `when`, `descr`, `positions`) 
VALUES ('$ename', '$id', '$imgsrc', '$when', '$descr', '$positions')

WHEN is also a reserved word, so better change its name.

And since you're using mysqli_* API already, check prepared statement

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That did it. I saw that prepared statement here and there, but know I know. Much appreciated.
0

You are using an SQL reserved word as a column name.

$sql = "INSERT INTO events (ename, id, imgsrc, when, descr, positions) VALUES (`$ename`, $id , `$imgsrc`, `$when`, `$descr`, `$positions`)";

You really shouldn't, but if you want to get away with this, surround your table/column names with back ticks ```, like this:

$sql = "INSERT INTO `events` (`ename`, `id`, `imgsrc`, `when`, `descr`, `positions`) VALUES ('$ename', '$id' , '$imgsrc', '$when', '$descr', '$positions')";

I've removed the back ticks you put around your values because, well, they shouldn't be there.

Please learn and use MySQLi prepared statements. They'll help.

3 Comments

What is the difference between @LoganWayne's answer to yours?
I felt it wasn't really in-depth, so I added an explanation of why the OP had the error, plus some other elaboration. No rule against that, is there?
Unfortunately your code also will generate errors, because you have not enclosed string variables in the query in single quotes.(and you have missed one back-tick)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.