-1

When filling in the fields in the browser and presing add product it returns Column count doesn't match value count at row 1 instead of adding it to the database.

<?php
if (isset($_POST['name'])) {

$name = mysql_real_escape_string($_POST['name']);
$price = mysql_real_escape_string($_POST['price']);
$shipping = mysql_real_escape_string($_POST['shipping']);
$quantity = mysql_real_escape_string($_POST['quantity']);
$description = mysql_real_escape_string($_POST['description']);
$keywords = mysql_real_escape_string($_POST['keywords']);
$category = mysql_real_escape_string($_POST['category']);

$sql = mysql_query("SELECT id FROM products WHERE name='$name'");
$productMatch = mysql_num_rows($sql);
if($productMatch > 0){
    echo'sorry name of film is already in database';
    exit();
}
$sql = mysql_query("INSERT INTO products (name,price,shipping,quantity,description,keywords,category)VALUES
('$name','$price','$shipping','$quantity','$description','$keywords','$category',now)") or die(mysql_error());
$id = mysql_insert_id();
$newname = "$id.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'], "./images/$newname");
}
?>
<?php

$product_list = "";
$sql = mysql_query("SELECT * FROM products");
$productCount = mysql_num_rows($sql);
if($productCount > 0){
    while($row = mysql_fetch_array($sql))   {
    $id = $row["id"];
    $name = $row["name"];
    $catagory = $row["catagory"];
    $price = $row["price"];
    $product_list .="";
    }
    }
    else {
    $product_list = "You have no products";
    }

?>
<?php echo $product_list; ?>

 <here is a form that contains all required data    

<?php include 'includes/overall/footer.php' ; ?>    

any ideas cheers this is to be used so we can add products to the database as an admin

2 Answers2

1

Error is very clear in it self Column count doesn't match value count. In your query values contains now at the end while you forget to mention the repective column for it.

Your columns:

name,price,shipping,quantity,description,keywords,category

While values:

'$name','$price','$shipping','$quantity','$description','$keywords','$category',now

NOTE: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
0

Something wrong with this query, I think you missed a field

$sql = mysql_query("INSERT INTO products (name,price,shipping,quantity,description,keywords,category)VALUES
('$name','$price','$shipping','$quantity','$description','$keywords','$category',now)") or die(mysql_error());

There are 7 fields here

name,price,shipping,quantity,description,keywords,category

But here there are 8 values,

'$name','$price','$shipping','$quantity','$description','$keywords','$category',now

So find out the field name for now value and that will fix the problem.

NOTE:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70