2015-01-10 28 views
0

我正在研究一個小訂單購物車,目前我已經實現了MySQL來顯示數據庫中的產品。我希望這些產品擁有自己的圖像,並將其組織成3個獨立的表格,供初學者,主菜和甜點使用。操縱MySQL表中的單個產品

目前我有一個表的所有項目,我不確定如何添加圖像旁邊的選定產品。

如下所示:

enter image description here

這裏是我的代碼:

<?php 

session_start(); 

$page = 'index.php'; 

mysql_connect('localhost','root','') or die(mysql_error()); 
mysql_select_db('cart') or die(mysql_error()); 

if (isset($_GET['add'])) { 
    $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add'])); 
    while ($quantity_row = mysql_fetch_assoc($quantity)) { 
     if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) { 
      $_SESSION['cart_' . (int)$_GET['add']] +='1'; 
      header('Location: ' . $page); 

    } 
} 
header('Location: ' . $page); 

} 

if (isset($_GET['remove'])) { 
    $_SESSION['cart_'.(int)$_GET['remove']]--; 
    header ('Location: ' . $page); 

} 

if (isset($_GET['delete'])) { 
    $_SESSION['cart_' . (int)$_GET['delete']]='0'; 
    header ('Location: ' . $page); 
} 



function products() { 
    $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id ASC'); 
    if (mysql_num_rows($get) == 0) { 
     echo "There are no products to display."; 
    } 
    else { 
    echo "<center>\n"; 
    echo " <table border=2 width=40% cellspacing=5 cellpadding=10 bgcolor=cyan cols=2>\n"; 
    echo "  <tr>\n"; 
    echo "  <th>View</th>\n"; 
    echo "  <th>Dish</th>\n"; 
    echo "  <th>Description</th>\n"; 
    echo "  <th>Item Price</th>\n"; 
    echo "  </tr>\n"; 
    while ($get_row = mysql_fetch_assoc($get)) { 

    ?> 
    <tr> 
     <td> <a href="ice.png"</a> </td> 
     <td> <?echo '<p>'.$get_row['name']?> </td> 
     <td> <?echo $get_row['description']?> </td> 
     <td> <?echo '&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'">Add</a></p>';?> </td> 
    </tr> 
    <? 
    } 
    echo "</table>\n"; 
    echo "</center>\n"; 
} 
} 
+0

將路徑存儲到數據庫中映像的Web路徑或磁盤路徑。然後用'' – Halcyon

+0

@Halcyon顯示圖片感謝您的回覆,您能否告訴我一個您的意思的小例子?我是編程新手 – Oscar

+0

@JamesPatterson你能展示打印藍色表格的代碼嗎? – Halcyon

回答

0

您可以將字段添加到表一中的產品類型。 因此,您將能夠使用該條件執行3個單獨的查詢。 例如"SELECT id, name, description, price FROM products WHERE quantity > 0 AND type LIKE 'desert' ORDER BY id ASC"