使用PHP变量插入数据库
我的代码有问题。 我想在数据库中插入PHP变量($variable) 但我有一个错误:使用PHP变量插入数据库,php,variables,insert,mysqli,Php,Variables,Insert,Mysqli,我的代码有问题。 我想在数据库中插入PHP变量($variable) 但我有一个错误: Erreur SQL ! INSERT INTO info(nom, prenom, entreprise, email, objet, message) VALUES(test,toto,overflow,toto@hotmail.fr,Autre,TEST) You have an error in your SQL syntax; check the manual that corresponds t
Erreur SQL !
INSERT INTO info(nom, prenom, entreprise, email, objet, message) VALUES(test,toto,overflow,toto@hotmail.fr,Autre,TEST)
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 '@hotmail.fr,Autre,TEST)' at line 1
我的代码:
<?php
// Connection to MySQL
$db = mysqli_connect('****', '***', '','****') or die("Erreur de connexion au serveur !!!");
// Create request
$sql = "INSERT INTO info(nom, prenom, entreprise, email, objet, message) VALUES($nom,$prenom,$entreprise,$email,$objet,$message)";
// Send request to DB
$req = mysqli_query($db,$sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysqli_error($db));
// Close connection
$deconnexion = mysqli_close(mysqli_connect('localhost', 'root', '','contact'));
?>
我想用mysqli来做这个插入
我的插入错误在哪里
$sql = "INSERT INTO info (nom, prenom, entreprise, email, objet, message) VALUES ('$nom','$prenom','$entreprise','$email','$objet','$message')";
字符串值需要被引用 下面是一个更好的替代方法
$sql = "INSERT INTO info (nom, prenom, entreprise, email, objet, message) VALUES (?, ?, ?, ?, ?, ?)";
$sql->bindParam('ssssss', $nom, $prenom, $entreprise, $email, $objet, $message);
$sql->execute();
来自php手册。请尝试此代码
<?php
// Connection to MySQL
$db = mysqli_connect('****', '***', '','****') or die("Erreur de connexion au serveur !!!");
// Create request
$sql = "INSERT INTO info(nom, prenom, entreprise, email, objet, message) VALUES('$nom','$prenom','$entreprise','$email','$objet','$message')";
// Send request to DB
$req = mysqli_query($db,$sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysqli_error($db));
// Close connection
$deconnexion = mysqli_close(mysqli_connect('localhost', 'root', '','contact'));
?>
需要引用字符串值。。。。但是您正在使用MySQLi,那么为什么不进入21世纪,开始使用prepared statements/bind呢variables@MarkBakerThx Mark:)好的,我必须这样做:$sql=“插入信息(nom,prenom,enterprise,email,objet,message)值(“$nom”,“$prenom”,“$enterprise”,“$email”,“$objet”,“$message”)”;如果您使用的是绑定变量,那么就不需要记住引用字符串,或者转义插入的变量Show来执行此操作?这比我的解决方案好?我对撇号变量有异议。。。如何逃避这个问题?你能用另一个问题来回答这个问题吗?准备好的陈述也要注意引用+1对于绑定参数示例,我理解,但代码相同。。。我尝试使用mysqli\u real\u escape\u string(),但这不起作用。。