2个问题......
情景:
我想通过表单查询我的数据库表,然后显示发生的结果(如果有结果),我目前的情况是它确实有效,但它完全清除了表单并将我留给了一个空页面只有结果才有意义。我想在我输入表单数据的同一页面上看到我的结果。 我希望以基于表格的格式看到我的数据,有点像......
|Name | Age|
|-------|----|
|Anthony| 20 |
我有一个单独的设计,以使其看起来很晚。 这是我到目前为止的设置......
displayform.html:
<html>
<form method="post" name="display" action="display.php" />
Enter the name you like to display the data from MySQL:<br>
<input type="text" name="name" />
<input type="submit" name="Submit" value="display" />
</form>
</html>
和display.php
<?php
mysql_connect("localhost", "toor", "FakePassword") or die("Connection Failed");
mysql_select_db("FakeDatabase")or die("Connection Failed");
$name = $_POST['name'];
$query = "select name, age from test WHERE name = '$name'";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$line['name']."</td>";
echo "<td>".$line['age']."</td>";
echo "<br>\n";
echo "</tr>";
}
?>
答案 0 :(得分:2)
mysqli_*
代替deprecated mysql_*
API 将它们组合在一个像这样的文件中display.php
):
<html>
<form method="post" name="display" action="display.php" />
Enter the name you like to display the data from MySQL:<br>
<input type="text" name="name" />
<input type="submit" name="Submit" value="display" />
</form>
<?php
mysql_connect("localhost", "toor", "FakePassword") or die("Connection Failed");
mysql_select_db("FakeDatabase")or die("Connection Failed");
if(!empty($_POST["name"])){ /* WE ADD THIS PART SO WHEN NO PASSED DATA IS FOUND, IT WILL NOT GENERATE "UNIDENTIFIED VARIABLE" ERROR */
$name = mysql_real_escape_string($_POST['name']); /* SANITIZE THE VALUE OF THIS VARIABLE */
$query = "select name, age from test WHERE name = '$name'";
$result = mysql_query($query);
echo "<table>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$line['name']."</td>";
echo "<td>".$line['age']."</td>";
echo "<br>\n";
echo "</tr>";
} /* END OF WHILE LOOP */
echo "</table>";
} /* END OF NOT EMPTY NAME */
?>
</html>
如果您对mysqli_*
感兴趣,建议您使用prepared statement。只需将您的php部分替换为:
<?php
/* ESTABLISH YOUR CONNECTION FIRST */
$con = new mysqli("localhost", "toor", "FakePassword", "FakeDatabase");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if(!empty($_POST["name"])){
if($stmt = $con->prepare("SELECT name, age FROM test WHERE name = ?")){
$stmt->bind_param("s",$_POST["name"]); /* BIND THE PASSED-ON VALUE TO THE QUERY */
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->bind_result($name,$age); /* BIND THE RESULT TO THESE VARIABLES */
echo "<table>";
while($stmt->fetch()){ /* FETCH ALL RESULTS */
echo "<tr>";
echo "<td>".$name."</td>";
echo "<td>".$age."</td>";
echo "<br>\n";
echo "</tr>";
} /* END OF WHILE LOOP */
echo "</table>";
$stmt->close();
} /* END OF PREPARED STATEMENT */
} /* END OF NOT EMPTY NAME */
?>
答案 1 :(得分:0)
如果您想要列名称,则必须使用此查询:SHOW COLUMNS FROM test
这是一个有效的例子。
另外,请注意SQL injection。如果你没有收到它,我的网络服务器会向我的屏幕发出警告:
不推荐使用mysql扩展程序,将来会将其删除:改为使用mysqli或PDO
<?php
mysql_connect("localhost", "toor", "FakePassword") or die("Connection Failed");
mysql_select_db("FakeDatabase")or die("Connection Failed");
$query = "SHOW COLUMNS FROM test";
$result = mysql_query($query);
echo "<tr>";
while ($header = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<td>". $header['Field']."</td>";
}
echo "<br>\n";
echo "</tr>";
$name = $_POST['name'];
$query = "select name, age from test WHERE name = '$name'";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td></td>";
echo "<td>".$line['name']."</td>";
echo "<td>".$line['age']."</td>";
echo "<br>\n";
echo "</tr>";
}
?>
修改强>
我回答了问题标题中描述的主要问题。下次,你应该发一个问题......每个问题。祝你好运。