php 如何获取 HTML 表单以从 Mysql 数据库中查询和生成结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14536984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a HTML Form to query and produce results from Mysql datebase
提问by user2013520
I have created a form that has one text field and one select box, I also have created a button. Now what I want to happen is when the button is pressed I want the string in the forms to be matched up to my database and then I want the results of those matches to be returned and shown on the screen.
我创建了一个包含一个文本字段和一个选择框的表单,我还创建了一个按钮。现在我想要发生的是,当按下按钮时,我希望表单中的字符串与我的数据库相匹配,然后我希望返回这些匹配的结果并显示在屏幕上。
For example if I was to type 'History' into my text field box and I also selected 'level 2' from my select box when the button (Submit Button) was pressed I would want returned on the page everything that matches up with the word history and the selection of level 2 in my database.
例如,如果我要在文本字段框中键入“历史记录”,并且在按下按钮(提交按钮)时我还从选择框中选择了“级别 2”,我希望在页面上返回与该词匹配的所有内容我的数据库中的历史记录和级别 2 的选择。
I know that I have to connect my page with my Database through PHP and I have successfully done that, but what I don't understand is how to then get my HTML Form to Query the database and provide results back onto the screen
我知道我必须通过 PHP 将我的页面与我的数据库连接,我已经成功地做到了这一点,但我不明白的是如何让我的 HTML 表单查询数据库并将结果返回到屏幕上
In terms of a example website that is very similar to the concept I would like to create take a look at this webpage. http://search.ucas.com/cgi-bin/hsrun/search/search/search.hjx;start=search.HsSearch.run?y=2013&w=H(UCAS Course Search) this website has multiple text fields and select boxes and a submit button exactly as I am trying to create and the results provided are only the results that match with what has been searched.
就一个与我想创建的概念非常相似的示例网站而言,请查看此网页。http://search.ucas.com/cgi-bin/hsrun/search/search/search.hjx;start=search.HsSearch.run?y=2013&w=H(UCAS Course Search) 这个网站有多个文本字段,选择框和提交按钮与我试图创建的完全一样,提供的结果只是与搜索结果相匹配的结果。
The code below works in terms of it links my text-field to my database but I can't get my text-field and my select box to link and query the database, only one or the other. I want them both to work from the same button (search button)
下面的代码将我的文本字段链接到我的数据库,但我无法让我的文本字段和我的选择框链接和查询数据库,只有一个或另一个。我希望它们都使用同一个按钮(搜索按钮)
<form method="post" action="Webpage here" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<form id="form1" name="ExamBoard" method="post" action="Webpage here">
<label for="select"></label>
<select name="ExamBoard" id="select">
<option value="EB1" selected="selected">EB1</option>
<option value="EB2">EB2</option>
<option value="EB3">EB3</option>
</select>
<input type="submit" name="submit" value="Search">
</form>
<p> </p>
<p>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
//connect to the database
$db=mysql_connect ("Name", "User", "Password*") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("Table Name");
//-query the database table
$sql="SELECT ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name . "%' ";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$CourseName =$row['CourseName'];
$ID=$row['ID'];
$ExamBoard=$row['ExamBoard'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"search.php?id=$ID\">" .$CourseName . " " . "</a></li>\n" ;
echo $ExamBoard . " " . "</a>\n";
echo "</ul>";
}
}
}
}
?>
回答by user1551869
New HTML File
新建 HTML 文件
newHTML.htm
新HTML.htm
<form method="post" action="Webpage here" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form
New PHP file
新建 PHP 文件
newPHP.pfp
新建PHP.pfp
<?php
if(preg_match("/^[ a-zA-Z]+/", $_REQUEST['name'])){
$name=$_REQUEST['name'];
//connect to the database
$db=mysql_connect ("Name", "User", "Password*") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("Table Name");
//-query the database table
$sql="SELECT ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name . "%' ";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$CourseName =$row['CourseName'];
$ID=$row['ID'];
$ExamBoard=$row['ExamBoard'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"search.php?id=$ID\">" .$CourseName . " " . "</a></li>\n" ;
echo $ExamBoard . " " . "</a>\n";
echo "</ul>";
}
?>
回答by user1551869
You have to ask yourself Do you want to do this with AJAX or with normal screen refreshes..
你必须问问自己,你想用 AJAX 还是普通的屏幕刷新来做到这一点。
With normal screen refreshes (The easy way)
使用正常的屏幕刷新(简单的方法)
Take your HTML and place it in one file (HTMLfile.html) Take your PHP and place it in another file (PHPCode.php)
将您的 HTML 放入一个文件 (HTMLfile.html) 将您的 PHP 放入另一个文件 (PHPCode.php)
HTML file form calls the PHP script from action attribute.
HTML 文件形式从 action 属性调用 PHP 脚本。
PHP file renders the response.
PHP 文件呈现响应。
Dont mash up the view and the controlling application.
不要混搭视图和控制应用程序。
There is other ways to do this in one script if you want to use AJAX.
如果您想使用 AJAX,还有其他方法可以在一个脚本中执行此操作。

