Jump to content
Larry Ullman's Book Forums

AprilSwenby

Members
  • Posts

    85
  • Joined

  • Last visited

AprilSwenby's Achievements

Newbie

Newbie (1/14)

1

Reputation

  1. I too had this question and I thank you for your answer - I am trying to find a way to answer the first review question on chapter 414. Do you mean - how would we re-write rather than how would the the function be called?
  2. Part of my problem was that on page 394, the written instructions did not say to type lines 22 and 23.... so I wasn't included the database connect file. I have done that now, and Now I am getting a message that says... Could not store the quote because: No database selected. The query being run was: INSERT INTO quotes (quote, source, favorite) VALUES ('test', 'test', 0) That's why I think I either have something typed wrong in my table or things aren't stored in the right directories.... I have debugged and I am connected to the database…. it still seems to be having a problem selecting the database….. I have attached my add_quotes script and my mysql_connect.php script and following is my table. Right now, I have my mysql_connect stored in the xamp folder, my add_quotes stored in the htdocs folder and I have a myquotes.php (which is my table) in both the xamp folder and in the htdocs folder, as I was trying to figure out if I had it in the right directory…. add_quotes.php <?php // Script 13.7 - add_quote.php /* This script adds a quote. */ // Define a page title and include the header: define('TITLE', 'Add a Quote'); include('templates/header.html'); print '<h2>Add a Quotation</h2>'; // Restrict access to administrators only: if (!is_administrator()) { print '<h2>Access Denied!</h2><p class="error">You do not have permission to access this page.</p>'; include('templates/footer.html'); exit(); } // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form. if ( !empty($_POST['quote']) && !empty($_POST['source']) ) { // Need the database connection: include('../mysql_connect.php'); // Prepare the values for storing: $quote = mysql_real_escape_string(trim(strip_tags($_POST['quote'])), $dbc); $source = mysql_real_escape_string(trim(strip_tags($_POST['source'])), $dbc); // Create the "favorite" value: if (isset($_POST['favorite'])) { $favorite = 1; } else { $favorite = 0; } $query = "INSERT INTO quotes (quote, source, favorite) VALUES ('$quote', '$source', $favorite)"; $result = mysql_query($query, $dbc); if (mysql_affected_rows($dbc) == 1){ // Print a message: print '<p>Your quotation has been stored.</p>'; } else { print '<p class="error">Could not store the quote because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>'; } // Close the connection: mysql_close($dbc); } else { // Failed to enter a quotation. print '<p class="error">Please enter a quotation and a source!</p>'; } } // End of submitted IF. // Leave PHP and display the form: ?> <form action="add_quote.php" method="post"> <p><label>Quote <textarea name="quote" rows="5" cols="30"></textarea></label></p> <p><label>Source <input type="text" name="source" /></label></p> <p><label>Is this a favorite? <input type="checkbox" name="favorite" value="yes" /></label></p> <p><input type="submit" name="submit" value="Add This Quote!" /></p> </form> <?php include('templates/footer.html'); ?> table as instruction on page 390 called myquotes <?php $dbc = mysql_connect('localhost', 'root', 'spring'); $query = 'CREATE TABLE quotes ( quote_id INT UNSIGNED NOT NULL AUTO_INCREMENT, quote TEXT NOT NULL, source VARCHAR(100) NOT NULL, favorite TINYINT(1) UNSIGNED NOT NULL, date_entered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (quote_id))'; ?> My mysql_connect.php file which has been debugged and I am connected. <?php $dbc = mysql_connect('localhost', 'root', 'spring'); mysql_select_db('myquotes', $dbc); ?>
  3. Yes - thank you I did fix that error right away - that is not my problem.
  4. Hi Larry - I am following along in the book - and I am on page 393 when we begin to add quotes. When I add a quote, the page returned sends back these errors: Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 18 Warning: mysql_real_escape_string() expects parameter 2 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 18 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 19 Warning: mysql_real_escape_string() expects parameter 2 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 19 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 28 Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 28 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 30 Warning: mysql_affected_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 30 Notice: Undefined variable: dbc in C:\xampp\htdocs\add_quote.php on line 33 Warning: mysql_error() expects parameter 1 to be resource, null given in C:\xampp\htdocs\add_quote.php on line 33 Could not store teh quote because: . The query being run was: INSERT INTO quotes (quote, source, favorite) VALUES ('', '', 0) That makes me think I do not have my table set up properly.... Page 380 didn't give a whole lot of details as to how to set up but here is what my table looks like: <?php $dbc - mysql_connect('localhost', 'root', 'spring'); if ($dbc) { $query = 'CREATE TABLE quotes ( quote_id INT UNSIGNED NOT NULL AUTO_INCREMENT, quote TEXT NOT NULL, source VARCHAR(100) NOT NULL, favorite TINYINT(1) UNSIGNED NOT NULL, date_entered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (quote_id))'; } mysql_close($dbc); ?> Any ideas why this isn't working?
  5. Yes! That worked for me too! So it seems I had it right at one point - but just had it in the wrong place! How frustrating! I've been working on this for over a week now.... Thank you so much for pointing that out to me.
  6. Hi Larry - I took a break like you said, and I feel like I'm so close! But...... This script returns about 100 lines of But that's because I've been entering 'april' for the last 100 times I've been testing the script. I tried a new name and the first time I entered the name it said that I've been registered, and the second time it told me This username is already used. Please enter a unique username! You are now registered! So it's returning two of my statements that contradict each other. This is further than I've ever gotten before - because I could never get it to return that at all - but why is it repeating? I have the break; for the while Also I did this print "<p>line = -{$line}-</p>"; and found that line = -Array-. I don't even know what that means - but I think that's good - because then my text file is now in an array, but it's not split by the delimiter like I want. So to sum it up - it seems to be returning the message This username is already used. Please enter a unique username! for as many times as that name is entered and it also contradicts that message at the end by saying You are now registered! <!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. */ error_reporting(E_ALL); // 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. ini_set('auto.detect_line_endings', 1); $open = fopen($file, 'r');//Check if username is duplicate: while($line = fgetcsv($open, 500, "\t")) {//Loop through the file: for($n=0; $n < count($line); $line++) { break; }//End of For if( ($line[0] == $_POST['username']) && ($line[0] == $_POST['username'])){ print '<p class="error">This username is already used. Please enter a unique username!</p>'; } }//End of while fclose($open); // 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="registerpursue.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>
  7. I am a little closer think. I used this code ini_set('auto_detect_line_endings', 1); $open = fopen($file, 'r'); //opens my data file while ($line = fgetcsv($open, 200, "\t" )) { //breaks my string from $file into parts if ($_POST['username'] == ($line[0])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement } break; } fclose($open); This code was debugged and I ran the form, I rec'd this as the return... That was telling me that it wasn't looking throughout the text file and was only looking at just the first word in the text file. Then Larry I have been practicing your debugging techniques and I read through the chapter on arrays again. Here is the code I have written... ini_set('auto_detect_line_endings', 1); $open = fopen($file, 'r'); //opens my data file while ($line = fgetcsv($open, 200, "\t" )) { //breaks my string from $file into parts for($n=0; $n <= count($line); $n++) { if ($_POST['username'] == ($n[0])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement } } break; } fclose($open); The problem is that - it is not recognizing unique usernames. I can enter april in as my username 100 times and it will always tell me that I've been registered. When i debugged this using this line print "<p>line[n] = -{$line[$n]}-</p>"; My output was this. (the notice is a repercussion of this line print "<p>line[n] = -{$line[$n]}-</p>"; so I'm not worried about that. So from this debugging - I am getting the feeling that it's not scanning my entire text file. It's only scanning the first line. Register line[n] = -April- line[n] = -2a2d595e6ed9a0b24f027f2b63b134d6- line[n] = -13214649081347- Notice: Undefined offset: 3 in C:\xampp\htdocs\registerpursue.php on line 59 line[n] = -- You are now registered! So my question is - how do I get this script to read and scan every word in the text file? that is what I thought i was doing with the for loop - but it's not doing that.... It only took the first 3 words. I really don't mean to keep bothering the forum lines, but I really do want to understand this.....
  8. Hi Larry - Thank you for your advice. I did apply those debugging techniques. I found that username is correct. I also found that $line is returning the first word in my text file - so it really not "looping" through the text file to find matches. That is helpful information. So the question is - how do I get things to loop through - I thought that is what my count() was doing. Can you direct me that way?
  9. Well - that didn't make sense either! I don't want the username compared to the passwords... So then I tried this... And my script worked without errors, but I am still able to enter duplicate usernames.... HELP! ini_set('auto_detect_line_endings', 1); $open = fopen("$file", 'r'); //opens my data file while ($line = fgetcsv($open, 20, "\t" )) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[0])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement } break; } fclose($open);
  10. or maybe this would be better...Still gives me an error Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\registerpursue.php on line 59 - but is this more on the right track? ini_set('auto_detect_line_endings', 1); $open = fopen("$file", 'r'); //opens my data file while ($line = fgetcsv($open, 20, "\t" )) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[0] $line[1] $line[2])){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement break; } } fclose($open);
  11. Thank you lingolatz! This helped immensly - however, I am still a little unclear when trying to follow your direction on the Here is what I have based on what you stated.... <!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. */ error_reporting(E_ALL); // 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. ini_set('auto_detect_line_endings', 1); $open = fopen("$file", 'r'); //opens my data file while ($line = fgetcsv($open, 20, "\t" )) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[0]){ $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement if($_POST['password1'] == ($line[1]) { $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement if($_POST['password2'] == ($line[2]) {//checks to see if user entered data matches what's in the text file $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement break; } } fclose($open); // 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="registerpursue.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> You also said I have an unneeded parentheses to surround '$line[$n]'.... ($_POST['username'] == ($line[$n])) Isn't that correct syntax? The parantheses surrounds the whole thing and then opening and closing ones for ($line[$n])?
  12. Yes - that is exactly what the problem was! Thanks! I still can't get the script to recognize a duplicate user name though! I just don't know what I am doing wrong.... I feel like I've tried every method.....
  13. Hello all - I am having a huge stumper. I have have been diligently working on the Pursue question for chapter 11 that requires the script to guarantee unique usernames in register.php Here is my script. I just can't figure out what I am doing wrong and why this won't work for me. I have modified it so many times! Here is what I have right now. This is what is being returned with this script: Register You are now registered! Warning: fgetcsv(): 3 is not a valid stream resource in C:\xampp\htdocs\registerpursue.php on line 55 <!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. */ error_reporting(E_ALL); // 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. ini_set('auto_detect_line_endings', 1); $open = fopen("$file", 'a+'); //opens my data file while ($line = fgetcsv($open, 200, "\t" )!== FALSE) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[$n])) {//checks to see if user entered data matches what's in the text file $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement break; } fclose($open); // 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="registerpursue.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>
  14. Jonathon - Here is my error now - I've been working on it non - stop . Found out that users directory was missing a slash. Argh... Anyway. I now have an error to work with - Warning: fgetcsv() expects parameter 1 to be resource, string given in C:\xampp\htdocs\registerpursue.php on line 54 <!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. */ error_reporting(E_ALL); // 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. ini_set('auto_detect_line_endings', 1); $open = fopen('$file', 'a+'); //opens my data file while ($line = fgetcsv($file, 200, "\t" )) { //breaks my string from $file into parts $n = count($line); if ($_POST['username'] == ($line[$n])) //checks to see if user entered data matches what's in the text file if($_POST['password1'] == ($line[$n])) //checks to see if user entered data matches what's in the text file if($_POST['password2'] == ($line[$n])) //checks to see if user entered data matches what's in the text file { $problem = TRUE; print '<p>That username has been taken, please try again</p>'; //if above is true, print this statement break; } fclose($open); // 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="registerpursue.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> I am stumped still. You had indicate I wasn't using my function right. What did you mean by that?
×
×
  • Create New...