Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
jayLim

Chapter 5 Syntax Error

Recommended Posts

Hello Forum

In Chapter 5 Creating the Basic Script, the add_page.php file, when testing out I get this message:

 

Parse error: syntax error, unexpected '{' in /Users/jlim/Website/effortless_ecommerce_2nd/ex1_exercise/html/add_page.php on line 70

 

I've also tried the original add_page.php file and I also get the same exact message. These are the ones I wrote:

 

...

 

<h1>Add a Site Content Page</h1>
<form action="add_page.php" method="post" accept-charset="utf-8">
<fieldset><legend>Fill out the form to add a page of content:</legend>
<?php
echo '<div class="form-group';
if(array_key_exists('category', $add_page_errors)) echo ' has-error';
echo '"><label for="category" class="control-label">Category</label>
<select name="category" class="form-control">
<option>Select One</option>';
 
$q = "SELECT id, category FROM categories ORDER BY category ASC";
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_array($r, MYSQLI_NUM) {
echo "<option value=\"$row[0]\"";
if(isset($_POST['category']) && ($_POST['category'] == $row[0])) echo ' selected="selected"';
echo ">$row[1]</option>\n";
}
echo '</select>';
if(array_key_exists('category', $add_page_errors)) echo '<span class="help-block">' . $add_page_errors['category'] . '</span>';
echo '</div>';
 
create_form_input('description', 'textarea', 'Description', $add_page_errors);
create_form_input('content', 'textarea', 'Content', $add_page_errors);
?>
<input type="submit" name="submit_button" value="Add This Page" id="submit_button" class="btn btn-default">
 
</fieldset>
</form>
<?php 
include('./includes/footer.html'); 
?>

Share this post


Link to post
Share on other sites

Your if-statements probably needs brackets. Hard to determine by watching code on this forum, but you need brackets unless your following commands are single-line. The echo statement makes them span more.

 

I suggest an IDE like Netbeans to automaticly catch these errors, btw.

Share this post


Link to post
Share on other sites


<?php


require('./includes/config.inc.php');


redirect_invalid_user('user_admin');


require(MYSQL);


$page_title = 'Add a Site Content Page';
include('./includes/header.html');


$add_page_errors = array();


if($_SERVER['REQUEST_METHOD'] === 'POST') {


if(!empty($_POST['title'])) {
$t = ecape_data(strip_tags($_POST['title']), $dbc);
} else {
$add_page_errors['title'] = 'Please enter thre title!';
}


if(filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
$cat = $_POST['category'];
} else {// No category selected.
$add_page_errors['category'] = 'Please select a category!';
}


if(!empty($_POST['description'])) {
$d = escape_data(strip_tags($_POST['description']), $dbc);
} else {
$add_page_errors['description'] = 'Please select the description!';
}


if(!empty($_POST['content'])) {
$allowed = '<div><p><span><br><a><img><h1><h2><h3><h4><ul><ol><li><blockquote>';
$c = escape_data(strip_tags($_POST['content'], $allowed), $dbc);
} else {
$add_page_errors['content'] = 'Please enter the content!';
}


if(empty($add_page_errors)) {
$q = "INSERT INTO pages(categories_id, title, description, content) VALUES($cat, '$t', '$d', '$c')";
$r = mysqli_query($dbc, $q);


if(mysqli_affected_rows($dbc) === 1) {
echo '<div class="alert alert-success"><h3>The page has been added!</h3></div>';


$_POST = array();


} else {//If it did not run ok.
trigger_error('The page could not be added due to a system error. We apologize for any inconvenience.');
}
}//End of $add_page_errors if.
} //End fothe main form submission conditional.


require('includes/form_functions.inc.php');
?>
<h1>Add a Site Content Page</h1>
<form action="add_page.php" method="post" accept-charset="utf-8">
<fieldset><legend>Fill out the form to add a page of content:</legend>
<?php
create_form_input('title', 'text', 'Title', $add_page_errors); 


echo '<div class="form-group';
if(array_key_exists('category', $add_page_errors)) echo ' has-error';
echo '"><label for="category" class="control-label">Category</label>
<select name="category" class="form-control">
<option>Select One</option>';


$q = "SELECT id, category FROM categories ORDER BY category ASC";
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_array($r, MYSQLI_NUM) {
echo "<option value=\"$row[0]\"";
if(isset($_POST['category']) && ($_POST['category'] == $row[0])) echo ' selected="selected"';
echo ">$row[1]</option>\n";
}
echo '</select>';
if(array_key_exists('category', $add_page_errors)) echo '<span class="help-block">' . $add_page_errors['category'] . '</span>';
echo '</div>';


create_form_input('description', 'textarea', 'Description', $add_page_errors);
create_form_input('content', 'textarea', 'Content', $add_page_errors);
?>
<input type="submit" name="submit_button" value="Add This Page" id="submit_button" class="btn btn-default">


</fieldset>
</form>
<?php 
include('./includes/footer.html'); 
?>

 

Share this post


Link to post
Share on other sites

I'm not seeing it. I even copied the code into my editor and didn't see any syntactical problems. I wonder if you're viewing/executing/referencing the right file?

Share this post


Link to post
Share on other sites

Hello JayLim,

 

I work on this book and i haven't problem with the code and the example code.

 

look the eventuel problem sources :

 

application/logiciel:

if you are "copié" the original code, sometimes an error would be come to your text - editor.

Test the same code with TextWrangler or textedit (in utf-8)  and see if no bizarre sign appear in code

 

outside source :

See also yours file "form_functions.inc.php", an error in the function?

 

cordially

Share this post


Link to post
Share on other sites

Ok, I spotted the problem! 

By carefully going over the codes I realized I had only one parenthesis in the while loop: 

 

