问题描述
我问了一个PDO错误问题(但是对象还有另一个问题...
这是我的代码:
include('../../config/connexion-bdd.php');
$nom = $_POST['nom'];
$regexNom = '/^[a-zA-Z -]+$/';
$prenom = $_POST['prenom'];
$regexPrenom = '/^[a-zA-Z -]+$/';
$email = $_POST['email'];
$regexEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$identifiant = $_POST['identifiant'];
$regexIdentifiant = '/^[a-z1-9]+$/';
$motDePasse = $_POST['motDePasse'];
$confirmeMotDePasse = $_POST['ConfirmeMotDePasse'];
$regexMdp = '/^[a-zA-Z1-9]+$/';
$utilisateurConnecte = $_POST['utilisateur-connecte'];
if (preg_match($regexNom, $nom) &&
preg_match($regexPrenom, $prenom) &&
preg_match($regexEmail, $email) &&
preg_match($regexIdentifiant, $identifiant) &&
preg_match($regexMdp, $motDePasse) &&
preg_match($regexMdp, $confirmeMotDePasse) &&
$motDePasse == $confirmeMotDePasse)
{
$stmt = $bdd->prepare('SELECT COUNT(*) FROM utilisateurs WHERE identifiant = ?');
$stmt->execute(array($_POST['identifiant']));
if ($stmt->fetchColumn() == 0){
$updt=$connect->prepare("INSERT INTO utilisateurs('nom','prenom','email','identifiant','mot_de_passe') VALUES (:nom, :prenom, :email, :identifiant, MD5(:mdp))");
$updt->execute(array('nom'=>$nom,'prenom'=>$prenom,'email'=>$email,'identifiant'=>$identifiant,'mdp'=>$motDePasse));
header('./gestion-utilisateur.php');
} else {
echo 'error 1';
}
} else {
echo 'error 2';
}
因此,对于此行$stmt = $bdd->prepare('SELECT COUNT(*) FROM utilisateurs WHERE identifiant = ?');
,我有此错误:
致命错误:在...中的非对象上调用成员函数prepare()
可以帮我吗?
很明显,您的$bdd
不包含SQL连接.您必须检查SQL连接的有效性.
$bdd
必须是一个对象,因此它包含一个名为prepare()
的方法.
如果未正确建立连接,则变量$bdd
将不是对象,因此将没有方法prepare.
现在,当您使用$bdd->prepare()
时,它会引发错误,指出$bdd
不是对象,因此无法使用称为prepare()
现在,您应该在$bdd->prepare()
行之前显示var_dump($bdd)
以便进行检查.
I've ask a question for PDO error (hereBut I've a another problem with object...
Here is my code :
include('../../config/connexion-bdd.php');
$nom = $_POST['nom'];
$regexNom = '/^[a-zA-Z -]+$/';
$prenom = $_POST['prenom'];
$regexPrenom = '/^[a-zA-Z -]+$/';
$email = $_POST['email'];
$regexEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$identifiant = $_POST['identifiant'];
$regexIdentifiant = '/^[a-z1-9]+$/';
$motDePasse = $_POST['motDePasse'];
$confirmeMotDePasse = $_POST['ConfirmeMotDePasse'];
$regexMdp = '/^[a-zA-Z1-9]+$/';
$utilisateurConnecte = $_POST['utilisateur-connecte'];
if (preg_match($regexNom, $nom) &&
preg_match($regexPrenom, $prenom) &&
preg_match($regexEmail, $email) &&
preg_match($regexIdentifiant, $identifiant) &&
preg_match($regexMdp, $motDePasse) &&
preg_match($regexMdp, $confirmeMotDePasse) &&
$motDePasse == $confirmeMotDePasse)
{
$stmt = $bdd->prepare('SELECT COUNT(*) FROM utilisateurs WHERE identifiant = ?');
$stmt->execute(array($_POST['identifiant']));
if ($stmt->fetchColumn() == 0){
$updt=$connect->prepare("INSERT INTO utilisateurs('nom','prenom','email','identifiant','mot_de_passe') VALUES (:nom, :prenom, :email, :identifiant, MD5(:mdp))");
$updt->execute(array('nom'=>$nom,'prenom'=>$prenom,'email'=>$email,'identifiant'=>$identifiant,'mdp'=>$motDePasse));
header('./gestion-utilisateur.php');
} else {
echo 'error 1';
}
} else {
echo 'error 2';
}
Therefore I have this error for this line $stmt = $bdd->prepare('SELECT COUNT(*) FROM utilisateurs WHERE identifiant = ?');
:
Fatal error: Call to a member function prepare() on a non-object in ...
Can you help my, please ?
It is all clear, Your $bdd
is not containing a SQL connection. You have to check for the validity of the SQL connection.
$bdd
must be an object, and thus it contains a method called prepare()
.
When the connection is not established correctly, the variable $bdd
will not be an object and thus will not have a method prepare.
Now, when you use $bdd->prepare()
it throws an error that $bdd
is not an object, thus it could not have a method called prepare()
Now, you should present a var_dump($bdd)
before the $bdd->prepare()
line for use to inspect it.
这篇关于为什么出现致命错误:在非对象上调用成员函数prepare()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!