Mikkel Bertelsen Mikkel Bertelsen - 6 months ago 10
SQL Question

SQL dosen't create anything in database

This is my code, but when I enter my database under users, nothing was created.
It has worked before.

Here is my database informations


$odb = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USERNAME, DB_PASSWORD);

if(!$odb){ die('Connection Failed!'); }

?>


And this is where i would like to create a user.

$steamid = $steamprofile['steamid'];

/*Check possible existing data*/

$SQLCheckLoginQuery = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `steamid` = :SteamID");

$SQLCheckLoginQuery -> execute(array(':SteamID' => $steamid));

$countUsers = $SQLCheckLoginQuery -> fetchColumn(0);

if($countUsers <= 0){

$SQLInsertQuery = $odb -> prepare("INSERT INTO `users` (steamid) VALUES :SteamID");
$SQLInsertQuery -> execute(array(':SteamID' => $steamid));
}
?>

Answer

You seems to have an error in your mysql query, your VALUES should be between brackets:

$SQLInsertQuery = $odb -> prepare("INSERT INTO `users` (steamid) VALUES (:SteamID)");

EDIT: On your comment you show us the structure of users, and the column avatar seems to be mandatory. So you should make a default value or add the avatar in your insert query

$SQLInsertQuery = $odb -> prepare("INSERT INTO `users` (steamid, avatar) VALUES (:SteamID, :avatar)");

Also, you can activate log in order to see if some exception are throwed

$odb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Comments