插入数据库问题pdo

时间:2016-07-07 02:01:21

标签: php sql pdo

我不确定是不是因为我真的很累或什么;我似乎无法弄清楚为什么我不能使用此代码插入我的数据库。

<?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();
}

}
?>

我有下表我已经检查过ID,名字都是一样的&amp;匹配。它只是没有插入任何东西?

<?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>

1 个答案:

答案 0 :(得分:5)

未命名提交

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

if(isset($_POST["submit"])){...}内的任何内容都不会启动。

所以:

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

另外,由于您正在使用PDO,请使用预准备语句。您可以使用SQL注入。

参考文献: