Page 1 of 1

help with discussion forum user input form and php scripts

Posted: Wed Jul 05, 2006 7:48 pm
by black85
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi people,
I'm trying to create a discussion forum which involves developing database tables, user input html forms and display of the results, but unfortunately i have been getting this error message - 'column count doesn't match value count at row 1'.

The following below are the Mysql database tables, user input htm form and php scripts used:

[syntax="sql"]create table forum_topics
(
topic_id int not null primary key auto_increment,
topic_title varchar(150),
topic_create_time datetime,
topic_owner varchar(150)
);


create table forum_posts
(
post_id int not null primary key auto_increment,
topic_id int not null,
post_text text,
post_create_time datetime,
post_owner varchar(150)
);
addtopic.html

Code: Select all

<html>
<head>
<title>Add a Topic</title>
</head>
<body>
<h1>Add a Topic</h1>
<form method=post action="do_addtopic.php">
<p><strong>Your E-Mail Address/strong><br>
<input type="text" name="topic_owner" size=40 maxlength=150>
<p><strong>Topic Title/strong><br>
<input type="text" name="topic_title" size=40 maxlength=150>
<P><strong>Post Text/strong><br>
<textarea name="post_text" rows=8 cols=40 wrap=virtual></textarea>
<P><input type="submit" name="submit" value="Add Topic"></p>
</form>
</body>
</html>
do_addtopic.php

Code: Select all

<?php
//check for required fields from the form
if ((!$_POST[topic_owner]) || (!$_POST[topic_title])|| (!$_POST[post_text])) {
header("Location: addtopic.html");
exit;
}

//connect to server and select database
$conn = mysql_connect("localhost", "root", "olu1bal") or die(mysql_error());
mysql_select_db("testDB",$conn) or die(mysql_error());

//create and issue the first query
$add_topic = "insert into forum_topics (topic_title) values ('$_POST[topic_title]', now())";
$add_topic = "insert into forum_topics (topic_owner) values ('$_POST[topic_owner]', now())";


mysql_query($add_topic,$conn) or die(mysql_error());

//get the id of the last query
$topic_id = mysql_insert_id();

//create and issue the second query
$add_post = "insert into forum_posts values ('', '$topic_id','$_POST[post_text]', now(), '$_POST[topic_owner]')";
mysql_query($add_post,$conn) or die(mysql_error());

//create nice message for user
$msg = "<P>The <strong>$topic_title</strong> topic has been created.</p>";
?>
<html>
<head>
<title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php print $msg; ?>
</body>
</html>
error message displayed[/syntax]

Code: Select all

'column count doesn't match value count at row 1'.

Please tell me where i'm going wrong a.s.a.p

black85


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jul 05, 2006 8:00 pm
by feyd
Your $add_topic query tells MySQL you want to insert one field but you are supplying two fields.

Posted: Thu Jul 06, 2006 1:00 pm
by black85
Thanks Feyd for replying to my thread. I am a php, mysql and apache beginner meaning i am working through exercises in this text book titled: sams teach yourself php, mysql and apache and not sure where the error lies in the php code. What do you suggest.

black

Posted: Thu Jul 06, 2006 1:12 pm
by Sphenn
Hi,

This is your query

Code: Select all

$add_topic = "insert into forum_topics (topic_title) values ('$_POST[topic_title]', now())";
You want to insert data into one field (topic_title), but you are supplying two pieces of information: $_POST['topic_title'], and the function now(). You can't supply both. I suspect you just want the first one.

Hope this helps

Sphenn

Posted: Thu Jul 06, 2006 1:59 pm
by black85
thanks sphenn.