Forum Moderators: coopster

Message Too Old, No Replies

Column count doesn't match value count at row 1

Column count doesn't match value count at row 1

         

caveman28

4:17 am on Oct 25, 2003 (gmt 0)

10+ Year Member


Ok, I am writing a php shopping cart and I have run onto an error I cannot fix. I know it is probably something really simple but I have been looking at it for an our and it must be some sort of logic error. I have an "add_product.php" page that allows me to add products to my mysql database. It was working fine until I added the code on lines 23 and 41 to include a textbox to submit a url for a photo of said product. I have a field in my database called products.picture which is set to "text" 30 characters and NULL by default. The error I get is when I fill in the text boxes on "add_product.php" and hit the command button "submit" There error is this:

Column count doesn't match value count at row 1

My code is below, any ideas? Ultimatly I'd like to display the photo of each product on the "edit_product.php" page i wrote. Help!

caveman28

4:18 am on Oct 25, 2003 (gmt 0)

10+ Year Member


Ooops! here's my code...

<?php
# add_product.php

$page_title = 'Add A Product';
require_once ("includes/admin_header.inc"[smilestopper]);
require_once ("includes/config.inc"[smilestopper]);

function make_product_select_row ($which) {
global $db_connection;
echo '<tr><td align="right">Product ' . ucfirst ($which) . ':</td><td align="left"><select name="' . $which . '_id">
';
$tablename = 'product_' . $which . 's';
$columnname = 'product_' . $which . '_id';
$query = "select * from $tablename order by $columnname";
$db_query = mysql_query ($query, $db_connection) or die (mysql_error());
while ($row = mysql_fetch_array ($db_query)) {
echo "<option value=\"$row[0]\">$row[1]</option>\n";
}
echo '</select></td></tr>';
}
// Submit the query if the form has been submitted.
if ($HTTP_POST_VARS['submit']) {
$query = "insert into products values ('0', '$HTTP_POST_VARS[name_id]', '$HTTP_POST_VARS[size_id]', '$HTTP_POST_VARS[format_id]', '$HTTP_POST_VARS[price]', '$HTTP_POST_VARS[sku]', '$HTTP_POST_VARS[picture]', '')";
mysql_query ($query, $db_connection) or die (mysql_error());
}

echo '</table>
<br />
<table>
<tr><td align="left"><u>Add a new product:</u><br><br></td></tr>
<form action="add_product.php" method="post">
';

make_product_select_row ('name');
make_product_select_row ('size');
make_product_select_row ('format');
echo '
<tr><td align="right">Price Per Unit:</td><td align="left"><input type="text" name="price" size="10"></td></tr>
<tr><td align="right">Item ID:</td><td align="left"><input type="text" name="sku" size="10"></td></tr>
<tr><td align="right">Image:</td><td align="left"><input type="text" name="picture" size="20"></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Add Product"></td></tr>
</form>
</table>';

require_once ("includes/admin_footer.inc"[smilestopper]);
?>

bcc1234

4:37 am on Oct 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That means you are trying to insert more values than there are columns in your table.
For example if you have mytable with 3 columns and did something like:
insert into mytable values("1","2","3","4")

that would give you such error.
Since you are trying to create a tuple with 4 columns in a table that only has 3.

caveman28

5:02 am on Oct 25, 2003 (gmt 0)

10+ Year Member


where in my code is the error? My mytable do you mean a database table or an html table? Sorry if I am a newbie but I am not seeing where to change this code... Thaks for any help ya can give, I wanna learn this! :)

caveman28

5:04 am on Oct 25, 2003 (gmt 0)

10+ Year Member



Is there an easier way to use images in my cart with this code? I tried adding it as just another textbox on the add_product.php page and figured once I had a url for an image in the db I could get it to echo in an img src tage somehow...

coopster

10:51 am on Oct 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> where in my code is the error?

It is your INSERT statement you are building in your $query variable; as bcc1234 stated, that means you are trying to insert more values than there are columns in your table.

>> My mytable do you mean a database table or an html table?

Database table.

>> Is there an easier way to use images in my cart with this code? I tried adding it as just another textbox on the
>> add_product.php page and figured once I had a url for an
>> image in the db I could get it to echo in an img src tage somehow...

You could, as long as the image has been uploaded into the directory/folder that was entered into the textbox. It doesn't do any good to tell the server to load an image if the actual image doesn't exist.

ergophobe

3:30 pm on Oct 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Is there an easier way to use images in my cart with this code?

Easier for whom? I mean do you want to make it easier to code or easier to use?

I did a mod that added multiple image functionality to OSCommerce and it was rather complicated, but I think it was easy for the end user. Basically, I used a file upload input box (rather than a text box) and then just used the filename for the entry in the database (with all sorts of checking to make sure the pathname was valid and didn't delete anything of importance).

I also had a lot of other stuff (multiple views of a product, multiple sizes for each view, dynamic renaming if a file of that name already existed in the shop, custom paths and directory structure based on product, brand, category... maybe a little too complicated, but darn flexible), but basically the idea was to avoid having the user type anything in so that a typo would not result in broken images.