插入不起作用,准备,绑定和执行,但不起作用

时间:2014-07-11 21:21:18

标签: php mysql sql prepared-statement execute

嘿伙计们,我在pp.php文件中有这段代码

<?php
class system{
public function register() {

        if ($_POST) {

            if ($_POST['password'] == $_POST['reppassword']) {
                if ($_POST['email'] == $_POST['repemail']) {

                    $logdb = new PDO('mysql:host=localhost;dbname=kikojust', 'kikojust', '123456');
                    $logdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $stmt = $logdb->prepare('INSERT INTO usreg
                                    (email, password, firstname, lastname, phone, mobile, adress, city, postalcode, country)
                                    VALUES
                                    (:email, :pass, :fname, :lname, :phone, :mobile, :adress, :city, :code, :country');
                    $stmt->bindParam(":email", $_POST['email'],PDO::PARAM_STR, 20);
                    $stmt->bindParam(":pass", $_POST['password'],PDO::PARAM_STR, 20);
                    $stmt->bindParam(":fname", $_POST['firstname'],PDO::PARAM_STR, 20);
                    $stmt->bindParam(":lname", $_POST['lastname'],PDO::PARAM_STR, 20);
                    $stmt->bindParam(":phone", $_POST['phone'],PDO::PARAM_STR, 16);
                    $stmt->bindParam(":mobile", $_POST['mobile'],PDO::PARAM_STR, 16);
                    $stmt->bindParam(":adress", $_POST['adress'],PDO::PARAM_STR, 50);
                    $stmt->bindParam(":city", $_POST['city'],PDO::PARAM_STR, 10);
                    $stmt->bindParam(":code", $_POST['postalcode'],PDO::PARAM_STR, 10);
                    $stmt->bindParam(":country", $_POST['country'],PDO::PARAM_STR, 14);
                    $stmt->execute();

                    echo 'You have being well donne registered, go to <a href="index.php">Login</a> to enter in our site';
                } else {
                    echo 'Go back to <a href="register.php">Register</a> and check your Email';
                }
            } else {
                echo 'Go back to <a href="register.php">Register</a> and check your password.';
            }
        } else {
            echo '
                <html>
                    <head>
                        <title>
                            Register!
                        </title>
                    </head>
                    <body>
                        <form name="login" action="" method="POST">
                            First Name:
                            <input type="text" name="firstname"/>
                            Last Name:
                            <input type="text" name="lastname"/>
                            <br />
                            Password: 
                            <input type="password" name="password"/>
                            Repeat Password: 
                            <input type="password" name="reppassword"/><br />
                            <br />
                            Email: 
                            <input type="text" name="email"/>
                            Repeat Email: 
                            <input type="text" name="repemail"/><br />
                            <br />
                            Phone: 
                            <input type="tel" name="phone"/>
                            Mobile: 
                            <input type="tel" name="mobile"/><br />
                            <br />
                            Adress: <br />
                            <input type="text" name="adress"/><br />
                            City: <br />
                            <input type="text" name="city"/><br />
                            Postal Code: <br />
                            <input type="text" name="postalcode"/><br />
                            Country: <br />
                            <input type="text" name="country"/><br />
                            <button type="submit">Register</button>
                        </form>
                    </body>
                </html>';
        }
    }
}

我在register2.php

中有这个
<?php
require_once 'Inc/pp.php';
                $login = new system;
                $login->register();

并且通过检查和邮件检查工作正常,一切看起来很好,直到它转到插入的部分,它给了我那个错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4' in
/home/kikojust/public_html/Inc/pp.php:26 Stack trace: #0 
/home/kikojust/public_html/Inc/pp.php(26): PDOStatement->execute() #1 
/home/kikojust/public_html/register2.php(4): system->register() #2 {main} thrown in 
/home/kikojust/public_html/Inc/pp.php on line 26

但是我没有得到什么问题,因为stmt句子准备好并且绑定但是没有运行,所有的声明都在表格中相同,如果我在sql控制台中运行相同的sql它很顺利,什么我的代码是否失败你们可以告诉我吗? THKS

2 个答案:

答案 0 :(得分:1)

您缺少SQL查询的括号。更新查询行,如下所示。

$stmt = $logdb->prepare('INSERT INTO usreg
                                    (email, password, firstname, lastname, phone, mobile, adress, city, postalcode, country)
                                    VALUES
                                    (:email, :pass, :fname, :lname, :phone, :mobile, :adress, :city, :code, :country);');

答案 1 :(得分:1)

由于语法错误,您的准备工作失败了:'

  $logdb->prepare(".....  (:email,..snip..., :country');
                                                     ^--missing )
                 1        2                           1

注意括号的编号。您忘记关闭VALUES列表的),但已关闭()准备值。它应该是

 ...., :country)');