0

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?

6
  • 1
    I believe that you need to use $_POST['Customer_name']; etc... rather than just $Customer_name as I cannot see that declared anywhere in that code. (Same for all) - Please expand and give us your error Commented Feb 10, 2016 at 16:44
  • 1
    @KyleE4K ... it's in the line directly above the bindValue, e.g. $Customer_Name = filter_input(INPUT_POST, 'Customer_Name'); Commented Feb 10, 2016 at 16:46
  • try one thing for me, call your bind param's something without an _ in. I remember once this being an issue I had and having a _ between it caused no error but it not to work. Just give that ago Commented Feb 10, 2016 at 16:49
  • 2
    Actually - from what's there, it's a simple typo : <input type="text" name ="Cusomer_Name"> <- note the lack of a t in customer. Commented Feb 10, 2016 at 16:52
  • haha I spent hours looking at this, I didn't realize the typo, this did not fix the issue though Commented Feb 10, 2016 at 16:57

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Incredible :) Thumbs Up
Thanks, I spent hours looking at this and never notice this at all, I changed it anyway, but this never fixed it
Got it working, once I fixed the typos and then removed the _ from the column names and rewrote the bind params I finally got it to insert to the table, thanks for the help everybody
0

$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
}

2 Comments

Ahh I see it now, error is Column 'Customer_Name' cannot be null I will have to check and see why only that column is causing an issue, the code for the other 2 is exactly the same, apart from the names of course
Fix all typos in html form noted by Emiliano and try again, this should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.