Jump to content
Larry Ullman's Book Forums

All Activity

This stream auto-updates     

  1. Today
  2. Hi Mr Larry I bought the kindle version of php and mysql and I bought the paper book from amazon uk since 2nd December but still not received and tracking number is not specified the carrier.. One of the reviewer in Amazon said that get the paper version and u will get the Kindle version for free is it right? If so can I get it now since I still waiting for the book to received and I hope that I could read the book from my tab this time Thanks
  3. Thanks, Larry. That was the answer I was hoping for.
  4. Necuima

    Checking MIME Type

    Hi Larry, Now have it working with a Visual Studio 2017 C++ front-end using the same C sharp DLL. It is very similar. The changes are: using namespace FileTypesLib; FileTypes^ fileTypes = gcnew FileTypes(); if (openFileDialog1->ShowDialog() != System::Windows::Forms::DialogResult::Yes) { // weird!! The negative case is the one that works?? String^ theFileName = openFileDialog1->FileName; // save the file name in the text box this->txtSelectedFile->Text = theFileName; // get the MIME type and save it in the form text box txtMIMEType->Text = fileTypes->GetMimeType(theFileName); } Again, it may be of interest to someone. Cheers
  5. I feel like using cookies is fairly standard--and reasonable--these days and is the right approach here. Sessions do also require cookies, or a somewhat complex workaround, and are more demanding of the server. All in all, sessions are a lot of work just to store a single number.
  6. Yesterday
  7. Thanks for sharing that!
  8. Apologies if this is due to a book error, but your query uses "id" while the database table uses "quote_id".
  9. Awesome. Kudos for figuring it out and thanks for letting us know!
  10. I am trying to decide to go with Sessions or Cookies. After reading page 404 about the two choices, it seems cookies are it (I only want to store a single number -which refers to a particular database from which information will be sent to the user). Not having required users to allow cookies in the past, making this website a "multi-user" (users from different geographic areas) system I must now do so. I worry about users not allowing cookies. My research gives varying answers about percentages of users that allow or disallow. What makes it vague is the information I read about does not discriminate between the different type of cookies -third party, tracking, secondary etc. On browsers, people can allow (or not) any of these groups or shut off "all" cookies. I do not want to turn returning users away by shutting them out of the site because they suddenly have to deal with the cookie issue. If I go with Session, every time they log in they will have to make a choice about which geographic area they are from (related to choice of db tables). My single cookie, say, the number 1 as the cookie's value handles everything I need and can last a year. So, what is the risk of losing users by storing a cookie vs the more complex and non viable Session. And, does not starting a Session require the placement of a 'PHPSESSID'? Would not allowing "all" cookies disable the Session? Sorry for the long explanation. Chop
  11. I was able to solve this! I went through my code again and I had the include function commented out on one of my files >.< Sorry, guys. Thank you.
  12. Greetings! I am having this issue (screenshot attached). I am able to add quotes to the database and see them in the database when logging in to phpMyAdmin, but for some reason I am getting this error on the View Quotes page: All Quotes Could not retrieve the data because: Unknown column 'id' in 'field list'. The query being run was: SELECT id, quote, source, favorite FROM quotes ORDER BY date_entered DESC Is there a reason for this? This is the code that I am using for the View All Quotes page: <?php // Script 13.8 - view_quotes.php /* This script lists every quote. */ // Include the header: define('TITLE', 'View All Quotes'); include('includes/header.php'); print '<h2>All Quotes</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('includes/footer.php'); exit(); } // Need the database connection: include('includes/mysqli_connect.php'); // Define the query: $query = 'SELECT id, quote, source, favorite FROM quotes ORDER BY date_entered DESC'; // Run the query: if ($result = mysqli_query($dbc, $query)) { // Retireve the returned records: while ($row == mysqli_fetch_array($result)) { // Print the record: print "<div><blockquote>{$row['quote']}</blockquote>- {$row['source']}\n"; // Is this a favorite? if ($row['favorite'] == 1) { print ' <strong>Favorite!</strong>'; } // Add administrative links: print "<p><b>Quote Admin:</b> <a href=\"edit_quote.php?id={$row['id']}\">Edit</a> <-> <a href=\"delete_quote.php?id={$row['id']}\">Delete</a></p></div>\n"; } // End of while loop. } else { // Query didn't run. print '<p class="error">Could not retrieve the data because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>'; } // End of query IF. mysqli_close($dbc); // Close the connection. include('includes/footer.php'); // Include the footer. ?> I also attached a screenshot of my file directory, and of the added quotes on phpmyadmin site, not sure if that helps. As far as the folder being named "includes" instead of "templates," that was the Professor's call. She wanted us to name that folder that way. Any clarification will be greatly appreciated!
  13. Not sure why I keep getting this error? Could someone please help me. This is the code I used for add_quote.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('includes/header.php'); 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('includes/footer.php'); 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('includes/mysqli_connect.php'); // Prepare the values for storing: $quote = mysqli_real_escape_string($dbc, trim(strip_tags($_POST['quote']))); $source = mysqli_real_escape_string($dbc, trim(strip_tags($_POST['source']))); // Create the "favorite" value: if (isset($_POST['favorite'])) { $favorite = 1; } else { $favorite = 0; } $query = "INSERT INTO quotes (quote, source, favorite) VALUES ('$quote', '$source', $favorite)"; mysqli_query($dbc, $query); if (mysqli_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>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>'; } // Close the connection: mysqli_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('includes/footer.php'); ?> And then the one for functions.php: <?php // Script 13.2 - functions.php /* This page defines custom functions. */ // This function checks if the user is an administrator. // This function takes two optional values. // This function returns a Boolean value. function is_administrator($name = 'Samuel', $value = 'Clemens') { // Check for the cookie and check its value: if (isset($_COOKIE[$name]) && ($_COOKIE[$name] == $value)) { return true; } else { return false; } } // End of is_administrator() function. Could someone please also explain the fact that we had to use Samuel for the $name and Clemens for the $value? I am so confused... I'd really appreciate any clarification! =]
  14. Necuima

    Checking MIME Type

    But I did spend some more time on it! I leveraged the class library kindly provided by Ed Gadziemski (https://www.codeproject.com/Articles/849083/Determining-File-Type-A-Demonstration-of-Different) and developed a small C# UI front-end in Visual Studio 2017. I used C# as that is what Ed's class library is written in. Here's part of the UI/front-end code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using FileTypesLib; namespace ReadFile { public partial class ReadFileCS : Form { FileTypes fileTypes = new FileTypes(); public ReadFileCS() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void btnSelectFile_Click(object sender, EventArgs e) { var filePath = string.Empty; txtMIMETypeBox.Clear(); using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = "e:\\pictures"; openFileDialog.Filter = "image files (*.jpg)|*.jpg|All files (*.*)|*.*"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { //Get the path of specified file filePath = openFileDialog.FileName; txtSelected_File.Text = filePath; btnSelectFile.Focus(); // get the file's MIME type txtMIMETypeBox.Text = fileTypes.GetMimeType(filePath); } // end dialog box opened OK } // end using (OpenFileDialog openFileDialog = new OpenFileDialog()) } // end private void btnSelectFile_Click(object sender, EventArgs e) private void btnExit_Click(object sender, EventArgs e) { this.Close(); } } // end public partial class ReadFileCS : Form } // end namespace ReadFile It may be of interest to someone! Season's Greetings from Oz.
  15. Last week
  16. That's a fine solution! In the other thread I posted what I originally had in mind, though. With programming, there are many ways!
  17. Not a problem to leave it as is. Thanks, though!
  18. No problem. Thanks for sharing what you learned!
  19. Larry, I have found the solution as to why it is. There are 2 different $_SESSION['destroyed'] variables that exist for 2 different sessions. Sorry for the bother...
  20. There is a second function but I couldn't upload it. It is in the manual. The second function is for session_start() and also checks if $_SESSION['destroyed'] is set. Maybe its like a race between the two functions for $_SESSION['destroyed'], I am not really sure.
  21. Hey Larry, Just a quick question about regenerating session id. Why does the php manual set a time stamp on the old session but then quickly deletes it by unsetting the variable. I have attached a screen shot of the example from the php manual website. It is the second example from: http://php.net/manual/en/function.session-regenerate-id.php
  22. It's more straightforward than you might be thinking. Here's how it starts: if ( (isset ($_POST['gender'])) AND ($gender == 'M') ) {
  23. Answered my own question: // Validate the gender: $gender = $_REQUEST['gender'] ?? NULL; if ($gender == 'M') { $greeting = '<p><strong>Good day, Sir!</strong></p>'; } elseif ($gender == 'F') { $greeting = '<p><strong>Good day, Madam!</strong></p>'; } else { // $_POST['gender'] is not set. echo '<p class="error">You forgot to select your gender!</p>'; }
  24. Sorry, I just noticed this section. I placed a Pursue question in the general area. I can't delete or move it so if this is something you can do that would be great. Thanks!
  25. I've been struggling with the following Pursue challenge. I found an eloquent solution in the forum for version 4 but it included a function which will not be discussed until chapter 3. I just can't find a way to echo a greeting based on gender selection without a nested conditional. I would appreciate help on this. Pursue: Rewrite the gender conditional in handle_form.php (Script 2.4) as one conditional instead of two nested ones. Hint: You’ll need to use the AND operator. Here is the nested conditional: // Validate the gender: if (isset($_POST['gender'])) { $gender = $_POST['gender']; if ($gender == 'M') { $greeting = '<p><strong>Good day, Sir!</strong></p>'; } elseif ($gender == 'F') { $greeting = '<p><strong>Good day, Madam!</strong></p>'; } else { // Unacceptable value. $gender = NULL; echo '<p class="error">Gender should be either "M" or "F"! </p>'; } } else { // $_POST['gender'] is not set. $gender = NULL; echo '<p class="error">You forgot to select your gender!</p>'; }
  26. Your solution is eloquent but is this Pursue challenge doable based on what has been learned by the end of Chapter 2? Functions have not been introduced yet. I believe this topic begins in Chapter 3. Sorry, just realized this question was for version 4 and not version 5. So, it is possible version 4 already discussed functions. I would still like to find a solution that does not require a function. I have not been able to get it to work using only conditionals, operators and arrays.
  1. Load more activity
×