不写入db

时间:2013-11-21 18:35:36

标签: php mysql

我来过这里很多次了。我无法获取信息发布到数据库。有人可以向我解释我做错了什么,以便我可以解决这个问题吗?

我在使用E_All和E_strict的本地主机上运行代码,唯一出现的是未定义的变量。不是那些应该发布的,而是用户名/ pass / db名称。

首先是我正在使用的表单,第二个是php。

<form method="post" action="hg.php">
            <div id="box">
            <font size="1px">
            Winner must be 18 years or older and have a valid email address. Drawings will be held 12/31/2013. If for any reason either winners do not claim prize within 30 days, that winner will be null and void and we will draw again on 01/31/14 for that winners prize choices. If no winner claims their prize within 30 days from 01/31/14, all drawings and winners will be 
            forfeit except for the already claimed prizes if any.
            </font></div>

            <div class="clear"> </div>
            <div id="box">
            <label for="textfield" style="margin-top:15px;">First Name</label> 
            <input id="textfield" type="text" name="first" />
            </div>

            <div id="box">
            <label for="textfield" style="margin-top:15px;">Last Name</label> 
            <input id="textfield" type="text" name="last" />
            </div>

            <div class="clear"> </div>

            <div id="box">
            <label for="textfield">Phone</label> 
            <input id="textfield" type="text" size="13" maxlength="13" name="contact" />
            </div>

            <div id="box">
            <label for="textfield" style="margin-left:15px;">Email</label> 
            <input id="textfield" type="text" name="email" style="margin-left:15px;"/>
            </div>

            <div class="clear"> </div>

            <div id="box">
            <label for="textfield">Date of Birth</label> 
            <input id="textfield" type="text" name="dob" />
            </div>
            <div class="clear"> </div>

            <div id="box"><input type="submit" value="Register" id="submit"></div>
            </form>

php

<!DOCTYPE html>
            <?php
            $first = $_POST['first'];
            $last= $_POST['last'];
            $contact = $_POST['contact'];
            $email = $_POST['email'];
            $dob = $_POST['dob'];

            $host = 'localhost';
            $db_name= 'rebeler_email';
            $db_username = 'rebeler_email';
            $db_password = 'callaway87';



            if(isset($_POST['submit'])) {
                $pdo = new PDO('mysql:host='.$host.';dbname='.$db_name, $db_username, $db_password);
                $statement = $pdo->prepare('
                    INSERT INTO `email`(
                        `email`,
                        `first`,
                        `last`,
                        `contact`,
                        `dob`
                    ) VALUES (
                        :email,
                        :first,
                        :last,
                        :contact,
                        :dob
                    )
                ');
                $result->execute('array(
                    `email`=>$_POST[`email`],
                    `first`=>$_POST[`first`],
                    `last`=>$_POST[`last`],
                    `contact`=>$_POST[`contact`],
                    `dob`=>$_POST[`dob`]
                )');                    $email_id = $pdo->lastInsertId();
                if (!result || !$customer_id) {
                    var_dump($pdo->errorInfo());
                    die('something went wrong'); // do something better to handle errors!
                }
            }


            ?>

            <html>

            <head>
            <link rel="stylesheet" type="text/css" href="css.css">

            <style type="text/css">

                #hgtitle{
                    height: 50px;
                    width: 300px;

                }
            </style>

            </head>


            <body>

            <a href="new.html"><img src="images/hgtitle.jpg" id="hgtitle"></a>

            <div id="box">
            Thank you for registering.
            </div>

            </body>


            </html>

2 个答案:

答案 0 :(得分:1)

请注意突出显示的变量:

            $statement = $pdo->prepare(' etc... ');
            ^^^^^^^^^^^

v.s。

            $result->execute(array( etc... ));
            ^^^^^^^

如果你的代码中有正确的错误处理,包括启用了error_reporting和display_errors,你就会看到第二行产生的“调用非对象的方法”错误。

这些设置永远不会在您的开发服务器上关闭,因为它们仅用于隐藏您应该立即看到/修复的错误。

答案 1 :(得分:1)

我自己有点像菜鸟,但我不确定为什么你在数据库的变量上使用POST全局变量。

我只是这样设置:

$host = localhost;
$db_name= rebeler_email;
$db_username = rebeler_email;
$db_password = callaway87;

因为你没有从表格中获取它们