PHP,添加到数据库

时间:2013-03-16 18:08:22

标签: php

当填写浏览器中的字段并预设添加产品时,它将返回 列数与第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' ; ?>    
任何想法欢呼 这将被使用,因此我们可以将产品作为管理员添加到数据库

2 个答案:

答案 0 :(得分:1)

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

你的专栏:

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

虽然价值观:

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

注意: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?请改为了解prepared statements,并使用PDO,或MySQLi - this article将帮助您确定哪个。

答案 1 :(得分: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值的字段名称,这将解决问题。

注意:

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?请改为了解prepared statements,并使用PDO,或MySQLi - this article将帮助您确定哪个。