Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'chapter 8'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 7 results

  1. New to this forum. Have been playing around with PHP and MySQL for some time and now writing my first application. So far all seems to be working but have got to chapter 8, script 8.4 edit_user.php (accessed from view_users.php) and will not seem to work. The edit link from view_users.php works and will display the record (can be seen at adrianball.london/view_users.php) Record information can be changed in the form but, after submitting the form, the database is not updated and the latest information does not display. My script code is below which I have checked 3 times with the book. Any help appreciated. Many thanks. <?php # Script 8.4 - edit_user.php // This page edits a user. // This page is accessed through view_users.php. $page_title = 'Edit a User'; include ('./includes/header.html'); // Check for a valid user ID, through GET or POST. if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_users.php $id = $_GET['id']; } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted. $id = $_POST['id']; } else { // No valid ID, kill the script. echo '<h1 id="mainhead">Page Error</h1> <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; include ('./includes/footer.html'); exit(); } require_once ('../mysql_connect.php'); // Connect to the db. // Check if the form has been submitted. if (isset($_POST['submitted'])) { $errors = array(); // Initialize error array. // Check for a first name. if (empty($_POST['firstname'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = escape_data($_POST['firstname']); } // Check for a last name. if (empty($_POST['lastname'])) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = escape_data($_POST['lastname']); } // Check for an email address. if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email address.'; } else { $e = escape_data($_POST['email']); } if (empty($errors)) { // If everything's OK. // Test for unique email address. $query = "SELECT user_id FROM students WHERE email='$e' AND user_id != $id"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { // Make the query. $query = "UPDATE students SET firstname='$fn', lastname='$ln', email='$e' WHERE user_id=$id"; $result = @mysql_query ($query); // Run the query. if (mysql_affected_rows() == 1) { // If it ran OK. // Print a message. echo '<h1 id="mainhead">Edit a User</h1> <p>The user has been edited.</p><p><br /><br /></p>'; } else { // If it did not run OK. echo '<h1 id="mainhead">System Error</h1> <p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message. echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message. include ('./includes/footer.html'); exit(); } } else { // Already registered. echo '<h1 id="mainhead">Error!</h1> <p class="error">The email address has already been registered.</p>'; } } else { // Report the errors. echo '<h1 id="mainhead">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 submit conditional. // Always show the form. // Retrieve the users information. $query = "SELECT firstname, lastname, email FROM students WHERE user_id=$id"; $result = @mysql_query ($query); // Run the query. if (mysql_num_rows($result) == 1) { // Valid user ID, show the form. // Get the user's information. $row = mysql_fetch_array ($result, MYSQL_NUM); // Create the form. echo '<h2>Edit a User</h2> <form action="edit_user.php" method="post"> <p>First Name: <input type="text" name="firstname" size="15" maxlength="15" value="' . $row[0] . '" /></p> <p>Last Name: <input type="text" name="lastname" size="15" maxlength="30" value="' . $row[1] . '" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="' . $row[2] . '" /> </p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="id" value="' . $id . '" /> </form>'; } else { // Not a valid user ID. echo '<h1 id="mainhead">Page Error</h1> <p class="error">This page has been accessed in error.</p><p><br /><br /></p>'; } mysql_close(); // Close the database connection. include ('./includes/footer.html'); ?>
  2. Hi RE: mod_rewrite rule If mod_rewrite is working for coffee and goodies..Providing code is correct...mod_rewrite should also work for sales...Correct? Here is the re_write I am using for sales #For sales: RewriteRule ^shop/sales/?$ /sales.php Thank you
  3. I have downloaded script 8.7 from the LU web site and installed it on my web site as part of working through the book. However, every time I enter the correct information it keeps telling me the email and password do not match. I can't find any errata details for this or any listings on the Forum. Any ideas?
  4. Hi I have the following codes for registration and login. The registration doesnt seem to work, can anybody point out where the problem is or what am i not doing, should there be a text file included? ?<?php // Script 8.10 - register.php #2 /* This page lets people register for the site (in theory). */ // Set the page title and include the header file: define('TITLE', 'Register'); include('header.html'); // Print some introductory text: print '<h2>Registration Form</h2> <p>Register so that you can take advantage of certain features like this, that, and the other thing.</p>'; // Add the CSS: print '<style type="text/css" media="screen"> .error { color: red; } </style>'; // Check if the form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { $problem = FALSE; // No problems so far. // Check for each value... if (empty($_POST['first_name'])) { $problem = TRUE; print '<p class="error">Please enter your first name!</p>'; } if (empty($_POST['last_name'])) { $problem = TRUE; print '<p class="error">Please enter your last name!</p>'; } if (empty($_POST['email']) || (substr_count($_POST['email'], '@') != 1) ) { $problem = TRUE; print '<p class="error">Please enter your email address!</p>'; } if (empty($_POST['password1'])) { $problem = TRUE; print '<p class="error">Please enter a password!</p>'; } if ($_POST['password1'] != $_POST['password2']) { $problem = TRUE; print '<p class="error">Your password did not match your confirmed password!</p>'; } if (!$problem) { // If there weren't any problems... // Print a message: print '<p>You are now registered!<br />Okay, you are not really registered but...</p>'; // Send the email: $body = "Thank you for registering with the J.D. Salinger fan club! Your password is '{$_POST['password1']}'."; mail($_POST['email'], 'Registration Confirmation', $body, 'From: admin@example.com'); // Clear the posted values: $_POST = array(); } else { // Forgot a field. print '<p class="error">Please try again!</p>'; } } // End of handle form IF. // Create the form: ?> <form action="register.php" method="post"> <p>First Name: <input type="text" name="first_name" size="20" value="<?php if (isset($_POST['first_name'])) { print htmlspecialchars($_POST['first_name']); } ?>" /></p> <p>Last Name: <input type="text" name="last_name" size="20" value="<?php if (isset($_POST['last_name'])) { print htmlspecialchars($_POST['last_name']); } ?>" /></p> <p>Email Address: <input type="text" name="email" size="20" value="<?php if (isset($_POST['email'])) { print htmlspecialchars($_POST['email']); } ?>" /></p> <p>Password: <input type="password" name="password1" size="20" value="<?php if (isset($_POST['password1'])) { print htmlspecialchars($_POST['password1']); } ?>" /></p> <p>Confirm Password: <input type="password" name="password2" size="20" value="<?php if (isset($_POST['password2'])) { print htmlspecialchars($_POST['password2']); } ?>" /></p> <p><input type="submit" name="submit" value="Register!" /></p> </form> <?php include('footer.html'); // Need the footer. ?> <?php // Script 8.13 - login.php #2 /* This page lets people log into the site (in theory). */ // Set the page title and include the header file: define('TITLE', 'Login'); include('header.html'); // Print some introductory text: print '<h2>Login Form</h2> <p>Users who are logged in can take advantage of certain features like this, that, and the other thing.</p>'; // Check if the form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form: if ( (!empty($_POST['email'])) && (!empty($_POST['password'])) ) { if ( (strtolower($_POST['email']) == 'me@example.com') && ($_POST['password'] == 'testpass') ) { // Correct! // Redirect the user to the welcome page! ob_end_clean(); // Destroy the buffer! header ('Location: welcome.php'); exit(); } else { // Incorrect! print '<p>The submitted email address and password do not match those on file!<br />Go back and try again.</p>'; } } else { // Forgot a field. print '<p>Please make sure you enter both an email address and a password!<br />Go back and try again.</p>'; } } else { // Display the form. print '<form action="login.php" method="post"> <p>Email Address: <input type="text" name="email" size="20" /></p> <p>Password: <input type="password" name="password" size="20" /></p> <p><input type="submit" name="submit" value="Log In!" /></p> </form>'; } include('footer.html'); // Need the footer. ?> Im getting these erros with the register and login codes:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Register</title> <style type="text/css" media="screen"> .error { color: red; } </style> </head> <body> <h1>Register</h1> <?php // Script 11.6 - register.php /* This script registers a user by storing their information in a text file and creating a directory for them. */ // Identify the directory and file to use: $dir = '../users/'; $file = $dir . 'users.txt'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form. $problem = FALSE; // No problems so far. // Check for each value... if (empty($_POST['username'])) { $problem = TRUE; print '<p class="error">Please enter a username!</p>'; } if (empty($_POST['password1'])) { $problem = TRUE; print '<p class="error">Please enter a password!</p>'; } if ($_POST['password1'] != $_POST['password2']) { $problem = TRUE; print '<p class="error">Your password did not match your confirmed password!</p>'; } if (!$problem) { // If there weren't any problems... if (is_writable($file)) { // Open the file. // Create the data to be written: $subdir = time() . rand(0, 4596); $data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL; // Write the data: file_put_contents($file, $data, FILE_APPEND | LOCK_EX); // Create the directory: mkdir ($dir . $subdir); // Print a message: print '<p>You are now registered!</p>'; } else { // Couldn't write to the file. print '<p class="error">You could not be registered due to a system error.</p>'; } } else { // Forgot a field. print '<p class="error">Please go back and try again!</p>'; } } else { // Display the form. // Leave PHP and display the form: ?> <form action="register.php" method="post"> <p>Username: <input type="text" name="username" size="20" /></p> <p>Password: <input type="password" name="password1" size="20" /></p> <p>Confirm Password: <input type="password" name="password2" size="20" /></p> <input type="submit" name="submit" value="Register" /> </form> <?php } // End of submission IF. ?> </body> </html> Register Please enter a username!'; } if (empty($_POST['password1'])) { $problem = TRUE; print ' Please enter a password! '; } if ($_POST['password1'] != $_POST['password2']) { $problem = TRUE; print ' Your password did not match your confirmed password! '; } if (!$problem) { // If there weren't any problems... if (is_writable($file)) { // Open the file. // Create the data to be written: $subdir = time() . rand(0, 4596); $data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL; // Write the data: file_put_contents($file, $data, FILE_APPEND | LOCK_EX); // Create the directory: mkdir ($dir . $subdir); // Print a message: print ' You are now registered! '; } else { // Couldn't write to the file. print ' You could not be registered due to a system error. '; } } else { // Forgot a field. print ' Please go back and try again! '; } } else { // Display the form. // Leave PHP and display the form: ?> Username: Password: Confirm Password: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Login</title> </head> <body> <h1>Login</h1> <?php // Script 11.8 - login.php /* This script logs a user in by check the stored values in text file. */ // Identify the file to use: $file = 'users/users.txt'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form. $loggedin = FALSE; // Not currently logged in. // Enable auto_detect_line_settings: ini_set('auto_detect_line_endings', 1); // Open the file: $fp = fopen($file, 'rb'); // Loop through the file: while ( $line = fgetcsv($fp, 200, "\t") ) { // Check the file data against the submitted data: if ( ($line[0] == $_POST['username']) AND ($line[1] == md5(trim($_POST['password']))) ) { $loggedin = TRUE; // Correct username/password combination. // Stop looping through the file: break; } // End of IF. } // End of WHILE. fclose($fp); // Close the file. // Print a message: if ($loggedin) { print '<p>You are now logged in.</p>'; } else { print '<p style="color: red;">The username and password you entered do not match those on file.</p>'; } } else { // Display the form. // Leave PHP and display the form: ?> <form action="login.php" method="post"> <p>Username: <input type="text" name="username" size="20" /></p> <p>Password: <input type="password" name="password" size="20" /></p> <input type="submit" name="submit" value="Login" /> </form> <?php } // End of submission IF. ?> </body> </html> Login You are now logged in.'; } else { print ' The username and password you entered do not match those on file. '; } } else { // Display the form. // Leave PHP and display the form: ?> Username: Password: This is the live one:http://project2013.net78.net/login.php
  5. I have been reading through all the URI based posts but I still can’t get the Coffee site example working on my local machine. I have my site located here: C:\apache\htdocs\eCom01\ I have the following setup in my config.php file: define('BASE_URI','C:/apache/htdocs/eCom01/'); define('BASE_URL','http://localhost:8080/ecom01/'); define('MYSQL','includes/mysql.inc.php'); When I view http://localhost:8080/ecom01/shop.php all images display and data is being pulled from the database. Here’s what the relative links look on this shop.php file: require('./includes/config.php'); include('./includes/header.html'); require(MYSQL); Inside my header.html file I have tried to setup the links 2 different ways: <li><a href="/shop/coffee/">Coffee</a></li> <li><a href="./shop/goodies/">Goodies</a></li> When I click the Coffee link, I get a NOT FOUND error (The requested URL /shop/coffee/ was not found on this server.) Somehow it is dropping the “/eCom01/” folder… When I click the Goodies link, the page displays all the content from the database, but does not display the css file or any of the pictures. I have been trying to trouble shoot this, but cannot come up with a fix. I have tried defining the BASE_URI and BASE_URL to many different values, but no matter what I set those to, I get the same results. I also added an echo $_SERVER['DOCUMENT_ROOT'] ; command on the shop.php file. This always displays “C:/apache/htdocs” I am assuming this issue has something to do with setting the BASE_URI and URL items, but I can’t figure it out. Even if I comment out those 2 lines, the mySql connection still works. Is there some setting I have change in my apache configuration file to allow these changes? When I change these URI and URL variables, shouldn’t the $_SERVER[‘DOCUMENT_ROOT’] reflect them? Any and all help is appreciated.
  6. I don't understand how to use e - so I've created a small form to test my understanding of event handling. The form requires a radio button to be checked. If I submit the form without checking a button the error message appears but then the form appears to be locked and I can't check a button. Firebug stops at this line U.stopDefault(e); which seems correct. Do I need to reset something? HTML <body> <form id="optInForm" method="post" action="optIn.php"> <fieldset><legend>Agree</legend> <label for="yes">Yes </label><input type="radio" name="optIn" value="TRUE" /> <label for="no">No </label><input type="radio" name="optIn" value="FALSE" /> <input type="submit" value="submit" id="submit"/> <input type="hidden" name="submitted" value="TRUE" /> <div id="errMsg"></div> </fieldset> </form> <script type="text/javascript" src="optIn.js"></script> <script type="text/javascript" src="utilities.js"></script> </body> optIn.js function checkForm() { 'use strict'; var optIn = document.getElementsByName('optIn'); var msg = ''; var chosen = null; for (var i=0, il = optIn.length; i<il; i++) { if (optIn[i].checked){ chosen = optIn[i].value; } } if (chosen === null) { msg='Please select Yes or No'; U.setText('errMsg', msg); U.stopDefault(e); } else { return true; } } // end checkForm function window.onload=function(){ 'use strict'; U.addEvent(U.$('optInForm'),'submit', checkForm); }; utilities.js var U = { $: function(id) { 'use strict'; if (typeof id =='string'){ return document.getElementById(id); } }, // end $ method setText: function(id, message) { 'use strict'; if ((typeof id == 'string') && (typeof message == 'string')) { var output = this.$(id); if (!output) return false; if (output.textContent !== undefined) { output.textContent = message; } else { output.innerText = message; } return true; } }, //end setText method addEvent: function(obj, type, fn) { 'use strict'; if (obj && obj.addEventListener) { obj.addEventListener(type, fn, false); } else if (obj && obj.attachEvent) { obj.attachEvent('on' + type, fn); } }, //end addEvent method removeEvent: function(obj, type, fn) { 'use strict'; if (obj && obj.removeEventListener) { obj.removeEventListener(type, fn, false); } else if (obj && obj.detachEvent) { obj.detachEvent('on' + type, fn); } }, //end removeEvent method stopDefault: function(e) { 'use strict'; e = e || window.event; if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } return false; } // end stopDefault method }; // end U class def
  7. Hello, I am having a problem with Chapter 8 "Creating Web Applications" pg. 195. While running my "index.php" through my web browser, I get several warning as follows: (It also happens with the downloaded script for this book "script_08_04") Warning: include(header.html) [function.include]: failed to open stream: No such file or directory in /home/content/87/8276687/html/index.php on line 11 Warning: include() [function.include]: Failed opening 'header.html' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/87/8276687/html/index.php on line 11 Welcome to a J.D. Salinger Fan Club Lorem ipsum dolor.... Another Header Lorem ipsum dolor... Warning: include(footer.html) [function.include]: failed to open stream: No such file or directory in /home/content/87/8276687/html/index.php on line 21 Warning: include() [function.include]: Failed opening 'footer.html' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/87/8276687/html/index.php on line 21 /* I created the "Templates and CSS" folders in my web root with both "header.html and footer.html" in the Templates folder and "1.css" in the CSS folder. index.php is in the web root. If I put all files in the web root It has no warnings but It doesn't incorporate the CSS file. " It doesn't look like example "D" on p. 195 " I was wondering if anyone knows what these warnings are and how to remedy the problem? Any help would be appreciated.
×
×
  • Create New...