flyingscot5 flyingscot5 - 4 months ago 11
PHP Question

failed to INSERT INTO [table] error in your SQL syntax; check the manual that corresponds to your MariaDB server version

//

Result is really Basic and stupid mistake: if you have space in your database name then

steam id
use "Back Ticks" or just remove spaces by adding "_" instead or just don't have spaces in your data base names.

Many Thanks to: @Rakesh Kumar, @Ahmad ghoneim

//

Disclaimer: I'm only 17 and dyslexic so sorry in advance for the poor spelling and horrible grammar, and yes I did have to look up how to spell dyslexic.

I'm having trouble with inserting data in to a
MySQL
database using
PHP
I've looked about around the website looking for similar results but I am not finding exactly what I'm looking for to be able to insert into my table.

I've simplified my code so if it is corrected it can be easily converted for other people to use in the own programs.

My problem is it always seem to fail no matter what way I write the code and fails in the same place it connects to the data base and finds the
dbname
but can't put data in it.

Error i get from website:


Error: insert into accounts (Steam ID) VALUES ('YAY') You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near 'ID) VALUES
('YAY')' at line 1


<?php
$serverhost = "localhost";
$pass = "";
$dbname = "website_db";
$table = "";

// Create connection
$connect = mysqli_connect($serverhost, $user, $pass, $dbname);

// Check connection
if (!$connect) {
die("Failed to connect to DataBase: " . mysqli_connect_error());
}

// SQL Variable
$sql = "insert into accounts (Steam ID)
VALUES ('YAY')";


// SQL query
if (mysqli_query($connect, $sql)) {
echo "Succesfully Inserted Data to Table";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connect);
}

// END
mysqli_close($connect);
?>

Answer

you should wrap it in backticks (``) like that

insert into accounts (`steam id`) VALUES ('HEY');
Comments