Search code examples
phpmysqlsqlmamp

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;


Exact Error:

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 20

I'm completely new to SQL and PHP, so as part of an assignment I was asked to edit a mentor's code and insert into my project. However, when I run the code, it comes up with an SQL syntax error at line 20 (which is where the <body> tag opens). Could someone please help me pinpoint the error? I'm really new to this stuff, and I'm sorry in advance if this is seen as "rude" or if I'm wasting your time <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>

Solution

  • Please remove the last , comma from the SQL statement and try again. That means change this:

    //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, 
    ');
    

    to this:

    //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
    ');