2013-03-16 23 views
-1

當填充瀏覽器中的字段並提示添加產品時,它返回 列計數與第1行 中的值計數不匹配,而是將其添加到數據庫中。PHP,添加到數據庫中

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

任何想法歡呼 這是要使用的,所以我們可以向數據庫中添加產品作爲管理員

+0

他們給了你正確的答案。順便說一下,你應該考慮使用PDO或Mysqli。 – Frildoren 2013-03-16 18:18:04

回答

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,並使用PDOMySQLi - this article將幫助您決定哪個。

+0

歡呼聲,錯過了一個到底 – user2177625 2013-03-16 18:24:25

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,並使用PDOMySQLi - this article將幫助您決定哪個。