0

I have a problem, I can't upload anything to database. In my database in the jelenlet table there is a jelen which is integer and a gyerekneve which is text.

Here is my php code:

<?php
$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO 'jelenlet' ('gyerekneve', 'jelen') VALUES ('barmi', 0)";

if ($conn->query($sql) === TRUE) {
    echo "Hozzaadtad ezt a nevet: ";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>

And don't know what is the problem with the code. The page says:

Error: INSERT INTO 'jelenlet' ('gyerekneve', 'jelen') VALUES ('barmi', 0) 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 ''jelenlet' ('gyerekneve', 'jelen') VALUES ('barmi', 0)' at line 1

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You are using single quotes where you should not be. I vote to close such questions as simple typographic errors. – Gordon Linoff Mar 07 '18 at 13:19
  • remove all those `'` from around table and field names – Federico klez Culloca Mar 07 '18 at 13:19
  • @GordonLinoff If you claim it's a typo (which you always vote as for posts like this), then why is there a specific duplicate that specifically targets it and you don't use it as I did here and always do. I've asked myself that question quite a few times before. – Funk Forty Niner Mar 07 '18 at 13:22

1 Answers1

0
$sql = "INSERT INTO jelenlet (gyerekneve, jelen) VALUES ('barmi', 0)";

This will work. BUT make sure to use prepared statements when you will try to pass variables to this one and not static values. The problem was that you were using single-quotes when you didn't have to. If you want to escape fields in a query you can use this : `

This query would also work :

$sql = "INSERT INTO `jelenlet` (`gyerekneve`, `jelen`) VALUES ('barmi', 0)";
pr1nc3
  • 8,108
  • 3
  • 23
  • 36