unsider Posted March 11, 2008 Share Posted March 11, 2008 This query is frustrating me, mainly because I can't find a well written article explaining how it all works. I am trying to insert a session value into a DB, and retrieve it in a later script. I've tested everything in the script up till the query, therefore I know thats the error. Heres the output: session->username set. Able to locate database. post array was set. Error adding query: Column count doesn't match value count at row 1 Warning: Cannot modify header information - headers already sent by (output started at /home/unsider/www/andy/comment_process.php:13) in /home/unsider/www/andy/comment_process.php on line 37 Code: <?php include("include/session.php"); if(!$session->username) { header('main.php'); } else { if($session->username) { echo "session->username set." . "<br>"; } if (!@mysql_select_db('mtlc?old')) { echo 'Unable to locate the ' . 'database.'; } if (mysql_select_db('mtlc?old')) { echo 'Able to locate database.' . "<br> "; } if(isset($_POST['commenttext'])) { echo "post array was set" . "<br>"; // HELP ################################################################################ $commenttext = $_POST['commenttext']; $username = $session->$username; $sql="INSERT INTO `comments` VALUES ( '$username','$commenttext', 'NOW()' )"; // #################################################################################### if (!@mysql_query($sql)) { echo 'Error adding query: ' . mysql_error(); header('main.php'); } } } ?> Please help Link to comment https://forums.phpfreaks.com/topic/95573-inserting-session-value-into-db/ Share on other sites More sharing options...
darkfreaks Posted March 11, 2008 Share Posted March 11, 2008 is there a date column in your db and are the variables in order of your database ??? Link to comment https://forums.phpfreaks.com/topic/95573-inserting-session-value-into-db/#findComment-489256 Share on other sites More sharing options...
unsider Posted March 11, 2008 Author Share Posted March 11, 2008 there are four columns in my table* (will be adding more later, but for now I'm trying to establish something) id (int) , commenttext (text) , commentdate (datetime), username (varchar255) respectively, so i guess they are not in order. Link to comment https://forums.phpfreaks.com/topic/95573-inserting-session-value-into-db/#findComment-489257 Share on other sites More sharing options...
darkfreaks Posted March 11, 2008 Share Posted March 11, 2008 <?php ob_start(); include("include/session.php"); if(!$session->username) { header('main.php'); } else { if($session->username) { echo "session->username set." . "<br>"; } if (!@mysql_select_db('mtlc?old')) { echo 'Unable to locate the ' . 'database.'; } if (mysql_select_db('mtlc?old')) { echo 'Able to locate database.' . "<br> "; } if(isset($_POST['commenttext'])) { echo "post array was set" . "<br>"; // HELP ################################################################################ $commenttext = $_POST['commenttext']; $username = $session->$username; $sql="INSERT INTO `comments`('commenttext','commentdate','username')VALUES ( $commenttext', 'NOW()','$username' )"; // #################################################################################### if (!@mysql_query($sql)) { echo 'Error adding query: ' . mysql_error(); header('main.php'); } } } ?> Link to comment https://forums.phpfreaks.com/topic/95573-inserting-session-value-into-db/#findComment-489258 Share on other sites More sharing options...
unsider Posted March 11, 2008 Author Share Posted March 11, 2008 Works. Thank you. haha Link to comment https://forums.phpfreaks.com/topic/95573-inserting-session-value-into-db/#findComment-489261 Share on other sites More sharing options...
darkfreaks Posted March 11, 2008 Share Posted March 11, 2008 make sure you put your fields in order next time and match them up like i did in the query Link to comment https://forums.phpfreaks.com/topic/95573-inserting-session-value-into-db/#findComment-489262 Share on other sites More sharing options...
haku Posted March 11, 2008 Share Posted March 11, 2008 Also, moving your headers up in your script to a point before there are any echo statements to the browser is preferable over using ob_start(). ob_start() masks problems, it doesn't solve them. Link to comment https://forums.phpfreaks.com/topic/95573-inserting-session-value-into-db/#findComment-489271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.