Forum Moderators: coopster

Message Too Old, No Replies

Column count doesn't match value count at row 1

I'm not sure why I'm getting this error

         

beniro

8:04 pm on May 18, 2006 (gmt 0)

10+ Year Member



I'm a PHP newbie and we've started using some of it here at work, sooo...

I'm getting "Column count doesn't match value count at row 1", but as far as I can tell, I have the same number of columns in the table as in the dataset I'm trying to insert...

Here's the code I've inherited. :)

---code---
session_start();

$ssid = session_id();

$conn = mysql_connect("mysql.site.com", "connection", "string");

mysql_select_db("dbtable", $conn);

$sql = "create table item_table(id int not null primary key auto_increment, product varchar(25), qty int, price float, total float, SID varchar(40), buy_time varchar(35))";

$result = mysql_query($sql,$conn);

if($_POST[product]=="none"){
$total=0;
$price=0;
}
else
$price=600;
$total = $price * $_POST[qty];
$time = time();

$my_time = date("r", $time);
echo "prod=".$_POST[product]."<br>";
echo "qty=".$_POST[qty]."<br>";
echo "price=".$price."<br>";
echo "total=".$total."<br>";
echo "ssid=".$ssid."<br>";
echo "time=".$my_time."<br>";
$sql = "insert into item_table values('', '$_POST[product]', '$_POST[qty]', '$price', '$total', '$ssid', '$my_time')";
echo $sql."<br>";

/// there's an error in the following line, possibly too few/too many columns
$result = mysql_query($sql, $conn) or die(mysql_error());
---/code---

Any insight in this matter would be greatly appreciated.

jatar_k

8:07 pm on May 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try pulling the arrays out of the double quotes, it doesn't work

$sql = "insert into item_table values('', '" . $_POST[product] . "', '" . $_POST[qty] . "', '$price', '$total', '$ssid', '$my_time')";

that might work

<added>I see you echo the query, what does it look like?

beniro

8:21 pm on May 18, 2006 (gmt 0)

10+ Year Member



Wow, thanks for the fast reply... That still didn't work however, but I've updated the code to look like this:

---code---

session_start();

$ssid = session_id();

$conn = mysql_connect("mysql.site.com", "connection", "string");

mysql_select_db("dbtable", $conn);

$sql = "create table item_table(id int not null primary key auto_increment, product varchar(25), qty int, price float, total float, SID varchar(40), buy_time varchar(35))";

$result = mysql_query($sql,$conn);

if($_POST[product]=="none"){
$total=0;
$price=0;
}
else
$price=600;
$total = $price * $_POST[qty];
$time = time();

$my_time = date("r", $time);

echo "prod=".$_POST[product]."<br>";
echo "qty=".$_POST[qty]."<br>";
echo "price=".$price."<br>";
echo "total=".$total."<br>";
echo "ssid=".$ssid."<br>";
echo "time=".$my_time."<br>";

$sql = "insert into item_table values('', '" . $_POST[product] . "', '" . $_POST[qty] . "', '$price', '$total', '$ssid', '$my_time')";

echo "sql=".$sql."<br>";

/// there's an error in the following line, possibly too few/too many columns
$result = mysql_query($sql, $conn) or die(mysql_error());

---/code---

The 'echos' at the end output this:

prod=ML
qty=2
price=600
total=1200
ssid=extremelylongalphanumer
time=Thu, 18 May 2006 13:16:42 -0700
insert into item_table values('', 'ML', '2', '600', '1200', 'extremelylongalphanumer', 'Thu, 18 May 2006 13:16:42 -0700')

and then...

Column count doesn't match value count at row 1

jatar_k

8:24 pm on May 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that insert has 7 columns

how many in your table?

[dev.mysql.com...]

If you do not specify a list of column names for INSERT ... VALUES or INSERT ... SELECT, values for every column in the table must be provided by the VALUES list or the SELECT statement. If you do not know the order of the columns in the table, use DESCRIBE tbl_name to find out.

so another option would be to use the column names like so

INSERT INTO tbl_name (col1,col2) VALUES('value1','value2');

beniro

8:53 pm on May 18, 2006 (gmt 0)

10+ Year Member



Ok...I'll try that and report back. Thanks very much for your help! Good day!