duandun3178 2016-07-07 02:01
浏览 77
已采纳

插入数据库问题pdo

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>
  • 写回答

1条回答 默认 最新

  • drk49438 2016-07-07 02:13
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