我有一個產品頁面,其中8個產品圖像位於正在由存儲在MySQL數據庫中的圖像填充的列表中。這些圖像都具有關聯的ID,其中價格,產品名稱和說明也與相同的ID相關聯。從URL獲取MySQL ID以顯示頁面上的信息
我想要做的背後的想法是;當用戶點擊8個產品圖像中的一個時,它們將被重定向到「結帳」頁面,該頁面將顯示相同的圖像,以及也存儲在數據庫中該ID下的所有信息。
截至目前,我的結帳頁面URL包含圖像的ID(url.com/checkout.php?id=1),我希望能找到一種方法來獲取所有存儲在該信息下的信息要在頁面上調用的URL中顯示ID。
這裏是我的PHP代碼,在產品頁面上的列表顯示圖像:
// Grab the data from our template table
$sql = "select * from templates";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<li>";
echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">";
// Note that we are building our src string using the ID from the database
echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />";
echo "<span>";
echo "<big>" . $row['name'] . "</big>";
echo $row['description'];
echo "</span>";
echo "</a>";
echo "</li>";
}
這是應該收集點擊的產品信息的PHP代碼(但沒有) :
if (isset($_GET['id']))
$id=$_GET['id'];
else
$id=1;
if (isset($_GET['action']))
$action=$_GET['action'];
else
$action="empty";
switch($action){
case "add":
if($_SESSION['cart'][$id])
$_SESSION['cart'][$id]++;
else
$_SESSION['cart'][$id]=1;
break;
case "remove":
if($_SESSION['cart'][$id])
{
$_SESSION['cart'][$id]--;
if($_SESSION['cart'][$id]==0)
unset($_SESSION['cart'][$id]);
}
break;
case "empty":
unset($_SESSION['cart']);
break;
}
//Display Cart
if(isset($_SESSION['cart'])) {
$total=0;
foreach($_SESSION['cart'] as $id => $x) {
$result=mysql_query("select image,name,price,description from templates WHERE id=$id");
$myrow=mysql_fetch_array($result);
$image=$myrow['image'];
$name=$myrow['name'];
$price=$myrow['price'];
$description=$myrow['description'];
}
}
這裏是實際的HTML/PHP,其中的信息是應該顯示:
<a class="caption" href="checkout.php">
<img src="http://URL-REMOVED.com/file_display.php?id=<?php $myrow['id'] ?>"/>
<span>
<big>
<strong><?php $myrow['name'] ?></strong>
</big>
<div class="price"><?php $myrow['price'] ?></div>
</span>
</a>
</div>
<div id="info_form_container">
<div class="product_info">
<div class="control-group">
<strong>Template Name:</strong>
<?php $myrow['name'] ?>
</div>
<div class="control-group">
<strong>Template Description:</strong>
<?php $myrow['description'] ?>
</div>
<div class="control-group">
<strong>Template Price: </strong>
<?php $myrow['price'] ?>
</div>
</div>
我想我不確定這是否是最好的方法?但我絕對想要將圖像存儲在數據庫中,並且我一定想用ID號給他們打電話...
我該如何做到這一點?我的代碼在哪裏錯了?
檢查表,表中可能沒有任何東西 –
新的應用程序**不應該使用**'mysql_query',而應該使用新的'mysqli'或PDO接口之一。 [SQL注入漏洞](http://bobby-tables.com/)會導致非常嚴重的問題。在你創造一個巨大的混亂之前,你最好切換到其中的一個,你必須清理。 – tadman
好的,謝謝你指出。但是這並不能回答我的問題......而且我的數據庫中的表都已滿。 –