while($row = mysqli_fetch_array($r, MYSQLI_NUM)  {

 

the code should go like this :

 

while($row = mysqli_fetch_array($r, MYSQLI_NUM) ) {

 

it's all about details!

thanks everyone!

 

J.

Share this post


Link to post
Share on other sites

I am also experiencing a slightly different problem with the add_page.php file. For some reason, when I attempt to submit a page it's issusing me the following statement Please select a category!

The problem is there are no categories to select other that the word select. So essencially, nothing is getting submitted to my data base and I have attempt to seach the code for any problem without any success.

 

Here is what my code currently looks like:

 

// This page is used by an administrator to create a specific page of HTML content.
// This script is created in Chapter 5.

// Require the configuration before any PHP code as the configuration controls error reporting:
require('./includes/config.inc.php');

// If the user isn't logged in as an administrator, redirect them:
redirect_invalid_user('user_admin');

// Require the database connection:
require('./includes/mysql.inc.php');

// Include the header file:
$page_title = 'Add a Site Content Page';
include('./includes/header.html');

// For storing errors:
$add_page_errors = array();

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {    
    
    // Check for a title:
    if (!empty($_POST['title'])) {
        $t = escape_data(strip_tags($_POST['title']), $dbc);
    } else {
        $add_page_errors['title'] = 'Please enter the title!';
    }
    
    // Check for a category:
    if (filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
        $cat = $_POST['category'];
    } else { // No category selected.
        $add_page_errors['category'] = 'Please select a category!';
    }

    // Check for a description:
    if (!empty($_POST['description'])) {
        $d = escape_data(strip_tags($_POST['description']), $dbc);
    } else {
        $add_page_errors['description'] = 'Please enter the description!';
    }
        
    // Check for the content:
    if (!empty($_POST['content'])) {
        $allowed = '<div><p><span><br><a><img><h1><h2><h3><h4><ul><ol><li><blockquote>';
        $c = escape_data(strip_tags($_POST['content'], $allowed), $dbc);
    } else {
        $add_page_errors['content'] = 'Please enter the content!';
    }
        
    if (empty($add_page_errors)) { // If everything's OK.

        // Add the page to the database:
        $q = "INSERT INTO pages (categories_id, title, description, content) VALUES ($cat, '$t', '$d', '$c')";
        $r = mysqli_query($dbc, $q);

        if (mysqli_affected_rows($dbc) === 1) { // If it ran OK.
    
            // Print a message:
            echo '<div class="alert alert-success"><h3>The page has been added!</h3></div>';
            
            // Clear $_POST:
            $_POST = array();
            
            // Send an email to the administrator to let them know new content was added?
            
        } else { // If it did not run OK.
            trigger_error('The page could not be added due to a system error. We apologize for any inconvenience.');
        }
        
    } // End of $add_page_errors IF.
    
} // End of the main form submission conditional.

// Need the form functions script, which defines create_form_input():
require('includes/form_functions.inc.php');
?>
<h1>Add a Site Content Page</h1>
<form action="add_page.php" method="post" accept-charset="utf-8">

    <fieldset><legend>Fill out the form to add a page of content:</legend>
<div class="form-group">
<label for="status" class="control-label">Status</label>
<select name="status" class="form-control"><option value="draft">Draft</option>
<option value="live">Live</option>
</select></div>

<?php
create_form_input('title', 'text', 'Title', $add_page_errors);

// Add the category drop down menu:
echo '<div class="form-group';
if (array_key_exists('category', $add_page_errors)) echo ' has-error';


echo '"><label for="category" class="control-label">Category</label>
<select name="category" class="form-control">
<option>Select One</option>';
// Bonus material!
// Added in Chapter 12.
// Allow for multiple categories:
echo '"><label for="category" class="control-label">Category</label>
<select name="category[]" class="form-control" multiple size="5">';

// Retrieve all the categories and add to the pull-down menu:
$q = "SELECT id, category FROM categories ORDER BY category ASC";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
    echo "<option value=\"$row[0]\"";
    // Check for stickyness:
    if (isset($_POST['category']) && ($_POST['category'] == $row[0]) ) echo ' selected="selected"';
    echo ">$row[1]</option>\n";
}

echo '</select>';
if (array_key_exists('category', $add_page_errors)) echo '<span class="help-block">' . $add_page_errors['category'] . '</span>';
echo '</div>';

create_form_input('description', 'textarea', 'Description', $add_page_errors);
create_form_input('content', 'textarea', 'Content', $add_page_errors);
?>
        
        <input type="submit" name="submit_button" value="Add This Page" id="submit_button" class="btn btn-default" />
    
    </fieldset>

</form>

<script type="text/javascript" src="js/tinymce/tinymce.min.js"></script>
<script type="text/javascript">
    tinyMCE.init({
        // General options
        selector : "#content",
        width : 800,
        height : 400,
        browser_spellcheck : true,
        
        plugins: "paste,searchreplace,fullscreen,hr,link,anchor,image,charmap,media,autoresize,autosave,contextmenu,wordcount",

        toolbar1: "cut,copy,paste,|,undo,redo,removeformat,|hr,|,link,unlink,anchor,image,|,charmap,media,|,search,replace,|,fullscreen",
        toolbar2:    "bold,italic,underline,strikethrough,|,alignleft,aligncenter,alignright,alignjustify,|,formatselect,|,bullist,numlist,|,outdent,indent,blockquote,",

        // Example content CSS (should be your site CSS)
        content_css : "/html/css/bootstrap.min.css",

    });
</script>
<!-- /TinyMCE -->

<?php /* PAGE CONTENT ENDS HERE! */

// Include the footer file to complete the template:
include('./includes/footer.html');
?>

 

I would greatly appreciate it if a member can point to me where it is I am going wrong.

Thanks.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...