我有一个产品页面,其中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
好的,谢谢你指出。但是这并不能回答我的问题......而且我的数据库中的表都已满。 –