简体   繁体   中英

PHP & MySQL no inserting data into table (PDO)

I am having trouble getting a record to insert into my database, I have checked the code and all the variables and names match between PHP and the database.

There are no error messages and I am getting the text saying booking was created but, no record is entered into the database.

Here is the php code for inserting the record;

     <div class="containter">
      <?php
      if (isset($_POST['submit'])) {
         try {
             include ('include\PDO.php');
             $sql = "INSERT INTO customers(Customer_Name, Customer_Email, Customer_Contact) VALUES (:Customer_Name, :Customer_Email, :Customer_Contact)";

             //Named Parameters

             $stmt = $dbh->prepare($sql);

             if (!$stmt) {
                        echo "\nPDO::errorInfo():\n";
                        print_r($dbh->errorInfo());
                         }

             $Customer_Name = filter_input(INPUT_POST, 'Customer_Name');
             $stmt->bindValue(':Customer_Name', $Customer_Name, PDO::PARAM_STR);

             $Customer_Email = filter_input(INPUT_POST, 'Customer_Email');
             $stmt->bindValue(':Customer_Email', $Customer_Email, PDO::PARAM_STR);

             $Customer_Contact = filter_input(INPUT_POST, 'Customer_Contact');
             $stmt->bindValue(':Customer_Contact', $Customer_Contact, PDO::PARAM_STR);

             print $Customer_Contact;
             print $Customer_Name;
             print $Customer_Email;

             $stmt->execute();

             $dbh = null;


         } catch (PDOException $e) {
             //Error Messages
             print "We have had an error: " . $e->getMessage() . "<br/>";
             die();
         }         

     ?>

     <p> Booking Created.</p>

     <?php } else { ?>

     <form action ="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
         <label>Name:</label> <input type="text" name ="Cusomer_Name">
         <label>Email:</label> <input type="email" name ="Cusomer_Email">
         <label>Contact:</label> <input type="tel" name ="Cusomer_Contact">
         <input type="submit" name ="submit">
     </form>

     <?php } ?>
</div>
</body>
</html>

I have checked everything I can think of but I just cannot seem to get the records to add to the database.

Any ideas of what I'm doing wrong?

In the html form you have:

<label>Name:</label> <input type="text" name ="Cusomer_Name">

In the php code you filter Customer_Name:

$Customer_Name = filter_input(INPUT_POST, 'Customer_Name');

There is a missing 't' in the html form.

Put Customer_Name instead of Cusomer_Name in the html form and do the same thing for Cusomer_Email and Cusomer_Contact

$stmt->execute(); returns boolean with answer. Try following snippet to figure out where exactly problem is:

$success = $stmt->execute();
if (!$success){
    print $stmt->errorInfo()[2]; //PDO driver error message
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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