phpstuff Posted August 17, 2013 Share Posted August 17, 2013 I coded all for Ch 13 and went to run through everything and I'm having DB related errors pop up. I provided the latest below. At first I thought I had the mysql_connect_ch13.php script setup wrong with regards to directory location. In my htdocs I have all the main files for ch13, the header/footer template files in templates folder (works fine), and functions_ch13.php in the includes folder(works fine too I believe). When I call the mysql_connect_ch13.php, I have it listed as a file by itself without dots or slashes which I believe is correct (I didn't put it in a different directoy than all the other scripts to keep things simple for now). As you see below the issue is with the script not recognizing a DB. I went through all scripts and made sure that I called the DB "myquotes" everywhere. I also think that I called the table "myquotes" for simplicity. I ran the create_db_ch13.php script more than once thinking that the first time I created a DB named "quotes" and not "myquotes." Anyways if anybody sees anything that sticks out please let me know, otherwise I'll probably just start from scratch on this chapter as well... Thanks! My Site of Quotes Add a QuotationCould not store the quote because:Table 'myquotes.myquotes' doesn't exist. The query being run was: INSERT INTO myquotes (quote, source, favorite) VALUES ('This is a test quote', 'someguy', '1') Quote Source Is this a favorite? Site AdminAdd Quote <-> View All Quotes <-> Logout Content © 2013 <!DOCTYPE HTML> <html> <head> <title>Create the database</title> </head> <body> <?php //Script 13.0 - create_db_ch13.php //This script connects to the MySQL server, creates and selects the database. //Attempt to connect to MySQL and print out messages: if ($dbc = @mysql_connect('localhost', 'root', '')) { print '<p>Successfully connected to MySQL!</p>'; // Try to create the database if (@mysql_query('CREATE DATABASE myquotes', $dbc)) { print '<p>The database has been created!</p>'; } else { //could not create it print '<p style="color: red;">Could not create the database because:<br />' . mysql_error($dbc) . '.</p>'; } // Try to select the database: if (@mysql_select_db('myquotes', $dbc)) { print '<p>The database has been selected!</p>'; } else { // could not select it print '<p style="color: red;">Could not select the database because:<br />' . mysql_error($dbc) . '.</p>'; } mysql_close($dbc); //close the connection } else { print '<p style="color: red;">Could not connect to MySQL:<br />' . mysql_error() . '.</p>'; } ?> </body> </html> <?php // Script 13.7 - add_quote_ch13.php // This script adds a quote // Define a page title and include the header define('TITLE', 'Add a Quote'); include('templates/header_ch13.html'); print '<h2>Add a Quotation</h2>'; // Restrict access to only Administrators if (!is_administrator()) { print '<h2>Access Denied</h2> <p class="error">You do not have access to this page.</p>'; include('templates/footer_ch13.html'); exit(); } // Check for a form submission if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ( !empty($_POST['quote']) && !empty($_POST['source'])) // Need the database connection (script) ======== IS THIS THE CORRECT DIRECTORY STRUCTURE? include('mysql_connect_ch13.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 box is checked then it = 1) if (isset($_POST['favorite'])) { $favorite = 1; } else { $favorite = 0; } $query = "INSERT INTO myquotes (quote, source, favorite) VALUES ('$quote', '$source', '$favorite')"; $r = 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_ch13.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" /></label></p> <p><input type="submit" value="Add this quote!" /></p> </form> <?php include('templates/footer_ch13.html'); ?> <?php // Script 13.11 - index_ch13.php /* This is the homepage for the site. It displays: - The most recent quote (default) - OR, a random quote - OR, a random favorite quote */ //Include header include('templates/header_ch13.html'); //Need DB connection include('mysql_connect_ch13.php'); //Define the query //Change the particulars depending upon values passed in the URL: if (isset($_GET['random'])) { $query = 'SELECT quote_id, quote, source, favorite FROM myquotes ORDER BY RAND() DESC LIMIT 1'; } elseif (isset($_GET['favorite'])) { $query = 'SELECT quote_id, quote, source, favorite FROM myquotes WHERE favorite=1 ORDER BY RAND() DESC LIMIT 1'; } else { $query = 'SELECT quote_id, quote, source, favorite FROM myquotes ORDER BY date_entered DESC LIMIT 1'; } //Run the query if ($r = mysql_query($query, $dbc)) { //Retrieve the returned record: $row = mysql_fetch_array($r); //Print the record print "<div><blockquote>{$row['quote']}</blockquote>- {$row['source']}"; // Is this a favorite? if ($row['favorite'] == 1) { print ' <strong>Favorite</strong>'; } // Complete the DIV print '</div>'; // If the admin is logged in, display admin links for this record: if (is_administrator()) { print "<p><b>Quote Admin:</b> <a href=\"edit_quote_ch13.php?id={$row['quote_id']}\">Edit</a> <-> <a href=\"delete_quote_ch13.php?id={$row['quote_id']}\">Delete</a> </p>\n"; } } else { //Query didn't run. print '<p class="error"<Could not retrieve the data because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>'; } // End of query IF. mysql_close($dbc); //Close the connection print '<p><a href="index_ch13.php">Latest</a> <-> <a href="index_ch13.php?random=true">Random</a> <-> <a href="index_ch13.php?favorite=true">Favorite</a></p>'; include('templates/footer_ch13.html'); // Include the footer ?> <?php // Script 13.1 - mysql_connect_ch13.php // This script connects to, and selects the database // Connect and select: if ($dbc = @mysql_connect('localhost', 'root', '')) { // Handle the error if the database couldn't be selected: if (!@mysql_select_db('myquotes', $dbc)) { print '<p style="color: red;">Could not select the database because:<br />' . mysql_error($dbc) . '.</p>'; mysql_close($dbc); $dbc = FALSE; } } else { // connnection failure print '<p style="color: red;">Could not connect to MySQL:<br />' . mysql_error() . '.</p>'; } ?> Link to comment Share on other sites More sharing options...
Antonio Conte Posted August 17, 2013 Share Posted August 17, 2013 The error message is clear cut here. The table "myquote" in the DB "myquotes" does simply not exist. Make sure both the DB and the table is correct. 1 Link to comment Share on other sites More sharing options...
phpstuff Posted August 17, 2013 Author Share Posted August 17, 2013 How would I check which databases have been created - can I somehow call a list of all created databases? I'm not sure where that is "stored". Link to comment Share on other sites More sharing options...
Antonio Conte Posted August 18, 2013 Share Posted August 18, 2013 The easiest solution is through PHPMyAdmin. If your on a hosting provider with cPanel, your guaranteed to have that. You might have it non-the-less, but if not, I would just change my provider. It's such a basic tool for DB administration you shouldn't really accept to be without. This holds true even if you are a Python or Ruby developer from my experience. 2 Link to comment Share on other sites More sharing options...
Recommended Posts