seephp Posted February 14, 2007 Share Posted February 14, 2007 I am writing A simple variatino of a blogging engine and it I keep getting the error: Could not select the database because: Column count doesn't match value count at row 1The query was INSERT INTO blog_entries (blogid, title, entry, date_entered) VALUES (0, 'I\'m trying this again!' 'This time I fixed a simple HTML problem!', NOW()). The code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> <title>Creat A sticky note</title> </head> <body> <?php ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); if (isset ($_POST['submit'])) { if ($dbc = mysql_connect ('localhost', 'root', 'vertrigo')) { if (!@mysql_select_db ('mysticky')) { die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); } } else { die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); } $query = "INSERT INTO blog_entries (blog_id, title, entry, date_entered) VALUES (0, '{$_POST['title']}' '{$_POST['entry']}', NOW())"; if (mysql_query ($query)) { print '<p>Your StickyWeb Sticky has been stuck.</p>'; } else { print "<p>Could not select the database because: <b>" . mysql_error() . "</b>The query was $query.</p>"; } mysql_close(); } ?> <form action="addNote.php" method="post"> <p>Create your Websticky Sticky note!</p> <p>Enter your title:<input type="text" name="title" size="40" maxsize="100" /></p> <p>Enter your note:<textarea name="entry" columns="40" rows="5"></textarea></p> <input type="submit" name="submit" value="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ataria Posted February 14, 2007 Share Posted February 14, 2007 INSERT INTO blog_entries (blog_id, title, entry, date_entered) SHOULD BE. INSERT INTO `blog_entries` (`blog_id`, `title`, `entry`, `date_entered`) Quote Link to comment Share on other sites More sharing options...
Jenk Posted February 14, 2007 Share Posted February 14, 2007 You are missing out on a comma between two of your values. Quote Link to comment Share on other sites More sharing options...
seephp Posted February 14, 2007 Author Share Posted February 14, 2007 Now I am getting this error: Could not select the database because: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'Trying this AGAIN!' ' at line 2The query was INSERT INTO blog_entries ('blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'Trying this AGAIN!' 'I hope this works!', NOW()). I had changed the code to: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> <title>Creat A sticky note</title> </head> <body> <?php ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); if (isset ($_POST['submit'])) { if ($dbc = mysql_connect ('localhost', 'root', 'vertrigo')) { if (!@mysql_select_db ('mysticky')) { die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); } } else { die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); } $query = "INSERT INTO blog_entries ('blog_id', 'title', 'entry', 'date_entered') VALUES (0, '{$_POST['title']}' '{$_POST['entry']}', NOW())"; if (mysql_query ($query)) { print '<p>Your StickyWeb Sticky has been stuck.</p>'; } else { print "<p>Could not select the database because: <b>" . mysql_error() . "</b>The query was $query.</p>"; } mysql_close(); } ?> <form action="addNote.php" method="post"> <p>Create your Websticky Sticky note!</p> <p>Enter your title:<input type="text" name="title" size="40" maxsize="100" /></p> <p>Enter your note:<textarea name="entry" columns="40" rows="5"></textarea></p> <input type="submit" name="submit" value="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 14, 2007 Share Posted February 14, 2007 As said previously, you forgot a comma. You have: "INSERT INTO blog_entries (blog_id, title, entry, date_entered) VALUES (0, '{$_POST['title']}' '{$_POST['entry']}', NOW())"; Change to : "INSERT INTO blog_entries (blog_id, title, entry, date_entered) VALUES (0, '{$_POST['title']}',<--------note the added comma! '{$_POST['entry']}', NOW())"; Quote Link to comment Share on other sites More sharing options...
seephp Posted February 14, 2007 Author Share Posted February 14, 2007 Thanks, I wasn't sure where that comma was supposed to go ;D. Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 14, 2007 Share Posted February 14, 2007 Always b4 or after the single quote,', Quote Link to comment Share on other sites More sharing options...
seephp Posted February 14, 2007 Author Share Posted February 14, 2007 I added the comma but I'm still getting: Could not select the database because: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'I reall hope this wor' at line 2The query was INSERT INTO blog_entries ('blog_id', 'title', 'entry', 'date_entered') VALUES (0, 'I reall hope this works this time!', 'If this works I can begin working on my project for Master Alley!', NOW()). Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 14, 2007 Share Posted February 14, 2007 U don't need the single quotes around you row identifiers.Try changing: ('blog_id', 'title', 'entry', 'date_entered') to: (blog_id, title, entry, date_entered) Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted February 14, 2007 Share Posted February 14, 2007 $query = "INSERT INTO `blog_entries` (blog_id, title, entry, date_entered) VALUES (0, '{$_POST['title']}', '{$_POST['entry']}', NOW())"; Quote Link to comment Share on other sites More sharing options...
seephp Posted February 14, 2007 Author Share Posted February 14, 2007 YES! It finally works. Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 14, 2007 Share Posted February 14, 2007 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> <title>Creat A sticky note</title> </head> <body> <?php //php.ini settings. ini_set ('display_errors', 1); //php.ini error settings. error_reporting (E_ALL & ~E_NOTICE); // if the form of title and entry are blank if(($title=="none")||($entry=="none")){ // tell user to fill in all the form echo "please fill in all the form cheers"; } //database $db=mysql_connecy("localhost","username","password"); mysql_select_db("databse",$db) or die (mysql_error()); //protect database $blog_id=addslases($_POST['blog_id']); $title=addslashes($_POST['title']); $entry=addslashes($_POST['entry']); $date_entered=addslashes($_POST['date_entered']); //date for date entred. $date_entered=date("d-m-y"); //if the user post an entry. if (isset($_POST['submit'])) { $query = "INSERT INTO blog_entries(blog_id,title,entry,date_entered) VALUES('$blog_id', '$title', '$entry', '$date_entered')"; $result=mysql_query($query) or die(mysql_error()); // 1st message if correct. echo '<p>Your StickyWeb Sticky has been stuck.</p>'; }else{ // 2nd message not correct. echo "<p>Could not select the database because:"; } ?> <form action="addNote.php" method="post"> <p>Create your Websticky Sticky note!</p> <p>Enter your title:<input type="text" name="title" size="40" maxsize="100" /></p> <p>Enter your note:<textarea name="entry" columns="40" rows="5"></textarea></p> <input type="submit" name="submit" value="submit" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.