Jump to content

inserting session value into DB


unsider

Recommended Posts

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

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.

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

	}
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.