Jump to content
Larry Ullman's Book Forums

Chapter 5: Add_Page (Category Drop Down Menu)


Recommended Posts

Hello again.

 

Problem:

 

Every time I am logged in as administrator to add content, it doesn't allow me to submit the "Add This Page". Instead, it keeps asking me to "Please select a category!" - which I have.

 

 

Current code I am using for 'validating category' (as on page 109)

 

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!';

}

 

What I have tried

 

I have tried the other variation of:

 

if (isset($_POST['category'] && (int) $_POST['category'] >= 1)) {

$cat = $_POST['category'];

} else { // No category selected.

$add_page_errors['category'] = 'Please select a category!';

}

 

 

But this keeps on sending me the error on the line. What do you think I should do?

Many thanks

Link to comment
Share on other sites

Hi Jon

 

When I try the variation code, I get this:

 

Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /hermes/waloraweb004/b1384/moo.laissezfairelondonco/wolfcut/add_page.php on line 33

 

I can't see where the syntax error is because I have copied the code thoroughly from the page 109.

 

===============

 

When I try the orginal code, there is no error message. Only keeps asking me to "Please select a category!"

Link to comment
Share on other sites

if (isset($_POST['category'] && (int) $_POST['category'] >= 1)) {

In the above code you have one of the closing parenthesis in the wrong place. It should be

if (isset($_POST['category']) && (int) $_POST['category'] >= 1) {

You need to see what is being passed in $_POST['category']. Try inserting

var_dump or print_r($_POST['category']);

above your if statement.

Link to comment
Share on other sites

The typo ')' you amended still leads to the same thing: "Please select a category!"

Here's the complete error message: (its a long one)....

============

 

An error occurred in script '/hermes/waloraweb004/b1384/moo.laissezfairelondonco/wolfcut/add_page.php' on line 34:
Use of undefined constant var_dump - assumed 'var_dump'
Array
(
   [0] => Array
    (
	    [file] => /hermes/waloraweb004/b1384/moo.laissezfairelondonco/wolfcut/add_page.php
	    [line] => 34
	    [function] => my_error_handler
	    [args] => Array
		    (
			    [0] => 8
			    [1] => Use of undefined constant var_dump - assumed 'var_dump'
			    [2] => /hermes/waloraweb004/b1384/moo.laissezfairelondonco/wolfcut/add_page.php
			    [3] => 34
			    [4] => Array
				    (
					    [GLOBALS] => Array
*RECURSION*
					    [_ENV] => Array
						    (
							    [PATH] => /usr/local/bin:/usr/bin:/bin
							    [sCRIPT_NAME] => /add_page.php
							    [REQUEST_METHOD] => POST
							    [HTTP_ACCEPT] => text/html, application/xhtml+xml, */*
							    [sCRIPT_FILENAME] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut/add_page.php
							    [sERVER_SOFTWARE] => Apache/2
							    [REMOTE_PORT] => 40734
							    [HTTP_USER_AGENT] => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
							    [HTTP_CACHE_CONTROL] => no-cache
							    [HTTP_ACCEPT_LANGUAGE] => en-GB
							    [CONTENT_TYPE] => application/x-www-form-urlencoded
							    [GATEWAY_INTERFACE] => CGI/1.1
							    [sCRIPT_URL] => /add_page.php
							    [DOCUMENT_ROOT] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut
							    [REMOTE_HOST] => host-78-151-213-160.as13285.net
							    [uNIQUE_ID] => UOjAmwoBDKcAAG4JbDAAAAAh
							    [sERVER_NAME] => wolfcut.co.uk
							    [HTTP_REFERER] => http://wolfcut.co.uk/add_page.php
							    [sERVER_ADMIN] => cgiadmin@yourhostingaccount.com
							    [HTTP_ACCEPT_ENCODING] => gzip, deflate
							    [HTTP_CONNECTION] => close
							    [CONTENT_LENGTH] => 125
							    [sCRIPT_URI] => http://wolfcut.co.uk/add_page.php
							    [sERVER_PORT] => 80
							    [HTTP_COOKIE] => TinyMCE_content_size=cw%3D571%26ch%3D121; PHPSESSID=3d30fa3c80ac91b2d2dd804bad7a3e91
							    [REMOTE_ADDR] => 78.151.213.160
							    [sERVER_PROTOCOL] => HTTP/1.0
							    [REQUEST_URI] => /add_page.php
							    [HTTP_HOST] => wolfcut.co.uk
							    [TZ] => EST5EDT
						    )
					    [HTTP_ENV_VARS] => Array
						    (
							    [PATH] => /usr/local/bin:/usr/bin:/bin
							    [sCRIPT_NAME] => /add_page.php
							    [REQUEST_METHOD] => POST
							    [HTTP_ACCEPT] => text/html, application/xhtml+xml, */*
							    [sCRIPT_FILENAME] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut/add_page.php
							    [sERVER_SOFTWARE] => Apache/2
							    [REMOTE_PORT] => 40734
							    [HTTP_USER_AGENT] => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
							    [HTTP_CACHE_CONTROL] => no-cache
							    [HTTP_ACCEPT_LANGUAGE] => en-GB
							    [CONTENT_TYPE] => application/x-www-form-urlencoded
							    [GATEWAY_INTERFACE] => CGI/1.1
							    [sCRIPT_URL] => /add_page.php
							    [DOCUMENT_ROOT] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut
							    [REMOTE_HOST] => host-78-151-213-160.as13285.net
							    [uNIQUE_ID] => UOjAmwoBDKcAAG4JbDAAAAAh
							    [sERVER_NAME] => wolfcut.co.uk
							    [HTTP_REFERER] => http://wolfcut.co.uk/add_page.php
							    [sERVER_ADMIN] => cgiadmin@yourhostingaccount.com
							    [HTTP_ACCEPT_ENCODING] => gzip, deflate
							    [HTTP_CONNECTION] => close
							    [CONTENT_LENGTH] => 125
							    [sCRIPT_URI] => http://wolfcut.co.uk/add_page.php
							    [sERVER_PORT] => 80
							    [HTTP_COOKIE] => TinyMCE_content_size=cw%3D571%26ch%3D121; PHPSESSID=3d30fa3c80ac91b2d2dd804bad7a3e91
							    [REMOTE_ADDR] => 78.151.213.160
							    [sERVER_PROTOCOL] => HTTP/1.0
							    [REQUEST_URI] => /add_page.php
							    [HTTP_HOST] => wolfcut.co.uk
							    [TZ] => EST5EDT
						    )
					    [PATH] => /usr/local/bin:/usr/bin:/bin
					    [sCRIPT_NAME] => /add_page.php
					    [REQUEST_METHOD] => POST
					    [HTTP_ACCEPT] => text/html, application/xhtml+xml, */*
					    [sCRIPT_FILENAME] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut/add_page.php
					    [sERVER_SOFTWARE] => Apache/2
					    [REMOTE_PORT] => 40734
					    [HTTP_USER_AGENT] => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
					    [HTTP_CACHE_CONTROL] => no-cache
					    [HTTP_ACCEPT_LANGUAGE] => en-GB
					    [CONTENT_TYPE] => application/x-www-form-urlencoded
					    [GATEWAY_INTERFACE] => CGI/1.1
					    [sCRIPT_URL] => /add_page.php
					    [DOCUMENT_ROOT] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut
					    [REMOTE_HOST] => host-78-151-213-160.as13285.net
					    [uNIQUE_ID] => UOjAmwoBDKcAAG4JbDAAAAAh
					    [sERVER_NAME] => wolfcut.co.uk
					    [HTTP_REFERER] => http://wolfcut.co.uk/add_page.php
					    [sERVER_ADMIN] => cgiadmin@yourhostingaccount.com
					    [HTTP_ACCEPT_ENCODING] => gzip, deflate
					    [HTTP_CONNECTION] => close
					    [CONTENT_LENGTH] => 125
					    [sCRIPT_URI] => http://wolfcut.co.uk/add_page.php
					    [sERVER_PORT] => 80
					    [HTTP_COOKIE] => TinyMCE_content_size=cw%3D571%26ch%3D121; PHPSESSID=3d30fa3c80ac91b2d2dd804bad7a3e91
					    [REMOTE_ADDR] => 78.151.213.160
					    [sERVER_PROTOCOL] => HTTP/1.0
					    [REQUEST_URI] => /add_page.php
					    [HTTP_HOST] => wolfcut.co.uk
					    [TZ] => EST5EDT
					    [title] => Films
					    [category] => Select One
					    [description] => Great Film
					    [content] =>
   Bourne Identity

					    [submit_button] => Add This Page
					    [TinyMCE_content_size] => cw=571&ch=121
					    [phpSESSID] => 3d30fa3c80ac91b2d2dd804bad7a3e91
					    [php_SELF] => /add_page.php
					    [REQUEST_TIME] => 1357430939
					    [argv] => Array
						    (
						    )
					    [argc] => 0
					    [_POST] => Array
						    (
							    [title] => Films
							    [category] => Select One
							    [description] => Great Film
							    [content] =>
   Bourne Identity
							    [submit_button] => Add This Page
						    )

					    [HTTP_POST_VARS] => Array
						    (
							    [title] => Films
							    [category] => Select One
							    [description] => Great Film
							    [content] =>
   Bourne Identity
							    [submit_button] => Add This Page
						    )

					    [_GET] => Array
						    (
						    )

					    [HTTP_GET_VARS] => Array
						    (
						    )

					    [_COOKIE] => Array
						    (
							    [TinyMCE_content_size] => cw=571&ch=121
							    [phpSESSID] => 3d30fa3c80ac91b2d2dd804bad7a3e91
						    )

					    [HTTP_COOKIE_VARS] => Array
						    (
							    [TinyMCE_content_size] => cw=571&ch=121
							    [phpSESSID] => 3d30fa3c80ac91b2d2dd804bad7a3e91
						    )

					    [_SERVER] => Array
						    (
							    [PATH] => /usr/local/bin:/usr/bin:/bin
							    [sCRIPT_NAME] => /add_page.php
							    [REQUEST_METHOD] => POST
							    [HTTP_ACCEPT] => text/html, application/xhtml+xml, */*
							    [sCRIPT_FILENAME] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut/add_page.php
							    [sERVER_SOFTWARE] => Apache/2
							    [REMOTE_PORT] => 40734
							    [HTTP_USER_AGENT] => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
							    [HTTP_CACHE_CONTROL] => no-cache
							    [HTTP_ACCEPT_LANGUAGE] => en-GB
							    [CONTENT_TYPE] => application/x-www-form-urlencoded
							    [GATEWAY_INTERFACE] => CGI/1.1
							    [sCRIPT_URL] => /add_page.php
							    [DOCUMENT_ROOT] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut
							    [REMOTE_HOST] => host-78-151-213-160.as13285.net
							    [uNIQUE_ID] => UOjAmwoBDKcAAG4JbDAAAAAh
							    [sERVER_NAME] => wolfcut.co.uk
							    [HTTP_REFERER] => http://wolfcut.co.uk/add_page.php
							    [sERVER_ADMIN] => cgiadmin@yourhostingaccount.com
							    [HTTP_ACCEPT_ENCODING] => gzip, deflate
							    [HTTP_CONNECTION] => close
							    [CONTENT_LENGTH] => 125
							    [sCRIPT_URI] => http://wolfcut.co.uk/add_page.php
							    [sERVER_PORT] => 80
							    [HTTP_COOKIE] => TinyMCE_content_size=cw%3D571%26ch%3D121; PHPSESSID=3d30fa3c80ac91b2d2dd804bad7a3e91
							    [REMOTE_ADDR] => 78.151.213.160
							    [sERVER_PROTOCOL] => HTTP/1.0
							    [REQUEST_URI] => /add_page.php
							    [HTTP_HOST] => wolfcut.co.uk
							    [TZ] => EST5EDT
							    [php_SELF] => /add_page.php
							    [REQUEST_TIME] => 1357430939
							    [argv] => Array
								    (
								    )

							    [argc] => 0
						    )
					    [HTTP_SERVER_VARS] => Array
						    (
							    [PATH] => /usr/local/bin:/usr/bin:/bin
							    [sCRIPT_NAME] => /add_page.php
							    [REQUEST_METHOD] => POST
							    [HTTP_ACCEPT] => text/html, application/xhtml+xml, */*
							    [sCRIPT_FILENAME] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut/add_page.php
							    [sERVER_SOFTWARE] => Apache/2
							    [REMOTE_PORT] => 40734
							    [HTTP_USER_AGENT] => Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
							    [HTTP_CACHE_CONTROL] => no-cache
							    [HTTP_ACCEPT_LANGUAGE] => en-GB
							    [CONTENT_TYPE] => application/x-www-form-urlencoded
							    [GATEWAY_INTERFACE] => CGI/1.1
							    [sCRIPT_URL] => /add_page.php
							    [DOCUMENT_ROOT] => /home/users/web/b1384/moo.laissezfairelondonco/wolfcut
							    [REMOTE_HOST] => host-78-151-213-160.as13285.net
							    [uNIQUE_ID] => UOjAmwoBDKcAAG4JbDAAAAAh
							    [sERVER_NAME] => wolfcut.co.uk
							    [HTTP_REFERER] => http://wolfcut.co.uk/add_page.php
							    [sERVER_ADMIN] => cgiadmin@yourhostingaccount.com
							    [HTTP_ACCEPT_ENCODING] => gzip, deflate
							    [HTTP_CONNECTION] => close
							    [CONTENT_LENGTH] => 125
							    [sCRIPT_URI] => http://wolfcut.co.uk/add_page.php
							    [sERVER_PORT] => 80
							    [HTTP_COOKIE] => TinyMCE_content_size=cw%3D571%26ch%3D121; PHPSESSID=3d30fa3c80ac91b2d2dd804bad7a3e91
							    [REMOTE_ADDR] => 78.151.213.160
							    [sERVER_PROTOCOL] => HTTP/1.0
							    [REQUEST_URI] => /add_page.php
							    [HTTP_HOST] => wolfcut.co.uk
							    [TZ] => EST5EDT
							    [php_SELF] => /add_page.php
							    [REQUEST_TIME] => 1357430939
							    [argv] => Array
								    (
								    )
							    [argc] => 0
						    )

					    [_FILES] => Array
						    (
						    )

					    [HTTP_POST_FILES] => Array
						    (
						    )

					    [_REQUEST] => Array
						    (
							    [title] => Films
							    [category] => Select One
							    [description] => Great Film
							    [content] =>
   Bourne Identity
							    [submit_button] => Add This Page
							    [TinyMCE_content_size] => cw=571&ch=121
							    [phpSESSID] => 3d30fa3c80ac91b2d2dd804bad7a3e91
						    )

					    [live] =>
					    [contact_email] => jizzwit@gmail.com
					    [HTTP_SESSION_VARS] => Array
						    (
							    [user_admin] => 1
							    [user_id] => 2
							    [username] => shimewaza
							    [user_not_expired] => 1
						    )

					    [_SESSION] => Array
						    (
							    [user_admin] => 1
							    [user_id] => 2
							    [username] => shimewaza
							    [user_not_expired] => 1
						    )

					    [user_admin] => 1
					    [user_id] => 2
					    [username] => shimewaza
					    [user_not_expired] => 1
					    [page_title] => Add a Site Content Page
					    [pages] => Array
						    (
							    [Home] => index.php
							    [About] => about.php
							    [Contact] => contact.php
							    [Register] => register.php
						    )
					    [this_page] => add_page.php
					    [v] => register.php
					    [k] => Register
					    [dbc] => mysqli Object
						    (
						    )

					    [add_page_errors] => Array
						    (
						    )

					    [t] => Films
				    )
		    )
    )
)

 

 

==================== END

Link to comment
Share on other sites

It's obvious isn't it - $_POST is not getting the category that you select. There could be any number of reasons for this, but you'll have to show us your code so we can be more helpful.

 

This kind of debugging is pretty basic and you may struggle with this book if you're not able to identify these errors. You may want to first read Larry's book, PHP and MySQL for Dynamic Web Sites.

  • Upvote 1
Link to comment
Share on other sites

Thanks Margaux, I have the book, but even that can be perplexing at times. Please take a look at the code from add_page.php:

=============

 

<?
require ('./includes/config.inc.php');
redirect_invalid_user('user_admin');
$page_title = 'Add a Site Content Page';
include ('./includes/header.html');
require ('./includes/mysql.inc.php');
$add_page_errors = array();
// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Check for a title:
if (!empty($_POST['title'])) {
 $t = mysqli_real_escape_string($dbc, strip_tags($_POST['title']));
} else {
 $add_page_errors['title'] = 'Please enter the title!';
}
// Check for a category:
if (isset($_POST['category']) && (int) $_POST['category'] >= 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 = mysqli_real_escape_string($dbc, strip_tags($_POST['description']));
} 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 = mysqli_real_escape_string($dbc, strip_tags($_POST['content'], $allowed));
} else {
 $add_page_errors['content'] = 'Please enter the content!';
}

if (empty($add_page_errors)) {

 $q = "INSERT INTO pages (category_id, title, description, content) VALUES ($cat, '$t', '$d', '$c')";
 $r = mysqli_query ($dbc, $q);
 if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

  echo '<h4>The page has been added!</h4>';

  // Clear $_POST:
  $_POST = array();

  // Send an email to the administrator to let them know new content was added?

 } else {
  trigger_error('The page could not be added due to a system error. We apologize for any inconvenience.');
 }

} .
} .
require ('includes/form_functions.inc.php');
?>
<h3>Add a Site Content Page</h3>
<form action="add_page.php" method="post" accept-charset="utf-8">
<fieldset><legend>Fill out the form to add a page of content:</legend>
 <p><label for="title"><strong>Title</strong></label><br /><?php create_form_input('title', 'text', $add_page_errors); ?></p>
<p><label for="category"><strong>Category</strong></label><br />
<select name="category"<?php if (array_key_exists('category', $add_page_errors)) echo ' class="error"'; ?>>
<option>Select One</option>

<?php // 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";
 }
?>
</select><?php if (array_key_exists('category', $add_page_errors)) echo ' <span class="error">' . $add_page_errors['category'] . '</span>'; ?></p>
<p><label for="description"><strong>Description</strong></label><br /><?php create_form_input('description', 'textarea', $add_page_errors); ?></p>
<p><label for="content"><strong>Content</strong></label><br /><?php create_form_input('content', 'textarea', $add_page_errors); ?></p>
<p><input type="submit" name="submit_button" value="Add This Page" id="submit_button" class="formbutton" /></p>
</fieldset>
</form>
<script type="text/javascript" src="./tiny_mce/jscripts/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
 // General options
 mode : "exact",
 elements : "content",
 theme : "advanced",
 width : 800,
 height : 400,
 plugins : "advlink,advlist,autoresize,autosave,contextmenu,fullscreen,iespell,inlinepopups,media,paste,preview,safari,searchreplace,visualchars,wordcount,xhtmlxtras",
 // Theme options
 theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,|,undo,redo,removeformat,|,search,replace,|,cleanup,help,code,preview,visualaid,fullscreen",
 theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,|,bullist,numlist,|,outdent,indent,blockquote,|,sub,sup,cite,abbr",
 theme_advanced_buttons3 : "hr,|,link,unlink,anchor,image,|,charmap,emotions,iespell,media",
 theme_advanced_toolbar_location : "top",
 theme_advanced_toolbar_align : "left",
 theme_advanced_statusbar_location : "bottom",
 theme_advanced_resizing : true,
 content_css : "./css/styles.css",
});
</script>
<!-- /TinyMCE -->
<?php /* PAGE CONTENT ENDS HERE! */
include ('./includes/footer.html');
?>

Link to comment
Share on other sites

Not many people are going to wade through all that code. The error has been spelled out, $_POST['category'] is not getting a value - you need to figure out why a category isn't being selected in your set-up. Could be your categories table is not set up properly.

 

A couple of things:

  1. var_dump is a function that needs an argument not a constant which is why you're getting that error. Here's how to use it.
     
  2. Please use code tags when including code, it makes it a lot easier to read. Its the <> icon. Also only include the relevant code.

  • Upvote 1
Link to comment
Share on other sites

ALL SORTED GUYS :)

 

Yep , it was my mistake again (as usual). Here's the solution:

 

SOLUTION:

I didn't populate the database, hence why the Variable ['categories'] could not be found. All I had to do was copy the SQL commands from Larry's downloads and insert them into PHPMYADMIN.

 

School Boy Error, but what a great learning experience. I now know the are 8 types of variable, what the are.... etc.... I know its not the conventional way of learning, but jumping in the deep end and tackling problems when the arise suits my style. Please continue to give me hints and tips as to where I go wrong. I can find out the rest and it makes things embed in my memory longer.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...