gpt4 book ai didi

php - 从 URL 获取 MySQL ID 以在页面上显示信息

转载 作者:行者123 更新时间:2023-11-30 23:28:09 24 4
gpt4 key购买 nike

我有一个产品页面,其中 8 个产品图像位于一个列表中,该列表由存储在 MySQL 数据库中的图像填充。这些图像都有关联的 ID,其中价格、产品名称和描述也与相同的 ID 关联。

我想做的事情背后的想法是;当用户单击 8 个产品图像中的一个时,他们将被重定向到“结帐”页面,该页面将显示相同的图像,以及数据库中存储在该 ID 下的所有信息。

截至目前,我有包含图像 ID 的结帐页面 URL (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 调用它们...

我怎样才能做到这一点?我的代码哪里错了?

最佳答案

编辑:我不认为我第一次理解你的问题。尝试使用它来生成您的产品列表,更新连接信息。如果可行,请使用以下方法清理您的变量并将您的连接信息存储在别处

<?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..-->

关于php - 从 URL 获取 MySQL ID 以在页面上显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com