【发布时间】:2019-12-26 10:17:27
【问题描述】:
我想通过 HTML 表单输入字段获取用户输入,然后将此输入发送到 SQL 查询。 我已经尝试了我搜索的方法,但没有选择任何值。 当我直接在 SQL 查询中给出值时,SQL 查询有效。
<body>
<div class="testbox">
<form action="train.php" method="POST">
<div class="item">
<div class="train-item">
<p>Departure Station </p>
<input type="text" name="depart" required />
</div>
<div class="train-item">
<p>Arrival Station</p>
<input type="text" name="arrive" required />
</div>
<div class="btn-block">
<button type="submit" name="search" href="/">Search</button>
</div>
</div>
</form>
</div>
<?php
if(isset($_POST['search'])){ // Fetching variables of the form which travels in URL
$depart = $_POST["depart"];
$arrive = $_POST["arrive"];
$sql = "SELECT ttype.TyName, train.TraNo, a.StaName dsta, c.Time dtime ,b.StaName asta , d.Time atime
FROM (((((ttype RIGHT JOIN train
ON ttype.TyNo=train.TyNo)
RIGHT JOIN pass c
ON train.TraNo=c.TraNo)
RIGHT JOIN station a
ON a.StaNo=c.StaNo)
RIGHT JOIN pass d
ON train.TraNo=d.TraNo)
RIGHT JOIN station b
ON b.StaNo=d.StaNo)
WHERE c.Time < d.Time
AND a.StaName='.$depart'
AND b.StaName='.$arrive' ";
$result = mysqli_query($link, $sql) or die("can't reach" . mysqli_error( ));
$rows = mysqli_num_rows($result);
$cols = mysqli_num_fields( $result);
$train_table = "";
$train_table .= "This query has ". $rows ." data";
$train_table .= ",and include". $cols ."columns";
?>
上面的代码显示“这个查询有0条数据,包括6列。
编辑: 感谢您帮助并告知我有关 SQL 注入的信息。 这是大学课程的学期项目。结果只会显示给我的同学和教授,我是唯一可以访问整个系统的人。再次感谢。
我发布自己的问题的原因是我不熟悉 PHP 或 HTTP 语言,也找不到我的代码的确切问题。换句话说,我不确定问题出在 HTTP 部分还是 PHP 部分。
【问题讨论】:
-
分享你的数据库结构和你需要的输出。只有这样我们才能为您提供任何建议
-
请检查我的答案,让我知道您的查询现在是否有效?
-
@Dharman 感谢您提供信息。我发布自己的问题的原因是我不熟悉 PHP 语言并且找不到我的代码的确切问题。换句话说,我不确定问题出在 HTTP 部分还是 PHP 部分。