单击php中的链接(来自数据库)后没有任何显示
Posted
技术标签:
【中文标题】单击php中的链接(来自数据库)后没有任何显示【英文标题】:nothing showed up after click a link(from database) in php 【发布时间】:2018-07-26 11:44:43 【问题描述】:我有一个名为 simple_stall
的数据库,其中表 order_detail
有 4 列 ID
Name
Ordered_Item
Quantity
...当前在用户提交订单后,他们将被重定向到一个页面称为order_detail.php
...此页面将显示表中所有已订购的项目,标题为ID
Name
Ordered_Item
Quantity
现在,当用户从表中单击某人的姓名时,我想将用户重定向到一个名为 view_more.php
的新页面,该页面将显示用户订购的项目,但是页面中没有显示任何内容。
这是我的代码:
index.php
<div class="container">
<form action="insert_data.php" method="POST">
<div>
<input type="text" name="Name" placeholder="Name">
</div>
<div>
<input type="text" name="Order" placeholder="Order">
</div>
<div>
<input type="text" name="Quantity" placeholder="Quantity">
</div>
<div>
<button type="submit" name="submit">Send Order</button>
</div>
</form>
</div>
insert_data.php
if (isset($_POST['submit']))
include_once 'dbh.php';
// Escape user inputs for security
$name = mysqli_real_escape_string($connection, $_POST['Name']);
$order = mysqli_real_escape_string($connection, $_POST['Order']);
$quantity = mysqli_real_escape_string($connection, $_POST['Quantity']);
// attempt insert query execution
$sql = "INSERT INTO order_detail (Name, Ordered_Item, Quantity) VALUES ('$name', '$order', '$quantity')";
if(mysqli_query($connection, $sql))
header("Location: ./order_detail.php?status=ordered");
else
echo "ERROR: Could not able to execute $sql. " . mysqli_error($connection);
// close connection
mysqli_close($connection);
else
header("Location: ./index.php?status=failed");
exit();
order_detail.php
<body>
<table>
<tr>
<th >No</th>
<th >Name</th>
<th >Ordered Item</th>
<th >Quantity</th>
</tr>
<?php
include_once 'dbh.php';
$query = "SELECT * FROM order_detail"; //You don't need a ; like you do in SQL
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the html
while($row = mysqli_fetch_array($result))
$name = $row['Name'];
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
echo "</table>"; //Close the table in HTML
mysqli_close($connection); //Make sure to close out the database connection
?>
view_more.php
if (isset($_POST['Name']))
include_once 'dbh.php';
$name = $row['Name'];
$query = "SELECT * FROM order_detail WHERE Name = $name";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
【问题讨论】:
【参考方案1】:它不会显示,
因为在view_more.php
上你有if (isset($_POST['Name']))
这永远不会是真的,因为你没有在view_more.php
上使用$_POST
,你正在使用<td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td>
你正在使用普通链接所以用这个代码替换它
if (isset($_GET['id']))
include_once 'dbh.php';
$name = $_GET['id'];
$query = "SELECT * FROM order_detail WHERE Name = '$name'";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
你应该很高兴,但是,我强烈建议你使用适当的 php 框架。
【讨论】:
嘿,谢谢..但现在它显示Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\simple_stall\view_more.php on line 14
有什么原因吗?另外,php框架是什么意思?喜欢method=post
?
返回空白页
哦,等等,在order_detail.php
中发现了错误的代码,它可以工作!谢谢~【参考方案2】:
在生成指向view_more.php
页面的链接时,您正在注入一个名为id
的GET 参数:
<a href='view_more.php?id=$name'>
在view_more.php
页面上,您正在测试一个名为name
的POST 参数:
if (isset($_POST['Name']))
解决这个问题
if (isset($_GET['id']))
顺便说一句,你的代码真的很丑。有很多事情做错了:
SQL 查询中未转义的参数:您会接触到SQL injections,这是一个严重的安全漏洞。 耦合 PHP 和 HTML 脚本:看看 Separation of Concerns 和 MVC Design pattern【讨论】:
以上是关于单击php中的链接(来自数据库)后没有任何显示的主要内容,如果未能解决你的问题,请参考以下文章
当用户单击数据表中的“查看链接”时,在PHP页面中查看详细信息
在输入'php artisan storage:link'命令后,如何使用Laravel链接存储文件夹在Heroku上显示图像?