-1

在浏览器中填写字段并按下添加产品时,它返回列计数与第 1 行的值计数不匹配,而不是将其添加到数据库中。

<?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' ; ?>    

任何想法都可以使用,因此我们可以以管理员身份将产品添加到数据库中

4

2 回答 2

1

错误在它自己中很明显Column count doesn't match value count。在您的查询值中包含now最后,而您忘记提及它的相应列。

您的专栏:

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

而价值观:

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

注意: 请不要mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?改为了解准备好的语句,并使用PDOMySQLi -本文将帮助您决定使用哪个。

于 2013-03-16T18:16:00.877 回答
0

此查询有问题,我认为您错过了一个字段

$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());

这里有7个字段

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

但是这里有8个值,

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

所以找出now值的字段名称,这将解决问题。

笔记:

请不要mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?改为了解准备好的语句,并使用PDOMySQLi -本文将帮助您决定使用哪个。

于 2013-03-16T18:15:55.810 回答