View Full Version : column count doesn't match value count

05-24-2011, 05:35 PM
i would be grateful if you can spare some few minutes of you time to look in to this code of mine. the form below is designed
to send a users reply to a topic to a datadase table and the code to process the reply (form action) is the one below.
The problem i have with this code is that when the sub,it button is clicked, i recieve error message saying "column count doesn't match value count"
i think it is the sql query but i can't figure out i i have gone wrong.

<form action="add_ans.php" method="POST" name="reply" id="reply">
<table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
<td width="5" valign="top" bgcolor="#e8eefa"><input name="sub_id" type="hidden" id="sub_id" value="<?php echo $row_posts['sub_cat_id']; ?>" />
<input name="posts_id" type="hidden" id="posts_id" value="<?php echo $row_posts['post_id']; ?>" />
<input name="rep_by" type="hidden" id="rep_by" value="<?php echo $row_name['urban']; ?>" /></td>
<td width="79%" bgcolor="#e8eefa"><textarea name="details" cols="45" rows="3" id="details">Reply to this topic</textarea></td>
<td bgcolor="#e8eefa"><input name="rep_id" type="hidden" id="rep_id" value="<?php echo $row_name['id']; ?>" /></td>
<td bgcolor="#e8eefa"><input name="rep_mail" type="hidden" id="rep_mail" value="<?php echo $row_posts['post_by_mail']; ?>" />
<input type="submit" name="Submit" value="Post reply" /></td>


(code for the processing of form data, meant send data to tbl_name and update a column in tbl_name2 by name replies)

$host = 'localhost';
$username = 'root';
$password = '';
$db_name = 'urbansystems';
$tbl_name = 'forum_replies';
$tbl_name2 = 'forum_posts';

mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

// Escape these to protect from SQL injection, for example intval() for integer values and mysql_real_escape_string() for strings, though you can have more elaborate protection
$sub_id = $_POST['sub_id'];
$posts_id = $_POST['posts_id'];
$rep_by = $_POST['rep_by'];
$details = $_POST['details'];
$rep_id = $_POST['rep_id'];
$rep_mail = $_POST['rep_mail'];

// There are SQL function for getting the date
$datetime = date("d/m/y H:i:s");

$sql2 = "SELECT MAX(reply_id) AS Maxa_id FROM $tbl_name WHERE forum_post_id=$posts_id";
$result2 = mysql_query($sql2) or die(mysql_error());
$rows2 = mysql_fetch_array($result2) or die(mysql_error());

$Max_id = 1;
if(mysql_num_rows($result2) > 0) {
$Max_id += $rows2['Maxa_id'];
// You don't need apostrophes around integer values in SQL queries
$sql = "INSERT INTO $tbl_name (sub_cat_id, forum_post_id, reply_by, reply_details, rep_by_id, rep_by_mail, datetime) VALUES ('$sub_id', '$Max_id', '$posts_id','$rep_by','$details', '$rep_id', '$rep_mail', '$datetime')";
$result = mysql_query($sql) or die(mysql_error());

if($result) {
echo '<h2>Successful posting</h2>';
echo "<a href='replies.php?posts_id=$post_id'>Click here to go back</a>";

$sql3 = "UPDATE $tbl_name2 SET replies='$Max_id' WHERE post_id='$posts_id'";
mysql_query($sql3) or die(mysql_error());

05-25-2011, 01:03 AM
$sql = "INSERT INTO $tbl_name (sub_cat_id, forum_post_id, reply_by, reply_details, rep_by_id, rep_by_mail, datetime) VALUES ('$sub_id', '$Max_id', '$posts_id','$rep_by','$details', '$rep_id', '$rep_mail', '$datetime')";
here is 7 fields and you have 8 values