1

I'm not sure if it's cause i'm really tired or what; I can't seem to figure out why i can't insert into my database with this code.

<?php
require 'mysqlcon.php';
?>

<?php


if(isset($_POST["submit"])){
try {
$dbh = new PDO("mysql:host=$hostname;dbname=CSY2028",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO bookings (name, email)
VALUES ('".$_POST["name"]."','".$_POST["email"]."')";
if ($dbh->query($sql)) {
header("location: index.php");
echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";

}
else{
echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

}
?>

I have the following table i've checked that the ID's, names are all the same & matching. It's just not inserting anything?

<?php
require 'mysqlcon.php';
?>

        <!-- Main -->
            <section id="main" class="wrapper">
                <div class="container">
    <section>
                            <h3>Book Novelty Vehicle</h3>
                            <form action="bookingsql.php" method="post">
                                <div class="row uniform 50%">
                                    <div class="6u 12u$(xsmall)">
                                        <input type="text" name="name" id="name" value="name" placeholder="Name" />
                                    </div>
                                    <div class="6u$ 12u$(xsmall)">
                                        <input type="email" name="email" id="email" value="email" placeholder="Email" />
                                    </div>
                                    <div class="6u 12u$(xsmall)">
                                        <input type="text" name="address" id="address" value="" placeholder="Address" />
                                    </div>
                                    <div class="6u$ 12u$(xsmall)">
                                        <input type="text" name="pup" id="pup" value="" placeholder="Pick up point" />
                                    </div>                                  
                                    <div class="6u 12u$(xsmall)">
                                        <input type="text" name="phone" id="phone" value="" placeholder="Phone Number" />
                                    </div>

                                    <div class="6u$ 12u$(xsmall)">
                                    <input type="text" name="event" id="event" value="" placeholder="Event Type" />
                                    </div>

                                    <div class="12u$">
                                        <div class="select-wrapper">
                                            <select name="category" id="category">
                                                <option value="">- Novelty Vehicle Option -</option>
                                                <option value="1">One</option>
                                                <option value="1">Two</option>
                                                <option value="1">Three</option>
                                                <option value="1">Four</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="6u$ 12u$(small)">
                                        <input type="checkbox" id="human" name="human">
                                        <label for="human">You are aware this is for booking novelty vehicles only?</label>
                                    </div>
                                    <div class="12u$">
                                        <textarea name="info" id="info" placeholder="Please give us any additional information, this will help us speed up your booking process." rows="6"></textarea>
                                    </div>
                                    <div class="12u$">
                                        <ul class="actions">
                                            <li><input type="submit" value="Book Vehicle" class="special" /></li>
                                            <li><input type="reset" value="Reset" /></li>
                                        </ul>
                                    </div>
                                </div>
                            </form>
                        </section>

                </div>
            </section>
ScaperExe
  • 39
  • 6
  • 1
    It's hard to tell whether or not this is firing at all `if(isset($_POST["submit"])){...}` since you cut off your form after the email input/div. My guess; it's not firing due to not naming the submit and/or missing `` tag. Who knows, only you do. – Funk Forty Niner Jul 07 '16 at 02:05
  • 1
    *"I have the following table i've checked that the ID's, names are all the same & matching."* - Using the `id` attribute implies javascript/jquery/ajax. If you're using any of that and you didn't include it, then that's another piece of guesswork. If not, then you don't need `id`, unless you're using those with CSS. – Funk Forty Niner Jul 07 '16 at 02:09
  • I've edited my entire form into my post. – ScaperExe Jul 07 '16 at 02:10
  • 1
    there you go; you didn't name your submit button. Here, add `name="submit"` and your code will work. – Funk Forty Niner Jul 07 '16 at 02:11
  • SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 – ScaperExe Jul 07 '16 at 02:14
  • 1
    then that could mean that you may have an AI column, or something in your db may not accept NULL values. – Funk Forty Niner Jul 07 '16 at 02:15
  • 1
    Plus, you added a whole bunch of other code/inputs, but only using 2 columns/values. check your column names too. – Funk Forty Niner Jul 07 '16 at 02:16
  • Thank's Fred, i was only trying to insert two values to test & to figure out what was going wrong easier. I've implemented all of the others. Thank you for you're help :) – ScaperExe Jul 07 '16 at 02:22
  • You're quite welcome, glad to have been of help, *cheers* – Funk Forty Niner Jul 07 '16 at 02:34

1 Answers1

3

Unnamed submit

<input type="submit" value="Book Vehicle" class="special" />

and nothing inside if(isset($_POST["submit"])){...} will ever fire up.

So do:

<input name="submit" type="submit" value="Book Vehicle" class="special" />

Plus, since you're using PDO, use a prepared statement. You're open to an SQL injection.

References:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141