因为我是新来的,所以我试图具体说明我的问题。如果您需要更多信息,请告诉我。
每当我尝试提交表单时,它都不会显示有关代码的任何错误。它只显示 echo 语句“失败”
我正在使用此循环来查看值是否已提交。这工作正常。它表明所有值都已提交,但这些值并未插入到数据库中。
foreach ($_POST as $key => $value) {
echo "($key) => ($value)<br/>";
}
我的 html 表单代码是:
<div class="formstyle">
<h2> Sign up </h2>
<center>
<form method = 'POST' name="form1" onSubmit="return validateForm()" action="">
<table border='0'>
<tr>
<td><LABEL for="firstname">First Name:<sup style="color:#F00">*</sup> </LABEL></td>
<td><INPUT type="text" name = "fname" id="fname" value="<?php echo $fname;?>"></td><td width="200px"><i style="color:red;" id="pointfn"></i></td>
</tr>
<tr>
<td><LABEL for="lastname">Last Name:<sup style="color:#F00">*</sup> </LABEL></td>
<td><INPUT type="text" name ="lname" id="lname" value="<?php echo $lname;?>"> </td><td width="200px"><i style="color:red;" id="pointln"></i></td>
</tr>
<tr>
<td><LABEL for="gender">Gender:<sup style="color:#F00">*</sup> </LABEL></td> <td>
<INPUT type="radio" name="gender" id="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male"> Male
<INPUT type="radio" name="gender" id="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female"> Female</td><td width="200px"> <i style="color:red;" id="pointgendr"></i></td>
</tr>
<tr>
<td><LABEL for="email">Email:<sup style="color:red;">*</sup> </LABEL></td>
<td><INPUT type="text" name = "email" id="email" value="<?php echo $email;?>"></td><td width="200px"><i style="color:red;" id="pointemail"></i></td>
</tr>
<tr>
<td><LABEL for="password">Password:<sup style="color:#F00">*</sup> </LABEL></td>
<td><INPUT type="password" name ="password" id="password" value="<?php echo $password;?>"></td><td width="200px"><i style="color:red;" id="pointpassword"></i></td>
</tr>
<tr>
<td></td><td><br/><INPUT type="submit" name = "register" value="Create Account">
<INPUT type="reset" onClick="return confirmreset()"></td>
</tr>
<tr>
<td></td><td style="font-size:12px;text-align:right;"><br/><i style="color:red;font-size:12px;align:right;" >* - Mandatory</i></td>
</tr>
</table>
</form></center>
这是将所有内容插入数据库的 php 代码
require('connect.php');
$fname = $lname = $gender = $email = $password = "";
if(isset($_POST['register'])){
$stmt = $pdo->prepare('INSERT INTO user(fname,lname,gender,email,password)
VALUES (:fname, :lname, :gender, :email, :password)');
$stmt->bindValue(':fname',$fname);
$stmt->bindValue(':lname',$lname);
$stmt->bindValue(':gender',$gender);
$stmt->bindValue(':email',$email);
$passwordHash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 12));
$stmt->bindValue(':password',$passwordHash);
$stmt->execute();
$email_stmt = $pdo->prepare("SELECT email FROM user WHERE email = :email");
$email_stmt->bindParam(':email', $email);
$email_stmt->execute();
if ($email_stmt->rowCount()>0){
echo 'Email Already Exists. Use Different Email OR Login ';
} else {
//Successful Registration
echo 'Registration Successful';
}
} else {
echo 'FAILED';
}
?>
任何帮助,将不胜感激。干杯。
您正在检查使用插入后是否收到电子邮件
试试这个
if(isset($_POST['register'])){
$password = $_POST['password'];
$fname = $_POST['fname'];
$email = $_POST['email'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$passwordHash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 12));
//validate form here
$email_stmt = $pdo->prepare("SELECT email FROM user WHERE email = :email");
$email_stmt->bindParam(':email', $email);
$email_stmt->execute();
$result = $email_stmt->fetch(PDO::FETCH_ASSOC);
if (empty($result)) {
$stmt = $pdo->prepare('INSERT INTO user(fname,lname,gender,email,password)
VALUES (:fname, :lname, :gender, :email, :password)');
$stmt->bindValue(':fname',$fname);
$stmt->bindValue(':lname',$lname);
$stmt->bindValue(':gender',$gender);
$stmt->bindValue(':email',$email);
$stmt->bindValue(':password',$passwordHash);
if ($stmt->execute()) {
echo 'Registration Successful';
}
else {
echo 'FAILED';
}
}
else {
echo 'Email Already Exists. Use Different Email OR Login ';
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句