我的工作 PHP 脚本连接到我的数据库获取数据,然后将其放入表中,如下所示:
while($row = mysqli_fetch_array($result)) //mysql_fetch_array bring back an object, in this case the $result of the mysql query and puts it into a variable $row
{
echo "<tr>";
echo "<td align='center'><img height='100' width='100' src=\"" . $row['picturepath'] . "\" /></td>"; //here I can use the results and call out which column I want from my database
echo '<td align="center">' . $row['name'] . '</td>';
echo '<td align="center"><input type="button" value="More Info" onclick="window.location=\'more_info.php?start=' . urlencode($row['description']) .' \';" /></td>';
echo "<td align='center'>" . $row['price'] . "</td> <td align='center'> <input type='button' value='Add to Order' onclick=''> </td>";
echo "</tr>";
}
echo "</table>";
echo "<table width=\"1024\" align=\"center\" >";
echo "<tr height=\"50\"></tr>";
“更多信息”按钮链接到一个单独的 php 脚本 more_info.php,其中的描述被读取到该特定项目的页面上。我还想显示与该项目相对应的图像,但在这样做时遇到了麻烦。
我尝试过的。
我修改了上面的代码以显示:
more_info.php?start=' . urlencode($row['description']) . urlencode($row['picturepath']) . '\';" /></td>';
然后,它会提取正确的信息,但它实际上提取的是图像的文件名,而不是实际的图片。
目标
我想要“更多信息”链接,提取用户单击“更多信息”的项目的描述和图像,并将两者转储到“more_info.php”中。在这种情况下我必须使用 $_POST 吗?或者也许在 more_info.php 上运行一个查询,其中仅当图片路径与项目名称匹配时才显示图片路径?我不确定...
最佳答案
在不看到代码的情况下很难判断 more_info.php 中发生了什么,但是您需要一种方法来区分该 php 脚本中的 2 个字段(描述、图片路径)。对于 url,请使用类似以下内容:
$url = "more_info.php?start=" . urlencode($row['description']) . "&path=" . urlencode($row['picturepath']);
在 onclick 中使用 $url 字符串:
... onclick="window.location=\'$url\'" ...
然后在 more_info.php 中使用 $_GET 检索作为 URL 一部分传递的 2 个参数(开始、路径):
if(isset($_GET['start']) && isset($_GET['path'])){
$description = $_GET['start'];
$picturepath = $_GET['path'];
echo "Description: $description <img src=\"$picturepath\"/>";
}
不确定这是否有帮助/是您正在寻找的。如果不提供 more_info.php 及其当前输出。
关于php - 如何显示与其他数据对应的数据 MySQL PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20506721/