Jump to content
Larry Ullman's Book Forums

Recommended Posts

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?

Link to post
Share on other sites

 

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);

?>

 

 

You may want to start with subsituing the '-' with '=' on this line...

 

$dbc = mysql_connect('localhost', 'root', 'spring');

  • Upvote 1
Link to post
Share on other sites

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);
?>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...