简体   繁体   English

HTML表单数据未使用PHP和PDO插入MySQL数据库

[英]html form data is not inserting in MySQL database using PHP and PDO

since i am new new here,i'm trying to be specific on my question. 由于我是新来的人,因此我想具体说明我的问题。 please tell me if you need more info. 请告诉我您是否需要更多信息。

Whenever i'm trying to submit the form it doesn't show any error regarding the code. 每当我尝试提交表单时,它都不会显示有关代码的任何错误。 it only show echo statement "FAILED" 它只显示回显语句“ FAILED”

i am using this loop to see if the values are submitted or not. 我正在使用此循环来查看是否已提交值。 this is working fine. 这很好。 it shows that all the values are submitted but these values are not inserting into database . 它显示所有值均已提交,但这些值未插入数据库中。

foreach ($_POST as $key => $value) {
    echo "($key) => ($value)<br/>";
}

my html form code is : 我的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>  

this is the php code that inserting everything into database 这是将所有内容插入数据库的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';
}

?>

any help would be appreciated. 任何帮助,将不胜感激。 Cheers. 干杯。

Looks like you don't need a foreach for this form. 看起来您不需要这种形式的foreach。 I think you should try for example: 我认为您应该尝试例如:

echo  $_POST['fname'];

http://php.net/manual/en/reserved.variables.post.php http://php.net/manual/en/reserved.variables.post.php

Also this name = "fname" should be this name="fname" 同样,此name = "fname"应为此name="fname"

You must put POST'S into variables. 您必须将POST放入变量中。 Try this 尝试这个

    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';
}

?>

you are checking if email taken after using insert 您正在检查是否在使用插入后收到电子邮件

try this 尝试这个

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM