2013-02-27 68 views
0

這個問題和它的標題可能會作爲一個模糊的問題來看,我很抱歉,如果它確實如此,但我沒有其他方式來問這個問題,因爲我自己很困惑。如何停止產品編號10以在PHP中下推產品編號9?

還好這裏有雲:

我創建了一個網站,該網站所有者可以發佈產品,以及它們在主頁上的圖像!這一點工作正常。

該主頁將只顯示最多10個產品。這一點也適用。

產品按數字存儲在數據庫中。例如:第一個項目/產品保存爲產品編號1,1是其ID,產品編號2是2,3是3,依此類推。 這一點也工作正常,因爲我想它的工作。

這裏的問題:

可以說我已經加入10個產品,這是最大可顯示在主頁德的主頁。產品1至10

當我添加一個新的產品(產品編號11),這個新增加的產品將清除最久產品附加值是產品編號爲1

我不希望這種事情發生。我需要將產品/項目留在隊列中,並像其他產品一樣存儲在數據庫中直到我手動刪除/刪除舊的項目,它將自動替換。

這是我使用的添加產品到數據庫的代碼:

<?php 
// Parse the form data and add inventory item to the system 
if (isset($_POST['product_name'])) { 

    $product_name = mysql_real_escape_string($_POST['product_name']); 
    $link = mysql_real_escape_string($_POST['link']); 
    $retail = mysql_real_escape_string($_POST['retail']); 
    $clicks = mysql_real_escape_string($_POST['clicks']); 
    $price = mysql_real_escape_string($_POST['price']); 
    $category = mysql_real_escape_string($_POST['category']); 
    $subcategory = mysql_real_escape_string($_POST['subcategory']); 
    $details = mysql_real_escape_string($_POST['details']); 
    // See if that product name is an identical match to another product in the system 
    $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1"); 
    $productMatch = mysql_num_rows($sql); // count the output amount 
    if ($productMatch > 0) { 
     echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="index.php">click here</a>'; 
     exit(); 
    } 
    // Add this product into the database now 
    $sql = mysql_query("INSERT INTO products (product_name, link, retail, clicks, price, details, category, subcategory, date_added) 
     VALUES('$product_name','$link','$retail','$clicks','$price','$details','$category','$subcategory',now())") or die (mysql_error()); 
    $pid = mysql_insert_id(); 
    // Place image in the folder 
    $newname = "$pid.jpg"; 
    move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/$newname"); 
    header("location: index.php"); 
    exit(); 
} 
?> 

任何幫助將是greately很有幫助。即使它把我放在正確的方向。

謝謝

+3

所以只需選擇最近的10個產品...'select ... order by ID DESC limit 10' – 2013-02-27 17:23:41

+0

發佈您的數據庫表模式可能會有所幫助 – Czechnology 2013-02-27 17:25:35

+0

您想更改產品的主頁代碼如何顯示在? – Hast 2013-02-27 17:33:14

回答

1

DB模式對我們來說是不必要的。這是一個訂購問題。您可能會發現,在您的SQL查詢獲取主頁的項目,如「ORDER BY ID DESC」或日期。您需要通過id ASC訂購商品(這是SQL的默認行爲,因此只需簡單地使用「ORDER BY id」就足夠了。

重複 - 您的SQL特別訂購記錄DESC的日期或ID。

+0

aww man ..你真棒。它做到了。簡單但很棒。非常感謝你.. – user2056633 2013-02-27 18:24:43

+0

不用擔心隊友。我強烈建議你前往w3schools並學習SQL--對於這些ANSI標準命令,大多數數據庫引擎是相同的。 – Trent 2013-02-27 18:30:57