Jump to content
Larry Ullman's Book Forums

Recommended Posts

Parse error: syntax error, unexpected '[' in /data/6/0/142/65/468717/user/register.php on line 10

This code is in script 9.3,  page 277, line 10 

The book is Visual QuickPro Guide PHP and MySQL for Dynamic Web Sites

Edited by SledgeDB
Link to comment
Share on other sites

You have a parse error in your script. This is normally caused by missing a matching parentheses, quote, bracket, or the like. It's possible it's on line 10 or, if not, then on a previous line but not triggered until line 10. If you need additional help identifying the problem, please post the actual code you're using.

Link to comment
Share on other sites

This code is actually the  exact code from the book. I only change the path of the header and footer to point to the correct directory.

 

 

<?php # Script 9.3 - register.php
// This script performs an INSERT query to add a record to the users table.

$page_title = 'Register';
include('header.html');

// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $errors = []; // Initialize an error array.

    // Check for a first name:
    if (empty($_POST['first_name'])) {
        $errors[] = 'You forgot to enter your first name.';
    } else {
        $fn = trim($_POST['first_name']);
    }

    // Check for a last name:
    if (empty($_POST['last_name'])) {
        $errors[] = 'You forgot to enter your last name.';
    } else {
        $ln = trim($_POST['last_name']);
    }

    // Check for an email address:
    if (empty($_POST['email'])) {
        $errors[] = 'You forgot to enter your email address.';
    } else {
        $e = trim($_POST['email']);
    }

    // Check for a password and match against the confirmed password:
    if (!empty($_POST['pass1'])) {
        if ($_POST['pass1'] != $_POST['pass2']) {
            $errors[] = 'Your password did not match the confirmed password.';
        } else {
            $p = trim($_POST['pass1']);
        }
    } else {
        $errors[] = 'You forgot to enter your password.';
    }

    if (empty($errors)) { // If everything's OK.

        // Register the user in the database...

        require('../mysqli_connect.php'); // Connect to the db.

        // Make the query:
        $q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA2('$p', 512), NOW() )";
        $r = @mysqli_query($dbc, $q); // Run the query.
        if ($r) { // If it ran OK.

            // Print a message:
            echo '<h1>Thank you!</h1>
        <p>You are now registered. In Chapter 12 you will actually be able to log in!</p><p><br></p>';

        } else { // If it did not run OK.

            // Public message:
            echo '<h1>System Error</h1>
            <p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';

            // Debugging message:
            echo '<p>' . mysqli_error($dbc) . '<br><br>Query: ' . $q . '</p>';

        } // End of if ($r) IF.

        mysqli_close($dbc); // Close the database connection.

        // Include the footer and quit the script:
        include('includes/footer.html');
        exit();

    } else { // Report the errors.

        echo '<h1>Error!</h1>
        <p class="error">The following error(s) occurred:<br>';
        foreach ($errors as $msg) { // Print each error.
            echo " - $msg<br>\n";
        }
        echo '</p><p>Please try again.</p><p><br></p>';

    } // End of if (empty($errors)) IF.

} // End of the main Submit conditional.
?>
<h1>Register</h1>
<form action="register.php" method="post">
    <p>First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>"></p>
    <p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>"></p>
    <p>Email Address: <input type="email" name="email" size="20" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" > </p>
    <p>Password: <input type="password" name="pass1" size="10" maxlength="20" value="<?php if (isset($_POST['pass1'])) echo $_POST['pass1']; ?>" ></p>
    <p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" value="<?php if (isset($_POST['pass2'])) echo $_POST['pass2']; ?>" ></p>
    <p><input type="submit" name="submit" value="Register"></p>
</form>
<?php include('footer.html'); ?>

 

Link to comment
Share on other sites

Hmmm... I don't see anything obviously wrong. That normally means there's a gremlin character in there, like a straight quote that's actually curly or the like. These aren't as easy to debug. I'd try retyping or copying-pasting the first few lines (from a clean source). Otherwise you'll need to debug backwards to find the exact line causing the problem. For example, if you remove line 10 (which you can do here without causing a new parse error), does the current error go away?

Link to comment
Share on other sites

Larry this is the code from your book/your website. Are you sure the code actually works? I Downloaded it from the companion Website LarryUllman.com. Maybe you could email you version of this code on page 277in script 9.3 that works for Frank@SledgeDB.com. Thank you. 

