0

I'm having trouble with inserting data into 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 seems to fail no matter what way I write the code and fails in the same place it connects to the database and finds the dbname but can't put data in it.

The error I get from a 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);
?>
flyingscot5
  • 84
  • 2
  • 13

2 Answers2

0

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

insert into accounts (`steam id`) VALUES ('HEY');
Ahmad ghoneim
  • 844
  • 7
  • 13
-1

Remove gap between Steam and ID, it should be Steam_ID or steamID. Make sure your db columns don't have any gap between words.

Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
  • i don't think u can under stand how many times iv'e rewrote the piece of code and different ways iv'e tried and this fixes it thank you loads for your help just hating my self atm. – flyingscot5 Jul 14 '16 at 02:37
  • @Dagon did you checked what i described. If you didn't understand then i.prove your skills. – Rakesh Kumar Jul 14 '16 at 02:38
  • @flyingscot5 don't worry it happens i start soon you will learn a lot amazing things, just carry on. – Rakesh Kumar Jul 14 '16 at 02:39