我尝试使用PDO制作注册表。我有下一个表格:
<form name="registration" action="registration.php" method="POST">
<label for 'username'>Username: </label>
<input type="text" name="userName"/>
<label for 'password'>Password: </label>
<input type="password" name="pass"/>
<label for 'first_name'>First name: </label>
<input type="text" name="fullName"/>
<label for 'email'>Email: </label>
<input type="text" name="email"/>
<br/>
<button type="submit">Submit</button>
</form>
我得到了registration.php文件来连接数据库并插入值:
<?php
$user = 'root';
$pass = '8169x5it';
$db = new PDO( 'mysql:host=localhost;dbname=reg_form', $user, $pass );
$form = $_POST;
$usernName = $form[ 'userName' ];
$pass = $form[ 'pass' ];
$fullName = $form[ 'fullName' ];
$email = $form[ 'email' ];
$sql = "INSERT INTO WebsiteUsers ( userName, pass, fullName, email ) VALUES ( :userName, :pass, :fullName, :email )";
$query = $db->prepare( $sql );
$query->execute( array( ':userName'=>$userName, ':pass'=>$pass, ':fullName'=>$fullName, ':email'=>$email ) );
?>
所以,问题是当我在文件中放入一些并按下提交按钮时,我的数据不会插入到数据库中。请帮助我,我是PDO和mysql的新手,我无法理解错误。提前谢谢!
答案 0 :(得分:0)
isset()
try{
$user = 'root';
$pass = '8169x5it';
$db = new PDO( 'mysql:host=localhost;dbname=reg_form', $user, $pass );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO WebsiteUsers ( userName, pass, fullName, email )
VALUES ( :userName, :pass, :fullName, :email )";
if($query = $db->prepare($sql)){
$query->bindValue(':userName', $_POST['userName']);
$query->bindValue(':pass', $_POST['pass']);
$query->bindValue(':fullName', $_POST['fullName']);
$query->bindValue(':email', $_POST['email']);
if($query->execute()){
echo 'execute() success ';
echo 'affected rows = '.$stmt->rowCount();
}else{
echo 'execute() failed';
}
}else{
echo 'prepare() failed';
}
}catch(PDOException $e) {
// Print PDOException message
echo $e->getMessage();
}