我在将PHP数据保存到MySQL数据库时遇到问题

时间:2015-12-08 17:40:52

标签: php mysql sql pdo

我的.php页面已成功连接到mySql数据库。它可以看到表格并从表格中拉出,但不会将数据从我的php表格中的文本框保存到数据库。

config.php

     <?php


    try{
        $db = new PDO("mysql:host=localhost;dbname=nolarec;port=3307","root","");
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        $db->exec("SET NAMES 'utf8'");
} catch(Exception $e) {
        echo $e->getMessage();
        exit;
}

?>

fball_event.php

        <form method="post" action="fball_create.php">
            <input type="hidden" name="submit" value="true">
                    <fieldset>
                    <legend>New Event</legend>
                     Id:   <input type="text" name="id"/>   <br/>
                     Name: <input type="text" name="name"/> <br/>
                     Time: <input type="text" name="time"/> <br/>
                     Type: <input type="text" name="type"/> <br/>
                    </fieldset>
                    <br />
            <input type="submit" value="Create New Event" />
        </form>



            <?php 

            require_once('config.php');


            if (isset($_POST['submit'])){

                include ('config.php');



                $id = $_POST['id'];
                $name = $_POST['name'];
                $time = $_POST['time'];
                $type = $_POST['type'];
                $results = $db->prepare ("INSERT INTO nolarec.fball_event (id, name, time, type) VALUES ('$id','$name','$time','$type')");




            }
            ?>

1 个答案:

答案 0 :(得分:1)

首先,您应该在查询中使用占位符作为数据输入,其次,您需要实际执行它,您刚刚准备好它。尝试:

$id = $_POST['id'];
$name = $_POST['name'];
$time = $_POST['time'];
$type = $_POST['type'];

$results = $db->prepare ("INSERT INTO nolarec.fball_event (id, name, time, type) VALUES (:id,:name,:time,:type)");

$results->bindValue(":id", $id);
$results->bindValue(":name", $name);
$results->bindValue(":time", $time);
$results->bindValue(":type", $type);
$results->execute();