I'm trying to write a basic function creating a table with a name as a parameter.
When I execute the function I get this error : "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 '( id int(10) unsigned NOT NULL AUTO_INCREMENT, nom VARCHAR (255), questio' at line 1"
I looked at many forum posts and it seems like this syntax worked for some people, but not for meeee ! Here's my code :
function connexion_bd() {
$serv ="localhost";
$username = "root";
$pwd = "blah_blah";
$bd ="project";
$connex = mysqli_connect($serv, $username, $pwd, $bd);
if (! $connex) {
page_erreur(ERR_CONNEX, mysqli_connect_error($connex)); exit;
}
return $connex;
}
function creer_jeu_bdd($nom) {
$connex = connexion_bd();
$nom = mysqli_real_escape_string($connex,$nom);
$req = "CREATE TABLE IF NOT EXISTS ".$nom." (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
nom VARCHAR (255),
question text NOT NULL,
reponse1 VARCHAR(256) NOT NULL DEFAULT ``,
reponse2 VARCHAR(256) NOT NULL DEFAULT ``,
reponse3 VARCHAR(256) NOT NULL DEFAULT ``,
bonne_reponse TINYINT(2) UNSIGNED NOT NULL,
foreign key (nom) references jeux(nom),
primary key(`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8; ";
$resultat = mysqli_query($connex, $req);
if (!$resultat) {
page_erreur(ERR_REQUETE, mysqli_error($connex));
} elseif (mysqli_num_rows($resultat) > 0) {
page_erreur(ERR_LOGIN, '');
}
mysqli_close($connex);
exit;
}
Thanks a lot.