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> Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/ 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`) Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184592 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. Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184615 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> Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184858 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())"; Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184862 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. Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184864 Share on other sites More sharing options...
Greaser9780 Posted February 14, 2007 Share Posted February 14, 2007 Always b4 or after the single quote,', Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184866 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()). Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184869 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) Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184871 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())"; Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184876 Share on other sites More sharing options...
seephp Posted February 14, 2007 Author Share Posted February 14, 2007 YES! It finally works. Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184913 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> Link to comment https://forums.phpfreaks.com/topic/38471-inserting-data-into-mysql-help/#findComment-184916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.