我想使用PDO创建一个注册表单,但由于未知原因,除了写入数据库之外,所有内容都有效。
自从星期五以来我一直很困难,而且我在PDO中有点初学者,我相信我没有看到什么,但我无法说出什么...... 有什么想法吗? <?php
include('bdd.php'); //Allows to connect to the db from an other file
?>
<form name="inscription" action="confirmation.php" method="POST">
<label for 'prenom'>Prénom: </label>
<input type="text" name="prenom" required placeholder="Prénom"/>
<label for 'nom'>Nom: </label>
<input type="text" name="nom" required placeholder="Nom"/>
<label for 'passe'>Mot de passe : </label>
<input type="password" name="passe" required/>
<label for 'confirm_passe'>Confirmez le mot de passe : </label>
<input type="password" name="passe" required/>
<label for 'email'>Email: </label>
<input type="email" name="email"/ required placeholder="Adresse e-mail">
<label for 'telephone'>Téléphone: </label>
<input type="telephone" name="telephone"/ required placeholder="ex: 0123456789">
<select name="fonction" id="fonction" required="required">
<option value="">Selectionnez votre fonction</option>
<option value="choix1">Présiden(e)</option>
<option value="choix2">Vice-Président(e)</option>
<option value="choix3">Administrateur</option>
<option value="choix4">Directeur/trice</option>
<option value="choix5">Travailleur social</option>
<option value="choix6">Secrétaire</option>
</select>
<input type="submit" value="send">
</form>
<?php
if(!empty($_POST['inscription'])){
if ( $_POST['confirm_passe'] != $_POST['passe'] ){
echo "Passowords don't match";
}
$req = $db->prepare('INSERT INTO inscrits (prenom, nom, passe, telephone, email, fonction, droits) VALUES(:prenom, :nom, :passe, :telephone, :email, :fonction, 0)');
$req->execute(array(
'prenom' => $_POST['prenom'],
'nom' => $_POST['nom'],
'passe' => $_POST['passe'],
'telephone' => $_POST['telephone'],
'email' => $_POST['email'],
'fonction' => $_POST['fonction'],
'droits' => $_POST['droits'],
));
}
?>
正如您所看到的,我不是英语,所以如果您需要了解某些内容,请不要犹豫要求任何翻译:)
谢谢!
答案 0 :(得分:1)
input_parameters中的键必须与SQL中声明的键匹配。
试试这个:
编辑错误消息:
组:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
在bdd.php文件中进行pdo错误处理。
然后
try{
$req = $db->prepare('INSERT INTO inscrits (prenom, nom, passe, telephone, email, fonction, droits) VALUES(:prenom, :nom, :passe, :telephone, :email, :fonction, 0)');
$req->execute(array(
':prenom' => $_POST['prenom'],
':nom' => $_POST['nom'],
':passe' => $_POST['passe'],
':telephone' => $_POST['telephone'],
':email' => $_POST['email'],
':fonction' => $_POST['fonction'],
':droits' => $_POST['droits']
));
}
catch (PDOException $e) {
print $e->getMessage ();
die;
}
答案 1 :(得分:1)
:droits
函数中缺少prepare()
(在VALUES内)。
$req = $db->prepare('INSERT INTO inscrits (prenom, nom, passe, telephone, email, fonction, droits) VALUES (:prenom, :nom, :passe, :telephone, :email, :fonction, :droits)');
$req->execute(array(
'prenom' => $_POST['prenom'],
'nom' => $_POST['nom'],
'passe' => $_POST['passe'],
'telephone' => $_POST['telephone'],
'email' => $_POST['email'],
'fonction' => $_POST['fonction'],
'droits' => $_POST['droits'],
));
}
答案 2 :(得分:0)
好的,所以我终于设法有一些实际工作的东西,这里有一个注册表单的代码,如果有人在将来需要它,需要由管理员验证:
<?php
include('bdd.php'); //File that allows you to get connected to the server
if(!empty($_POST)){
// Form has been submitted
if ( $_POST['confirm_passe'] != $_POST['passe'] ) echo "Passwords don't match";
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
//
$req = $db->prepare('INSERT INTO your_table (table1, table2, table3, table4, rights) VALUES(:value1, :value2, :value3, :value4, :rights)');
$req->execute(array(
':value1' => $_POST['table1'],
':value2' => $_POST['table2'],
':value3' => $_POST['table3'],
':value4' => $_POST['table4'],
':rights' => 0
));
header('location:confirmation.php'); //Redirects you in a page that informs you that you are now registered
}
catch (PDOException $e) {
print $e->getMessage ();
}
}
?>
<!--And here goes the form that you can adapt at will -->
<form name="inscription" action="inscription.php" method="POST">
<label for="prenom">Prénom: </label>
<input type="text" name="prenom" required placeholder="Prénom"/>
<label for 'nom'>Nom: </label>
<input type="text" name="nom" required placeholder="Nom"/>
<label for 'passe'>Mot de passe : </label>
<input type="password" name="passe" required/>
<label for="confirm_passe">Confirmez le mot de passe : </label>
<input type="password" name="confirm_passe" required/>
<label for 'email'>Email: </label>
<input type="email" name="email"/ required placeholder="Adresse e-mail">
<label for 'telephone'>Téléphone: </label>
<input type="telephone" name="telephone"/ required placeholder="ex: 0123456789">
<select name="fonction" id="fonction" required="required">
<option value="">Selectionnez votre fonction</option>
<option value="choix1">Présiden(e)</option>
<option value="choix2">Vice-Président(e)</option>
<option value="choix3">Administrateur</option>
<option value="choix4">Directeur/trice</option>
<option value="choix5">Travailleur social</option>
<option value="choix6">Secrétaire</option>
</select>
<input type="submit" value="send">
</form>
感谢您的帮助!