Jump to content
Larry Ullman's Book Forums

All Activity

This stream auto-updates

  1. Last week
  2. Yeah, that'll do it. Kudos for figuring it out and thanks so much for sharing the solution!
  3. Hi Larry, I have resolved on my own (really with the aid of stackoverflow). It's a cache problem. I have substituted my css link tag with him: <link href="css/sticky-footer-navbar.css?v=<?php echo time(); ?>" rel="stylesheet"> to force to use the fresh version (surely I could to empty the cache too). Or I could include the css like that: <style> <?php include "css/sticky-footer-navbar.css" ?> </style> However those above are not the best solutions because force ever the browser to make an http request to download the css file (thus the user will never use the cache copy though if my css has not been changed). The best way is to use: <link href="css/sticky-footer-navbar.css?v=1" rel="stylesheet"> And when I change the css file I have to remember to do: <link href="css/sticky-footer-navbar.css?v=2>" rel="stylesheet"> to force the download instead of the cache version. I haven't closed the post because could be useful to someone else. This link https://stackoverflow.com/questions/50662906/my-css-file-not-working-in-my-php-file helped me Regards Nicola
  4. I was trying to solve the second exercise (Pursue) of the third chapter, namely to add a textarea and a checkbox. I haven't created a new form, I have reused your form, I have simply added the textarea and made it a sticky form again (My textarea is deliberately optional). But I encountered a problem that was a very "pain in the ass" (I'm not a native english, it is a dirty word? sorry if is this the case 😇). I tried to style my textarea, with a background-color:red and a resize:vertical properties in css/sticky-footer-navbar.css but they don't work. If I give them as inline style instead they work. I'm not able to figure out what is the problem, may be I'm missing something. There is the attached code, it's very very similar to your code. Little change described above. Please, can you help me to understand where I'm wrong? Thanks in advance Nicola n2.zip
  5. Hey, Seifer, replying way after the fact. As a general flow, you'll probably want to have a function that renders all the cards based on what's in the array, another function for adding items to the array and a third function for deleting things. Lots of pseudo-code here, but the render function would be something like the following: const render = () => { let html = ''; html += '<ul>'; items.forEach((item) => { html += `<li> // More HTML here for each card </li>`; }); html += '</ul>'; document.querySelector('wherever-you-want-to-put-the-html').innerHTML = html; }; Then, every time a change is made to the items array, you simply call the render function and it does everything for you. This is very similar to how React/Vue, etc. work behind the scenes. The add and remove functions would be something like the following: const add = (item) => { items.push(item); render(); }; const remove = (idx) => { items = items.filter((item, i) => i !== idx); render(); }; Again, being very rough here, but that's the basic flow, and everything else goes around this. You can style things and add whatever functionality you want on top of this. Good luck!
  6. Earlier
  7. It might be best if you start coding by documenting what you want to do and then convert those comments into code. For example: Add a record to the products table. Get the product ID for the record just added. If there are sizes, add one record to product2size for each size. This uses the product ID already fetched and the size ID, which comes from the HTML. If there are product categories, add one record to product2pcat for each category. This uses the product ID already fetched and the category ID, which comes from the HTML. So with that written out like so you can see for starters that you don't need to fetch the product ID multiple times, just once will do. You'll also see that steps 3 & 4 are parallel, not dependent upon each other (from what I can tell). But your code for step 4 includes "isset($sizeError)". I don't see why that's there. That may be why the third query isn't running? From the code and your comments it looks like the second and third queries could be executed in either order as neither depends upon the other. There's nothing particularly special about running these three queries. I think you have some logic issues. As I recommended before, if you use a slew of print statements you could see what is or is not true or happening. It's a blunt debugging tool, but it works. Another likely cause of confusion is you're using both prepared statements and non-prepared statements. That's not really a good idea, from the point of comprehension. Moreover, you're not using prepared statements for the two queries that would benefit the most from the prepared statements. By using prepared statements the second and third queries can be executed within the foreach loops, nicely.
  8. Sir, You are right. what I don't know is how to insert values into the third table that is why I repeated what I did in the second table. I also want to make use of proudict_id in the third table. If you can assist with this explanation I will be glad.
  9. One issue I can see is that you call `$product_id = $stmt->insert_id;` a second time, but you already have the $product_id value, so that shouldn't be necessary. And there's a good chance `$stmt->insert_id` won't return anything a second time it's called, but I could be wrong about htat.
  10. Dear larry, thanks very much for your reply. It did not give me any error on the screen it just said it was inserted successfully but when I check the PHPmyadmin, the first two queries inserted values to the product table and the product2size table which is the first cross reference table for a particular product which has many sizes in the sizes table, but the second cross-reference table which is product2pcat table which also supposed to get the insert product_id of the same product and many catecories_id which a product belong to. i know I should use the primary key of the last->insertid but since I also want it to insert the primary key of the product table the product2pcat table. My question is this what should I do for the third table that is product2pcat cross reference table to have values when I click the submit button on the page.
  11. Could you be more specific than "the query did not run"? Did PHP attempt to execute the query but it didn't succeed? And, if so, what was the MySQL error? Or is it not getting to that point in your code where PHP is even trying to execute the query? You may want to litter your code with print statements to see what is or isn't happening, and what values exist where.
  12. Dear All, I was trying to insert records into multiple tables in one file. The first two queries ran very well but the third one did not run. The first table is normal table while the last 2 tables are cross-reference tables, which store primary keys of two tables each. Here is the code snippets for your help: // initialize prepared statement $stmt = $con->stmt_init(); $insert_product = "INSERT INTO products (cat_id, manufacturer_id,date,product_title,product_url,product_img1,product_img2,product_img3,product_price,product_keywords,product_desc,product_features,product_video,seo_keywords,product_label,product_sale ) VALUES(?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; // bind parameters and execute statement if ($stmt->prepare($insert_product)) { // bind parameters and execute statement $stmt->bind_param('iisssssisssssss', $cat, $manufacturer_id, $product_title, $product_url,$product_img1,$product_img2,$product_img3,$product_price,$product_keywords,$product_desc,$product_features,$product_video, $product_seo,$product_label,$product_sale ); $stmt->execute(); if ($stmt->affected_rows > 0) { echo "<script>alert('Product has been inserted sucessfully')</script>"; } } // if the product entry was inserted successfully, check for sizes if (($stmt->affected_rows > 0) && isset($_POST['size'])) { // get the product's primary key $product_id = $stmt->insert_id; foreach ($_POST['size'] as $size_id) { if (is_numeric($size_id)) { $values[] = "($product_id, " . (int) $size_id . ')'; } } if ($values) { $sql = 'INSERT INTO product2size (product_id, size_id) VALUES ' . implode(',', $values); // execute the query and get error message if it fails if (!$con->query($sql)) { $sizeError = $con->error; } } } // third query // if the product entry was inserted successfully, check for product categories if (!isset($sizeError) && isset($_POST['product_cat'])) { // get the product's primary key $product_id = $stmt->insert_id; foreach ($_POST['product_cat'] as $p_cat_id) { if (is_numeric($p_cat_id)) { $values[] = "($product_id, " . (int) $p_cat_id . ')'; } } if ($values) { $sql = 'INSERT INTO product2pcat (product_id, p_cat_id) VALUES ' . implode(',', $values); // execute the query and get error message if it fails if (!$con->query($sql)) { $pcatError = $con->error; } } } My problem is that I wouldn’t know what I did wrong that is preventing the third query to run.
  13. Yes, there's a typo there that's my fault--sorry!--in that you should be selecting the password from the database, too. You have that in place in your code now. From your debugging work it seems that if (password_verify($p, $row['pass'])) { is returning false. That means the problem is either with $p or with $row['pass']. I would start by registering a new user with a new password--just "password"--and testing that, just to rule out that you haven't mistakenly used the wrong password (i.e., the code is actually working properly!). I'd also print out the value of password_hash('your password') and make sure that entire value is being stored in the database (using phpMyAdmin or the like).
  14. Ah, your code uses `if define()` when it should be `if defined()`. That's what your error means: define() requires two arguments, the constant name and the value, but you're only providing one (because you meant to use defined()).
  15. Hey CMGR, I think you're missing a set of parenthesis in your form validation. In the code you copied, you have: if (isset $_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) But it should be as follows: if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) { Notice after the isset() you are missing the opening parenthesis (before $_POST['distance'], but you have the closing one after $_POST['efficiency']. Your also missing the final closing parenthesis after the '&& is_numeric($_POST['efficiency'])' -- there should be a second parenthesis. The parenthesis before the isset() and at the very end should essentially be 'wrapping itself' around all six of the validators (ie everything you're checking is TRUE via your IF statement). I also noticed in your html inputs, it you have this code: <p>Fuel Efficiency: <select name="efficiency"> <option value-"10">Terrible</option> <option value-"20">Decent</option> <option value-"30">Very good</option> <option value-"50">Outstanding</option> </select></p> HTML values, I believe, always need to be assigned with the equals (=) operator, so it should be like this: <p>Fuel Efficiency: <select name="efficiency"> <option value="10">Terrible</option> <option value="20">Decent</option> <option value="30">Very Good</option> <option value="50">Outstanding</option> </select></p> If you're getting this error: echo 'Please enter a valid distance, price per gallon, and fuel efficiency' it could be because the HTML inputs are not able to interpret the values of the radio options. Another cause could be because you have inconsistent naming in your doc. In this line: $dollars = $gallons * $_POST['gallon-price']; you name $_POST['gallon-price']; but in your HTML, you have it as 'gallon_price' -- which matches your PHP validation. Again, the HTML and PHP probably aren't able to communicate values when the naming is inconsistent or has an error. You should change that array value to 'gallon_price', so it looks like so: $dollars = $gallons * $_POST['gallon_price']; Lastly, and this may be nothing, in your form action, you have: <form action="calculator1.php" method="post"> is that the name of the file on your server? One other note, I noticed you said you use Dreamweaver -- I'd highly recommend downloading a text editor like Visual Studio Code, which will let you compare your code to Larry's code on the book's github. I haven't heard of anyone using DW in a the last several years, and VS Code is a free download with lots of plugins and extensions. It will highlight discrepancies which makes debugging and looking for syntactical errors much faster. I'd also recommend downloading and installing MAMP/LAMP and running your server via that application as well. Hopefully you're able to make the changes and get moving foward!
  16. I changed the isset line and removed the commas, replaced them with && and included the whole isset() block in a paraenthesis set,. I removed the placeholder code in the html form. The page now loads, but it is now only displaying the else clause, so it's still not working correctly.
  17. I am trying to do your tutorials and getting an HTTP 500 error. It is coming from this script. it is showing me errors in the php block (not the HTML) in Dreamweaver. It is coming from the isset() and the else lines. Do you see an error? The html will work without it. It is currently part of another page, and the rest of it is fine. I have tried to type it as it was shown in the book, although I tried a few other versions, but still got the same result. Should this use $_GET[] instead? <?php // Check for form submission if($_SERVER['REQUEST_METHOD'] == 'POST') { //Minimal form validation: if (isset $_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) { // Calculate the results $gallons = $_POST['distance'] / $_POST['efficiency']; $dollars = $gallons * $_POST['gallon-price']; $hours = $_POST['distance']/65; //65 mph //Print results echo '<div class="error"><h2>Total estimated cost</h2></div> <p>The total cost of driving is ' .$_POST['distance']. ' miles, averaging ' .$_POST['efficiency']. 'miles per gallon, and paying an average of $'.$_POST['gallon-price']. 'per gallon, is $'.number_format($dollars, 2). '. If you drive an average of 65 mph, the trip will take approximately '. number_format($hours, 2). 'hours.</p>';} else { //invalid submitted values. echo '<div class="error"><h2>Error</h2><p>Please enter a valid distance, price per gallon, and fuel efficiency.</p></div>'; } } //leave PHP section, create HTML form below. ?> <section class="calculator"> <h2>Trip Cost Calculator</h2> <form action="calculator1.php" method="post"> <p>Distance (in miles). <input type="number" name="distance" placeholder="10"></p> <!-- add required to make it not submit unless it is filled out --> <p>Avg. Price Per Gallon: <input type="radio" name="gallon_price" value="3.00"> 3.00 <input type="radio" name="gallon_price" value="3.50"> 3.50 <input type="radio" name="gallon_price" value="4.00"> 4.00 </p> <p>Fuel Efficiency: <select name="efficiency"> <option value-"10">Terrible</option> <option value-"20">Decent</option> <option value-"30">Very good</option> <option value-"50">Outstanding</option> </select></p> <p><input type="submit" name="submit" value="Calculate!"></p> </form>
  18. Hi, I get the following error and a blank page trying to print the date and time, and I can't seem to find what I did wrong: error: <br /> <b> fatal error </b>: uncaught ArgumentCountError... it seems to happen when I replace the title with the PHP script (which includes the constant) in the header. When I remove that PHP script and leave it like it was in the beginning, I get the result the in screenshot and the date and time printed look strange. Regards Yannick
  19. Hey, I notice this thread is from 2018, but I can't seem to find any other information on it -- just let me know if I should make a new thread. Basically, I'm having the exact same problem as the OP from 2018 -- I've worked through ch. 13, and can add/register a new user by using the 13.7 password_hash() function. I've checked in phpMyAdmin and can see the new user, the hashed password, etc. I can also update the user from the edit page as well. For reference, I'm on PHP Version 7.4.12. Basically, when trying to do the login function from 13.8, I keep getting 'Error - the email address and password do not match what we have on file.' I've tested this with a few different usernames and pw's, and can assure the passwords are correct, but I keep getting the error. I've also tested the error reporting, and can say for certain the error is not being thrown by the if/else statement here: if (mysqli_num_rows($r) == 1) ...... } else { //Not a match $errors[] = 'The email testing address and password entered do not match those on file.'; But rather, the error is coming from here: if (password_verify($p, $row['pass'])) { unset($row['pass']); return [true, $row]; } else { $errors[] = 'The email testing2 address and password entered do not match those on file.'; } So, in the book, I noticed on the text of pg. 455 (5th edition) that it says to input this: $q = "SELECT user_id, first_name, pass FROM users WHERE email='$e'"; however in the visual examples, it says to use this code, which is also how the file on github has the code for 13.8 as well: $q = "SELECT user_id, first_name FROM users WHERE email='$e'"; I've tried running the code both with and without the 'pass' value in the SELECT function, but I keep getting the error. I'll post my code below, but basically, I'm just trying to see if there's a definite answer for what I'm doing wrong. In the thread above, someone mentioned needing the 'pass' and it makes sense, but there's conflicting information between the book elements, github, and the prior post. For reference, I've also compared my .php file to the 13.8 file on github, but can't seem to find the mistake I'm making on this one. One last note, I also tried commenting out the code from 'unset($row['pass'])) to see if that would help, but that also does not seem to be causing the issue. Here's my code as of now: <?php #Script 12.2 - login_functions.inc.php function redirect_user($page = 'index.php') { //start defining the URL... //URL is http:// plus the host name plus the current directory: $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); //remove any trailing slashes: $url = rtrim($url, '/\\'); //Add the page: $url .= '/' . $page; //Redirect the user: header("location: $url"); exit(); //Quit the script. } //End of redirect_user function. function check_login($dbc, $email = '', $pass = '') { $errors = []; //Initialize error array if (empty($email)) { $errors[] = 'You forgot to enter your email address.'; } else { $e = mysqli_real_escape_string($dbc, trim($email)); } if (empty($pass)) { $errors[] = 'You forgot to enter your password.'; } else { $p = trim($pass);//$p = mysqli_real_escape_string($dbc, trim($pass)); } if (empty($errors)) { $q = "SELECT user_id, first_name, pass FROM users WHERE email='$e'"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array($r, MYSQLI_ASSOC); if (password_verify($p, $row['pass'])) { unset($row['pass']); return [true, $row]; } else { $errors[] = 'The email testing2 address and password entered do not match those on file.'; } //return [true, $row]; } else { //Not a match $errors[] = 'The email testing address and password entered do not match those on file.'; } } //End of empty($errors) IF return [false, $errors]; } //End of check_login function. I really appreciate your help on this. Chapter 14 has been great so far, I just wanted to see if I could get some clarity on this matter before I forget about the issue coming up. Thanks Again in Advance!
  20. Ah! Kudos for figuring it out and thanks for sharing what the cause was!
  21. Thank you Larry for your help. Your solution worked perfectly in the interim! After changing the storage engine to MyISAM and attempting other workarounds, the problem still incurred. Later I realised for a specified key to be too long, the charset used in the client must be consuming too many bytes and could therefore not be UTF8. The problem was due to my accidental omission of: CHARSET UTF8; I would have expected unusual characters to be stored in the database if the charset were wrong however this was an error thrown before any database population could have taken place. Cheers and thanks again, MF
  22. Thanks for the nice words. Yeesh on this error message, though! That command creates four keys (or indexes): on user_id, on username, on email, and on the combination of username and pass. None of those seems to be 100 bytes! My hunch is it's the login key that's causing the problem. Try removing that from the command (and delete the comma after `UNIQUE (email)` and try it again.
  23. Hello, I would appreciate any assistance I can receive. While following the instructions provided in chapter 17 to create the users table for the forum2 database (page 553), I had encountered the following error: CREATE TABLE users ( -> user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, -> lang_id TINYINT UNSIGNED NOT NULL, -> time_zone VARCHAR(30) NOT NULL, -> username VARCHAR(30) NOT NULL, -> pass VARCHAR(255) NOT NULL, -> email VARCHAR(60) NOT NULL, -> PRIMARY KEY (user_id), -> UNIQUE (username), -> UNIQUE (email), -> INDEX login (username, pass) -> ); Error message: #1071 - Specified key was too long; max key length is 1000 bytes Some additional information: Currently using paid hosting. My database character set is: utf8mb4 My database collation is: utf8mb4_unicode_ci All tables use: InnoDB. I would like to please know how this error can be resolved to continue proceeding with the chapter. Enjoying the book! Many thanks, MF.
  24. You're most welcome. I'm glad to hear it and thanks for posting this!
  25. This is now in production. I've attached the code for anyone else who might be in a similar situation. if ($local) { // development echo <<<DD1 <!-- DEVELOPMENT TEST!!!--> <div class="paypal_box"> <!-- right-shifts the div --> <!-- first the PayPal Logo - see: https://www.paypal.com/us/webapps/mpp/logo-center --> <img class="PP_logo" src="./images/PP_logo.jpg" alt="PayPal Logo"> <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="xxx@yyy.com.au"> <input type="hidden" name="item_name" value="Selected Items"> <input type="hidden" name="item_number" value=""> <input type="hidden" name="amount" value="$total"> <input type="hidden" name="handling" value="0.00"> <input type="hidden" name="shipping" value="0.00"> <input type="hidden" name="currency_code" value="AUD"> <input type="hidden" name="lc" value="AU"> <input type="hidden" name="bn" value="PP-BuyNowBF"> <input type="hidden" name="add" value="1"> <input type="image" src="./images/PayPal_Button.jpg" alt="Submit" width="100"> <!-- the submit button --> </form> </div> <!-- end paypal_box class div --> <br /><br /> DD1; } // end development else { // production - see: https://cvconnect.commercevision.com.au/display/KB/PayPal+Payments echo <<<DD2 <!-- PRODUCTION!!!--> <div class="paypal_box"> <!-- right-shifts the div --> <!-- first the PayPal Logo - see: https://www.paypal.com/us/webapps/mpp/logo-center --> <img class="PP_logo" src="./images/PP_logo.jpg" alt="PayPal Logo"> <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="xxx@yyy.com.au"> <input type="hidden" name="item_name" value="Selected Items"> <input type="hidden" name="item_number" value=""> <input type="hidden" name="amount" value="$total"> <input type="hidden" name="handling" value="0.00"> <input type="hidden" name="shipping" value="0.00"> <input type="hidden" name="currency_code" value="AUD"> <input type="hidden" name="lc" value="AU"> <input type="hidden" name="return" value="https://xxx/index.php?p=thanks_3"> <input type="hidden" name="cancel_return" value="https://xxx/index.php?p=cancel_3"> <input type="hidden" name="bn" value="PP-BuyNowBF"> <input type="hidden" name="add" value="1"> <input type="image" src="./images/PayPal_Button.jpg" alt="Submit" width="100"> <!-- the submit button --> </form> </div> <!-- end paypal_box class div --> <br /><br /> DD2; } // end production You will recognise the "$local" from one of your other books 🙂 As an aside, when obtaining guidance from your E-Commerce book, I used PDO for all database activities and that has worked fine. All the best, Necuima
  26. Hi Larry, I just want to say "Thanks" for the guidance from this book and also the E-Commerce book. I followed a lot of the e-commerce guidance for a shopping cart, PayPal and used MySQL joins as per this book and eventually all went well. Cheers from Oz, Necuima
  1. Load more activity
×
×
  • Create New...