首页 > 解决方案 > 执行php脚本后如何将mysql查询结果返回到html页面?

问题描述

我有一个用户输入 2 个变量的表单。这两个变量在我的 mysqlquery 中使用。结果可以是:没有匹配项或 1 个或多个匹配项。在每种情况下,我都希望将该 sql 查询的输出作为输入字段下方的原始网页上的结果(在“queryresult”文本字段中)。怎么做?

查询正在运行,但是在单击按钮后会打开一个新页面,其中包含我不想要的查询结果。

您可以在此处查看表格:www.larscichowski.nl/coinexchange

我已经尝试使用隐藏的 iframe 并检查了类似问题的答案

在 html 中,这是表单部分的代码:

        <section class="section-form" id="form">
            <div class="row" >
                <h2>Coin Exchange Finder</h2>
            </div>
            <div class="row">
            <form method="get" action="query.php" class="contact-form">



                    <div class="row">


                        <div class="col span-1-of-3">
                            <label for="name">Source Coin</label>
                        </div>

                        <div class="col span-1-of-3">
                            <input class="typeahead form-control" name="sourcecoin" id="sourcecoin" type="text" required>
                        </div>



                    </div>
                    <div class="row">


                        <div class="col span-1-of-3">
                            <label for="name">Destination Coin</label>
                        </div>

                        <div class="col span-1-of-3">
                            <input class="typeahead form-control" name="destcoin" id="destcoin" type="text" >
                        </div>
                    </div>
                    <script type="text/javascript">



                    <div class="row">
                        <div class="col span-1-of-3">
                            <label>&nbsp;</label>
                        </div>

                        <div class="col span-2-of-3">
                            <input type="submit" value="Find matching 
 exchanges">
                        </div>
                    </div>

                    <div class="row">
                        <div class="col span-1-of-3">
                            <label>We found the following matches:</label>
                        </div>
                        <div class="col span-2-of-3">
                            <input type="text" id="queryResult"/>
                        </div>
                    </div>
                </form>
            </div>
        </section>

query.php 文件如下所示:

<?php
$servername = "xx";
$username = "xx";
$password = "xx";
$dbname = "xx";
$sourcecoin = strip_tags(trim($_POST["sourcecoin"]));
$destcoin = strip_tags(trim($_POST["destcoin"]));


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    echo "Connection not established. Check credentials";
} 


$sql = "SELECT Pairs_Source.Exchange, Exchanges.HyperLink
FROM Pairs AS Pairs_Source INNER JOIN Pairs AS Pairs_Dest ON 
Pairs_Source.Exchange = Pairs_Dest.Exchange
Left join Exchanges on Pairs_Source.Exchange=Exchanges.Exchange
WHERE Pairs_Source.Coin='$sourcecoin' AND Pairs_Dest.Coin='$destcoin'";


$result = $conn->query($sql);



$json = [];
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $json[]=$row['Exchange'];
        //echo "<br> They have got the following exchange(s) in common: ". 
 $row["Exchange"] ."<br>";
    }

} else {
        echo "Unfortunately these 2 coins don't have an exchange in 
 common";
}
    echo json_encode($json);

$conn->close();
?>

标签: phphtmlmysql

解决方案


您可以将表单提交到同一页面并将 php 代码放在同一文件中。

为此,请将您的 PHP 代码包装在 中if($_SERVER['REQUEST_METHOD'] == 'POST'),这样它只会在提交表单时运行。


您需要更改表单标签,以便提交到当前页面(或操作中的页面名称):

<form method="post" action="" class="contact-form">

然后,您可以将结果部分更改为类似的内容。(因此它将您要显示的消息存储到变量中):

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $message = "<br> They have got the following exchange(s) in common: 
 ". $row["Exchange"] ."<br>";
     }

 } else {
         $message = "Unfortunately these 2 coins don't have an exchange in 
 common";
 }

最后,您可以通过执行以下操作在页面中的任何位置回显消息:

if(isset($message)) {
    echo $message;
}

您的页面将如下所示:

if($_SERVER['REQUEST_METHOD'] == 'POST') {

  // The contents of query.php

}

// the html code

推荐阅读