在php中单击链接(来自数据库)后没有显示任何内容

ttcibm8c  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(337)

我有一个数据库叫 simple_stall 带table order_detail 它有4列
ID Name Ordered_Item Quantity …当前在用户提交订单后,他们将被重定向到名为 order_detail.php …此页将显示带有标题的表中的所有订购项
ID Name Ordered_Item Quantity 现在,当用户从表中单击某人的名字时,我想将用户重定向到一个名为 view_more.php 它将显示用户订购的项目,但是页面中没有显示任何内容。
这是我的密码: index.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`

`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);

}

xmq68pz9

xmq68pz91#

它不会出现,
因为在 view_more.php 你有 if (isset($_POST['Name'])) 因为你没有使用 $_POSTview_more.php ,您正在使用 <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框架。

hjzp0vay

hjzp0vay2#

生成到的链接时 view_more.php 佩奇,你正在注入一个名为 id :

<a href='view_more.php?id=$name'>

在街上 view_more.php page,您正在测试一个名为 name :

if (isset($_POST['Name']))

把这个固定到

if (isset($_GET['id']))

顺便说一下,你的代码真的,真的很难看。有很多事情做得不对:
sql查询中的unescaped参数:您暴露于sql注入,这是一个严重的安全漏洞。
耦合的php和html脚本:看一下关注点的分离和mvc设计模式

相关问题