Jump to content
Larry Ullman's Book Forums

Ryan R

Members
  • Posts

    63
  • Joined

  • Last visited

Everything posted by Ryan R

  1. Thank you, Hartley! You were right! I didn't change the database name in mysqli_connect.php file. Thanks again for your help.
  2. Hi all, Thanks for your help. I just tested the message board webpage after all of pages were built up. There are some warnings I got from header.html page. The warnings are below: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\template-forums\includes\header.html on line 37 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\template-forums\includes\header.html on line 47 Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\template-forums\includes\header.html on line 50 I don't know why I am having these. I am attaching 'header.html' page below. -------------------------------------------------------------------------------------------------------------------------------------------------------- <?php # Script 15.1 - header.html /* This script... * - starts the HTML template. * - indicates the encoding using header(). * - starts the session. * - gets the language-specific words from the database. * - lists the available languages. */ // Indicate the encoding: header ('Content-Type: text/html; charset=UTF-8'); // Start the session: session_start(); // For testing purposes: $_SESSION['user_id'] = 1; $_SESSION['user_tz'] = 'America/New_York'; //$_SESSION = array(); // Need the database connection: require_once('../../mysqli_connect.php'); // The language ID is stored in the session. // Check for a new language ID... if (isset($_GET['lid']) && is_numeric($_GET['lid'])) { $_SESSION['lid'] = (int) $_GET['lid']; } elseif (!isset($_SESSION['lid'])) { $_SESSION['lid'] = 1; // Default. } // Get the words for this language. $words = FALSE; // Flag variable. if ($_SESSION['lid'] > 0) { $q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $words = mysqli_fetch_array($r, MYSQLI_ASSOC); } } // If we still don't have the words, get the default language: if (!$words) { $_SESSION['lid'] = 1; // Default. $q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}"; $r = mysqli_query($dbc, $q); $words = mysqli_fetch_array($r, MYSQLI_ASSOC); } mysqli_free_result($r); ?> <!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><?php echo $words['title']; ?></title> <style type="text/css" media="screen"> body { background-color: #ffffff; } .content { background-color: #f5f5f5; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; } a.navlink:link { color: #003366; text-decoration: none; } a.navlink:visited { color: #003366; text-decoration: none; } a.navlink:hover { color: #cccccc; text-decoration: none; } .title { font-size: 24px; font-weight: normal; color: #ffffff; margin-top: 5px; margin-bottom: 5px; margin-left: 20px; padding-top: 5px; padding-bottom: 5px; padding-left: 20px; } </style> </head> <body> <table width="90%" border="0" cellspacing="10" cellpadding="0" align="center"> <tr> <td colspan="2" bgcolor="#003366" align="center"><p class="title"><?php echo $words['title']; ?></p></td> </tr> <tr> <td valign="top" nowrap="nowrap" width="10%"> <b> <?php // Display links: // Default links: echo '<a href="index.php" class="navlink">' . $words['home'] . '</a><br /> <a href="forum.php" class="navlink">' . $words['forum_home'] . '</a><br />'; // Display links based upon login status: if (isset($_SESSION['user_id'])) { // If this is the forum page, add a link for posting new threads: if (stripos($_SERVER['PHP_SELF'], 'forum.php')) { echo '<a href="post.php" class="navlink">' . $words['new_thread'] . '</a><br />'; } // Add the logout link: echo '<a href="logout.php" class="navlink">' . $words['logout'] . '</a><br />'; } else { // Register and login links: echo '<a href="register.php" class="navlink">' . $words['register'] . '</a><br /> <a href="login.php" class="navlink">' . $words['login'] . '</a><br />'; } // For choosing a forum/language: echo '</b><p><form action="forum.php" method="get"> <select name="lid"> <option value="0">' . $words['language'] . '</option> '; // Retrieve all the languages... $q = "SELECT lang_id, lang FROM languages ORDER BY lang_eng ASC"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) > 0) { while ($menu_row = mysqli_fetch_array($r, MYSQLI_NUM)) { echo "<option value=\"$menu_row[0]\">$menu_row[1]</option>\n"; } } mysqli_free_result($r); unset($menu_row); echo '</select><br /> <input name="submit" type="submit" value="' . $words['submit'] . '" /> </form></p> </td> <td valign="top" class="content">'; ?> --------------------------------------------------------------------------------------------------------------------------------------------- Thanks for your help in advance and look forward to hearing from you soon.
  3. I contacted my hosting company to see if they have PHP6. However, the latest one that they have is PHP5. Where can I get PHP6 then?
  4. Hi All, Thanks for your immense help before. I have one question regarding the example shown in the book "PHP6 and MySQL5" I tried it according to what this book said. However, I saw this error message "Fatal error: Class 'Collator' not found in C:\xampp\htdocs\collation.php on line 22" I am attaching the web page here. Please see below: ------------------------------------------------------------------------------------------------------------------------------------- <?php header ('Content-Type: text/html; charset=UTF-8'); ?> <!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>Collation in PHP</title> </head> <body style="font-size: 18pt;"> <?php # Script 14.3 - collation.php // Create an array of words: $words = array('chère', 'côté', 'chaise', 'château', 'chaînette', 'châle', 'Chère', 'côte', 'chemise'); // Sort using the default PHP function: echo '<h3>Using sort()</h3>'; sort($words); echo implode('<br />', $words); // Sort using the Collator: echo '<h3>Using Collator</h3>'; $c = new Collator('fr_FR'); $c->sort($words); echo implode('<br />', $words); ?> </body> </html> --------------------------------------------------------------------------------------------------------------------------------------------- Thank you in advance and look forward to hearing from you soon.
  5. Hi All, Thank you very much for your immense help in advance. I have a question about "uploading a file" in Chapter 10. For handling error messages, this book used switch function. But I don't know where I can find defining codes in this web page. If an error occurs, how do you know the error falls into case 1, or case 2 or case 3 or .. ? Here below are the codes from the book. ---------------------------------------------------------------------------------------------------------------------------------------- <!DOCTYPE html> <html dir="ltr" lang="en-US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Upload an Image</title> <style type="text/css" title="text/css" media="all"> .error{ font-weight:bold; color:#c00; } </style> </head> <body> <?php #script 10.3 - upload_image.php //check if the form has been submitted if(isset($_POST['submitted'])) { //check for an uploaded file if(isset($_FILES['upload'])) { //validate the type. should be JPEG or PNG $allowed = array('image/pjpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png','images/pjpeg','images/jpeg','images/JPG','images/X-PNG','images/PNG','images/png','images/x-png' ); if(in_array($_FILES['upload']['type'], $allowed)) { //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}")) { echo'<p><em>The file has been uploaded!</em></p>'; } // end of move .. IF }else{ //Invalid type echo'<p class="error">Please upload a JPEG or PNG image.</p>'; } }// end of isset($_FILES['upload']) IF //check for an error if($_FILES['upload']['error'] > 0) { echo'<p class="error">The file could not be uploaded because: <strong>'; //print the message based upon the error switch($_FILES['upload']['error']) { case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.'; break; case 3: print 'The file was only partially uploaded.'; break; case 4: print 'No file was uploaded.'; break; case 6: print 'No temporary folder was available'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occured.'; break; } // end of swtich print'</strong></p>'; }//end of error IF //delete the temporary file if it still exists; if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) { unlink($_FILES['upload']['tmp_name']); } }// end of the submitted conditional ?> <form enctype="multipart/form-data" action="upload_image.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="524288"> <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend> <p><b>File:</b> <input type="file" name="upload" /></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"></div> <input type="hidden" name="submitted" value="TRUE" /> </form> </body> </html> ---------------------------------------------------------------------------------------------------------------- Thanks again in advance for your help and look forward to hearing from you soon.
  6. Thank you guys! I finally figured it out! I put the mysqli_num_rows after database query ($q) Thanks again.
  7. Thank you very much, Paul. I meant to say the PHP version on my server was greater than 5.0. Not MySQL. Anyway, It works well. Thanks again. If you don't mind, could you explain what this if "(float) substr(phpversion(), 0, 3) > 5.0" means? It sounds like my php version on my server is less than 5.0.
  8. Hi All, How are you? hope all is well with you. I would like to ask you a question about date and time function. I am reading through Chapter 10.There's a function that sets the default timezone 'date_default_timezone_set( );' I typed it as it was in the example this book introduced. Like this -> date_default_timezone_set('America/New_York'); and put this webpage onto my web server. But it said :Fatal error: Call to undefined function: date_default_timezone_set() in /www/mascdesign_com/email/datetime.php on line 15 Mysql version on my server is 5.1 and I think it shouldn't have any problem.. I am attaching my web page codes down below. ------------------------------------------------------------------------------------------------------------- <!DOCTYPE html> <html dir="ltr" lang="en-US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Contact Me</title> </head> <body> <h1>Contact Me</h1> <?php #script 10.1 - datetime.php //set the default timezone; date_default_timezone_set('America/New_York'); //check for form submission if(isset($_POST['submitted'])) { //minimal form validation if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments'])) { //create the body $body="Name:{$_POST['name']}\n\n Comments:{$_POST['comments']}"; //make it no longer than 70 characters long. $body=wordwrap($body, 70); //send the email mail('service@mascdesign.com','Contact form submission', $body,"From: {$_POST['email']}"); //print a message echo'<p><em>Thank you for contacting me at'.date('g:i a (T)').'on'.date('l F j, Y').'.I will reply some day.</em></p>'; //how long did it all take? echo'<p><strong>It took'.(time() - $_POST['start']).'seconds for you to complete and submit the form.</strong></p>'; //Clear $_POST (so that the form is not sticky) $_POST=array(); }else{ echo'<p style="font-weight:bold; color:#c00;">Please fill out the form completely</p>'; } }// end of main isset() if // create the HTML form: ?> <p>Please fill out the this form to contact me.</p> <form action="datetime.php" method="post"> <p>Subject: <input type="text" name="subject" size="30" maxlength="80" value="<?php if(isset($_POST['subject'])) { echo $_POST['subject'];}?>"/></p> <p>Name: <input type="text" name="name" size="30" maxlength="60" value="<?php if(isset($_POST['name'])) {echo $_POST['name'];} ?>" /></p> <p>Email Address: <input type="text" name="email" size="30" maxlength="80" value="<?php if(isset($_POST['email'])) {echo $_POST['email'];} ?>" /></p> <p>Comments: <textarea name="comments" rows="5" cols="30" ><?php if(isset($_POST['comments'])) { echo $_POST['comments']; } ?></textarea></p> <p><input type="submit" name="submit" value="Send"></p> <input type="hidden" name="submitted" value="TRUE"> </form> </body> </html> --------------------------------------------------------------------------------------------------------------------------------- It would be deeply appreciated if you can help me with this. Thanks in advance and look forward to hearing from you soon.
  9. Hi Jonathon, I thought your suggestion would definitely work.. Maybe I am putting it to a wrong spot. Can you help me with this again? I will attach the whole codes that I made here. ----------------------------------------------------------------------------------------------------------------------------------------------------------- <?php #script 8.5 - register.php #2 $page_title='register'; include('includes/header.html'); //check if the form has been submitted; if(isset($_POST['submitted'])) { require_once('../../mysqli_connect.php');//connect to the db $errors=array(); // Initialize an error array; //check for a first name; if(empty($_POST['first_name'])) { $errors[]='You forgot to enter your first name.'; } else { $fn=mysqli_real_escape_string($dbc, trim($_POST['first_name'])); } //check for a last name; if(empty($_POST['last_name'])) { $errors[]='You forgot to enter your last name'; }else{ $ln=mysqli_real_escape_string($dbc, trim($_POST['last_name'])); } //check for an email address; if(empty($_POST['email'])) { $errors[]='You forgot to enter your email address'; }else { $e=mysqli_real_escape_string($dbc, 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=mysqli_real_escape_string($dbc, trim($_POST['pass1'])); } }else { $errors[]='You forgot to enter your password'; } if(empty($errors)) { // If everything is OK. //register the user in the database.. //make the query $q="INSERT INTO users (first_name,last_name,email,pass,registration_date) VALUES('$fn','$ln','$e',SHA1('$p'),NOW())"; $r=@mysqli_query($dbc, $q);//run the query; if($r) {//if it ran OK //print the message. echo'<h1>Thank you!</h1> <p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p></br></p>'; }else{//if it didn not run OK. if(mysqli_num_rows($r) != 0) { echo'<p>Your email is already registered in the database. Please try a different one.</p>'; } else { echo'<h1>System Error</h1> <p class="error">You could not be registered due to a system error. We appologize for any inconvenience.</p>'; //debugging message; echo'<p>'.mysqli_error($dbc).'</p>'; } } // end of if($r); 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) occured:<br>'; foreach($errors as $msg) {//print the each error. echo "- $msg<br>\n"; } echo '</p><p>Please try again</p><p><br></p>'; }//end of if(empty($errors))IF. mysqli_close($dbc); // close the database connection }//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="20" value="<?php if(isset($_POST['last_name'])) echo $_post['last_name']; ?>" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /></p> <p>Password: <input type="password" name="pass1" size="10" maxlength="20" /></p> <p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p> <P><input type="submit" name="submit" value="Register" /></P> <p><input type="hidden" name="submitted" value="TRUE" /></p> </form> <?php include('includes/footer.html'); ?> --------------------------------------------------------------------------------------------------------------------------------------------------------------- I put the codes that you advised me. You can look at the codes in bold. Thanks again for your kind cooperation and look forward to hearing from you soon.
  10. Thank you Jonathon, It's very helpful!! I saw the function of mysqli_num_rows() on the chapter 8 as a tip but didn't describe how to use. Now I can see how..
  11. Hi all, I am going through Chapter 8 of "php6 and mysql5" now. I am wondering people can get a custom error message when they register inputting the email address that is already existing in database? I added UNIQUE index on email in mysql so that I can prevent it from having duplicate email addresses. They would get a default error message. However, I want to customize it so that end users only can see the message. Such as "The email address you input is already existing. Please try a different email." I tried to manipulate the codes but kept getting error messages and not working properly. What I did is below(script 8.5 - register.php); ------------------------------------------------------------------------------------------------------------------- . . . $sem = "SELECT email FROM users"; $rc = @mysqli_query($dbc, $sem); //check for an email address; if(!empty($_POST['email'])) { if($_POST['email'] = $rc){ echo"<p>The email address you input is already existing. Please try a different email</p>\n"; }else { $e=mysqli_real_escape_string($dbc, trim($_POST['email'])); } }else{ $errors[]='You forgot to enter your email address'; } . . . . ------------------------------------------------------------------------------------------------------------------ Thank you very much for your help in advance and look forward to hearing from you soon.
×
×
  • Create New...