[英]html form data is not inserting in MySQL database using PHP and PDO
由于我是新来的人,因此我想具体说明我的问题。 请告诉我您是否需要更多信息。
每当我尝试提交表单时,它都不会显示有关代码的任何错误。 它只显示回显语句“ FAILED”
我正在使用此循环来查看是否已提交值。 这很好。 它显示所有值均已提交,但这些值未插入数据库中。
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';
}
?>
任何帮助,将不胜感激。 干杯。
看起来您不需要这种形式的foreach。 我认为您应该尝试例如:
echo $_POST['fname'];
http://php.net/manual/en/reserved.variables.post.php
同样,此name = "fname"
应为此name="fname"
您必须将POST放入变量中。 尝试这个
require('connect.php');
$fname = $lname = $gender = $email = $password = "";
if(isset($_POST['register'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$password = $_POST['password'];
$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 ';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.