SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;

时间:2017-02-21 02:03:42

标签: php mysql sql mamp

确切错误:

  

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以获得在''附近使用的正确语法。在第20行

我对SQL和PHP完全陌生,因此作为作业的一部分,我被要求编辑导师的代码并插入到我的项目中。但是,当我运行代码时,它会在第20行出现SQL语法错误(这是<body>标记打开的位置)。有人可以帮我查明错误吗?我对这些东西真的很陌生,如果这被视为&#34;粗鲁&#34;我很抱歉。或者如果我浪费你的时间&lt; 3.

<!DOCTYPE HTML>
<html>

<head>
    <title>"Sign Up" Form:</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <!-- Latest compiled and minified CSS -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>

<body>
    <div id="page-wrapper">
        <div class="wrapper style1">
            <section id="main" class="container">

                <?php
                    //-------------------------------------------------
                    $firstNameVal = 'Insert First Name';
                    $lastNameVal = 'Insert Surname';
                    $usernameVal = 'Choose a Username';
                    $passwordVal = '-------';
                    $emaiAddresslVal = 'Insert email';
                    $addressVal = 'Insert Address';
                    $suburbVal = 'Insert Suburb';
                    $ageVal = '17';

                    //-------------------------------------------------



                    //database credentials
                    define('DBHOST','localhost');
                    define('DBUSER','root');
                    define('DBPASS','root');
                    define('DBNAME','Login');

                    //new database connection
                    $db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
                    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                    //if form has been submitted process it
                    if(isset($_POST['submit'])){
                        // ???
                        $_POST = array_map( 'stripslashes', $_POST );

                        //collect form data
                        extract($_POST);

                        //very basic validation
                        if($firstName ==''){
                            $error[] = 'Please enter a first name.';
                        }
                        if($lastName ==''){
                            $error[] = 'Please enter a last name.';
                        }

                        if($username ==''){
                            $error[] = "Please enter a username";
                        }
                        if($password ==''){
                            $error[] = 'Please enter a password';
                        }
                        if($emailAddress ==''){
                            $error[] = 'Please enter an email address.';
                        }
                        if($address ==''){
                            $error[] = "Please enter your address";
                        }
                        if($suburb ==''){
                            $error[] = 'Please enter your suburb.';
                        }
                        if($mobile ==''){
                            $error[] = 'Please enter your personal mobile  number.';
                        }
                        if($age == ''){
                            $error[] = 'Please enter your age!';
                        }
                        if(!isset($error)){

                            try {

                                //insert into database
                                $query = $db->prepare('INSERT INTO user(
                                firstName, 
                                lastName, 
                                username,
                                password,
                                emailAddress,
                                address,
                                suburb,
                                age,
                                mobile) 
                                VALUES (
                                :firstName, 
                                :lastName, 
                                :username, 
                                :password, 
                                :emailAddress, 
                                :address, 
                                :suburb, 
                                :age, 
                                :mobile, 
                                ');
                                $query->execute(array(
                                    ':firstName' => $firstName, // processes the values for each form field
                                    ':lastName' => $lastName, //
                                    ':username' => $username, //
                                    ':password' => $password, //
                                    ':emailAddress' => $emailAddress, //
                                    ':address' => $address, //
                                    ':suburb' => $suburb, //
                                    ':age' => $age, //
                                    ':mobile' => $mobile, //
                                ));

                                //redirect to index page
                                header('Location: success.php');
                                exit;
                                // if error, it displays error
                            } catch(PDOException $e) {
                                echo $e->getMessage();
                            }
                        }
                    }
                    //check for any errors
                    if(isset($error)){
                        echo '<section id="content" class="box" style="background-color: #F4CDCD;">';
                            echo '<h3>Errors</h3>';
                            echo '<b><ul>';
                            foreach($error as $error){
                                echo '<li>'.$error.'</li>';
                            }
                            echo '</ul></b>';
                        echo '</section>';
                    }
                ?>

                    <form method="post">

                        <!-- Your Details -->
                        <section id="content" class="box">
                            <h3>Your Details</h3>
                            <div class="row uniform 60%">
                                <div class="6u 12u(narrower)">
                                    <label for="firstName">First Name</label>
                                    <input type="text" name="firstName" id="firstName" value="<?php if(isset($error)){ echo $_POST['firstName'];}?>" placeholder="First Name" />
                                </div>
                                <div class="6u 12u(narrower)">
                                    <label for="lastName">Last Name</label>
                                    <input type="text" name="lastName" id="lastName" value="<?php if(isset($error)){ echo $_POST['lastName'];}?>" placeholder="Last Name" />
                                </div>
                            </div>

                            <div class="row uniform 60%">
                                <div class="6u 12u(narrower)">
                                    <label for="username">username</label>
                                    <input type="text" name="username" id="username" value="<?php if(isset($error)){ echo $_POST['username'];}?>" placeholder="Userame" />
                                </div> 
                                <div class="6u 12u(narrower)">
                                    <label for="password">password</label>
                                    <input type="password" name="password" id="password" value="<?php if(isset($error)){ echo $_POST['password'];}?>" placeholder="Password" />
                                </div>
                            </div>
                            <div class="row uniform 60%">

                                <div class="6u 12u(narrower)">
                                    <label for="emailAddress">Email</label>
                                    <input type="email" name="emailAddress" id="emailAddress" value="<?php if(isset($error)){ echo $_POST['emailAddress'];}?>" placeholder="jane.doe@compuhyperglobalmeganet.com" />
                                </div>
                                <div class="6u 12u(narrower)">
                                    <label for="mobile">Mobile</label>
                                    <input type="text" name="mobile" id="mobile" value="<?php if(isset($error)){ echo $_POST['mobile'];}?>" placeholder="1234 567 890" />
                                </div>
                                <div class="6u 12u(narrower)">
                                    <label for="age">Age</label>
                                    <input type="text" name="age" id="age" value="<?php if(isset($error)){ echo $_POST['age'];}?>" placeholder="17" />
                                </div>
                            </div>
                            <div class="row uniform 60%">

                                <div class="6u 12u(narrower)">
                                    <label for="address">Address</label>
                                    <input type="text" name="address" id="address" value="<?php if(isset($error)){ echo $_POST['Address'];}?>" placeholder="21B Baker Street" />
                                </div>
                                <div class="6u 12u(narrower)">
                                    <label for="suburb">Mobile</label>
                                    <input type="text" name="suburb" id="suburb" value="<?php if(isset($error)){ echo $_POST['suburb'];}?>" placeholder="marylebone" />
                                </div>

                            </div>
                        </section>

                        <br>
                        <!-- Submit Button -->
                        <section id="content" class="box">
                            <div class="row uniform 60%">
                                <div class="12u">
                                    <ul class="actions">
                                        <li style="width: 100%; list-style: none; ">
                                            <input type="submit" name="submit" value="Finish and Submit" style="width: 100%;" />
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </section>

                    </form>

            </section>
        </div>

    </div>
<!-- Scripts -->
</body>

</html>

1 个答案:

答案 0 :(得分:0)

请从SQL语句中删除最后一个,逗号,然后重试。这意味着改变这个:

//insert into database
$query = $db->prepare('INSERT INTO user(
firstName, 
lastName, 
username,
password,
emailAddress,
address,
suburb,
age,
mobile) 
VALUES (
:firstName, 
:lastName, 
:username, 
:password, 
:emailAddress, 
:address, 
:suburb, 
:age, 
:mobile, 
');

到此:

//insert into database
$query = $db->prepare('INSERT INTO user(
firstName, 
lastName, 
username,
password,
emailAddress,
address,
suburb,
age,
mobile) 
VALUES (
:firstName, 
:lastName, 
:username, 
:password, 
:emailAddress, 
:address, 
:suburb, 
:age, 
:mobile
');