Link to comment
Share on other sites

Yes, the code in the book does work. I used it while writing the book and for all the images I took. I'm a bit confused by what you're saying in this last post, though. I just downloaded the script from the site and ran it again, unedited. Here is the initial load:

Screen Shot 2019-09-20 at 5.28.45 PM.png

And after clicking "Register" without entering anything:

Screen Shot 2019-09-20 at 5.28.50 PM.png

I'm not seeing a parse or other error. 

Link to comment
Share on other sites

Yes, the code in the book does work. I used it while writing the book and for all the images I took. I'm a bit confused by what you're saying in this last post, though. I just downloaded the script from the site and ran it again, unedited. Here is the initial load:

Screen Shot 2019-09-20 at 5.28.45 PM.png

And after clicking "Register" without entering anything:

Screen Shot 2019-09-20 at 5.28.50 PM.png

I'm not seeing a parse or other error. 

Link to comment
Share on other sites

I would also double-check that you're running/editing the file you think you are (this chapter has multiple versions of register.php). And if it's still not obvious what's going on, please confirm your PHP version and OS. Oh, and when mentioning errors it's best to include the specific error that you're seeing. Thanks!

Link to comment
Share on other sites

Please email me the initial code that you are using above to Frank@SledgeDB.com   i.e.  (I just downloaded the script from the site and ran it again, unedited. Here is the initial load).

 

I do understand that most technical book writers do not have working code in their books. I understand that most code snipping in books is for illustration purposes only. It would be too cumbersome to include the details of every variable, every flag, every include, every function, etc in code that is designed to illustration how to code.  

As you know, a single program can contain as many lines of code as all 300+ pages of your book(s).

 

Link to comment
Share on other sites

I can email you the code but it'd be the exact same file downloaded from here: https://github.com/LarryUllman/phpmysqlvqp-5ed/blob/master/ch09/script_09_03/register.php 

What happens when you run this? Do you see any error messages? Your last two posts don't include any details. 

I can't speak for other writers, but the code in my books is generally not for illustration purposes. It worked when I wrote the book! However, code is not like a light bulb that can be screwed in in any socket (and even light bulbs vary quite a bit). Code runs within an environment; code that worked on one environment may not work on another, although PHP works across most environments well. You haven't yet said what version of PHP and OS you're using, which may shed some light on the issue. 

As a database expert, I'm sure you can appreciate this. I could do a MySQL 5.3 dump of a database that can't be imported into a MySQL 5.7 database, let alone a MySQL 8. That doesn't mean the dump was invalid, just that it won't work as-is for all situations. 

Link to comment
Share on other sites

On 9/23/2019 at 9:53 AM, Larry said:

I can email you the code but it'd be the exact same file downloaded from here: https://github.com/LarryUllman/phpmysqlvqp-5ed/blob/master/ch09/script_09_03/register.php 

What happens when you run this? Do you see any error messages? Your last two posts don't include any details. 

I can't speak for other writers, but the code in my books is generally not for illustration purposes. It worked when I wrote the book! However, code is not like a light bulb that can be screwed in in any socket (and even light bulbs vary quite a bit). Code runs within an environment; code that worked on one environment may not work on another, although PHP works across most environments well. You haven't yet said what version of PHP and OS you're using, which may shed some light on the issue. 

As a database expert, I'm sure you can appreciate this. I could do a MySQL 5.3 dump of a database that can't be imported into a MySQL 5.7 database, let alone a MySQL 8. That doesn't mean the dump was invalid, just that it won't work as-is for all situations. 

Please email me the initial code that you are using above to Frank@SledgeDB.com   i.e.  (I just downloaded the script from the site and ran it again, unedited. Here is the initial load).

 

I do understand that most technical book writers do not have working code in their books. I understand that most code snipping in books is for illustration purposes only. It would be too cumbersome to include the details of every variable, every flag, every include, every function, etc in code that is designed to illustration how to code.  

As you know, a single program can contain as many lines of code as all 300+ pages of your book(s).

 

Link to comment
Share on other sites

I am thinking of abandoning Php /  My SQL and moving forward with Django, Python, Mongo! Django Web framework provides all the user authentication and more. There are no SQL injection problems with MongoDB. I already know both some Python and I know some MongoDB. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...