gpt4 book ai didi

php - 执行php脚本后如何将mysql查询结果返回到html页面?

转载 作者:行者123 更新时间:2023-11-29 16:06:08 24 4
gpt4 key购买 nike

我有一个表单,用户输入 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();
?>

最佳答案

您可以将表单提交到同一页面,并将 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

关于php - 执行php脚本后如何将mysql查询结果返回到html页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55723720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com