我有一个产品页面,其中8个产品图像位于列表中,该列表由存储在MySQL数据库中的图像填充。图像都具有关联的ID,其中价格,产品名称和描述也与相同的ID相关联。
我想要做的事情背后的想法是;当用户点击8个产品图像中的一个时,它们将被重定向到“结账”页面,该页面将显示相同的图像,以及在该数据库中也存储在该ID下的所有信息。
截至目前,我有结帐页面网址,包括图片ID(url.com/checkout.php?id=1),我希望找到一种方法来获取存储在该ID下的所有信息在要在页面上调用的URL中显示。
这是我的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 ...
来调用它们我怎样才能做到这一点?我的代码在哪里错了?
答案 0 :(得分:0)
<?php
$mysqli_connection = new mysqli("localhost", "username", "password", "database");
$sql = "SELECT * FROM templates";
$result = $mysqli_connection->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
echo '<li>';
echo '<a href="purchase.php?id='.$id.'" class="caption">';
echo '<img src="http://URL-REMOVED.com/file_display.php?id='.$id.'" />';
echo '<span>';
echo '<big>'.$name.'</big>';
echo $description;
echo '</span>';
echo '</a>';
echo '</li>';
}
?>
OG答案:
我认为你在做什么就好......这是我使用的方法(尽管我看起来有点整洁)。运行PHP函数,使用您获得的ID查询MySQL数据库,然后开始将您获取的信息转储到您的站点中。为了帮助提高可读性并减少拼写混淆,将$myrow['whatever']
结果分配到变量中可能会有所帮助,但这对我来说比任何事情都更为美观。
要修复MySQL内容并使用mysqli,请尝试以下操作:
$sql = "SELECT * FROM templates";
$result = $mysqli_connection->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
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>";
}
尝试使用以下方法清理您的信息:
$my_stuff = mysqli_connection->real_escape_string($row['that_stuff']);
另外,如果你愿意,你知道你可以在echo语句周围使用单引号(''),对吗?可以更容易地逃避所有的双引号..
总而言之,这可能是我将要做的一个粗略的例子,但我会将其分解为函数,并可能创建一些全局变量(例如连接本身):
<?php
$mysqli_connection = new mysqli("localhost", "username", "password", "database");
$sql = "SELECT * FROM templates WHERE id=$id";
$result = $mysqli_connection->query($sql);
// I'm assuming there is only one entry, so no while loop for me
$row = $result->fetch_array(MYSQLI_ASSOC);
$your_title = $mysqli_connection->real_escape_string($row['title']);
$path_to_image = $mysqli_connection->real_escape_string($row['image']);
$description = $mysqli_connection->real_escape_string($row['description']);
$price = $mysqli_connection->real_escape_string($row['price']);
?>
<html>
<head></head>
<body>
<h3><?php echo $your_title; ?></h3>
<img src="<?php echo $path_to_image; ?>" />
<ul>
<li>Description: <?php echo $description; ?></li>
<li>Price: <?php echo $price; ?></li>
<!-- etc..-->