单击PHP中的链接(来自数据库)后,没有任何显示

时间:2018-07-26 11:44:43

标签: php html mysql sql database

我有一个名为simple_stall的数据库,该数据库的表order_detail有4列ID Name Ordered_Item Quantity ...目前在用户之后提交订单,他们将被重定向到名为order_detail.php的页面...此页面将在表中显示标题为ID Name Ordered_Item {{1 }}

现在,当用户从表中单击某人的名字时,我想将用户重定向到名为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 width="30px">No</th> <th width="30%">Name</th> <th width="30%">Ordered Item</th> <th width="50px">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

2 个答案:

答案 0 :(得分:1)

它不会显示,

因为在view_more.php上您拥有if (isset($_POST['Name'])),这将永远是不正确的,因为您没有在$_POST上使用view_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框架。

答案 1 :(得分:0)

在生成指向HoloLens页面的链接时,您正在注入名为view_more.php的GET参数:

id

<a href='view_more.php?id=$name'> 页上,您正在测试名为view_more.php的POST参数:

name

将此问题修复为

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

顺便说一句,您的代码确实非常丑陋。有很多事情做错了: