使用 html 表单和 php 与演示从 MySql 数据库中检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9774406/
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
Retrieve data from MySql database using html form and php with demo
提问by Sarp
I am new at php and I have read almost all forums, but no result!!! I am trying to retrieve some data from my mySql db using an html form and a php file. The html form called choose.htm is as follows:
我是 php 新手,几乎阅读了所有论坛,但没有结果!!!我正在尝试使用 html 表单和 php 文件从我的 mySql 数据库中检索一些数据。名为choose.htm的html表单如下:
<form name="choose" method = "POST" action = search.php>
<table>
<tr>
<tr><td height="3"></td></tr>
<td width="60"><font1>Denomination</font1></td>
<td><Select name = denom>
<option value="" selected>All</option>
<option value="half">Half Cents</option>
<option value="large">Large Cents</option>
<option value="bust">Bust Dollars</option>
<option value="morgan">Morgan Dollars</option>
</Select></td></tr>
<tr>
<tr><td height="3"></td></tr>
<td width="60"><font1>Year</font1></td>
<td><Select name = year>
<option value="" selected>All</option>
<option value="1793"><font4>1793</font></option>
<option value="1794">1794</option>
<option value="1795">1795</option>
<option value="1796">1796</option>
</Select></td></tr>
<tr>
<tr><td height="3"></td></tr>
<td width="60"><font1>Picture</font1></td>
<td><Select name = picture>
<option value="" selected>All</option>
<Option value="liberty">Liberty Cap</Option>
<Option value="draped">Draped Bust</Option>
<Option value="classic">Classic Head</Option>
<Option value="chain">Chain Reverse</Option>
</Select></td></tr>
<tr><td height="3" colspan="2"></td></tr>
<tr><td><font1>Text</b></font1></td>
<td><input name=text type=text></td></tr>
</Select></td></tr>
<tr><td><input name=look type=submit value=Submit></td></tr>
</form>
And the php file called search.php is as follows:
而名为search.php的php文件如下:
<html>
<body>
<?php
$username="root";
$password="";
$database="xxxxxxxx";
mysql_connect(localhost,$xxxxxx,$xxxxxx);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM coins";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>US Coins</center></b><br><br>";
$i=0;
while ($i < $num) {
$denom=$_POST["denom"];
$year=$_POST["year"];
$picture=$_POST["picture"];
$text=$_POST["text"];
echo "$denom<br>Year: $year<br>Picture: $picture<br>Text: $text<br><br>";
$i++;
}
?>
</body>
</html>
I am getting an output at the number of rows in my db, with the options that I have selected:
我在我的数据库中以行数获得输出,其中包含我选择的选项:
half Year: 1793 Picture: Text:
半年:1793 图片:文字:
half Year: 1793 Picture: Text:
半年:1793 图片:文字:
........ goes on.
........ 继续。
I couldn't solve the problem. All helps will be very much appreciated...
我无法解决问题。所有帮助将不胜感激...
Sarp
萨普
回答by Thew
You will need some better code if you want to find your error.
如果您想找到错误,您将需要一些更好的代码。
1) http://validator.w3.org- put your HTML code in that form and fix ALL the errors first.
2) Parse all the PHP code before you start the HTML. Else the user will see just half the loaded page for a sec. And thats not quite clean.
3) @mysql_select_db($database) or die() is wrong. Handle the PHP errors.
4) Use mysql_fetch_assoc
instead of mysql_numrows
.
1) http://validator.w3.org- 将您的 HTML 代码放入该表单并首先修复所有错误。
2) 在开始 HTML 之前解析所有 PHP 代码。否则用户将在 1 秒内只看到加载页面的一半。那不是很干净。
3) @mysql_select_db($database) 或 die() 是错误的。处理 PHP 错误。
4)使用mysql_fetch_assoc
代替mysql_numrows
。
You can thank me for cleaning up your messy code later.
你可以感谢我稍后清理你凌乱的代码。
<?php
$host = 'localhost';
$username="root";
$password="";
$database="xxxxxxxx";
$connection = @mysql_connect($host,$username,$password);
$selection = @mysql_select_db($database, $connection);
if(!$connection || !$selection){
echo 'Connection failed. Contact webmaster and ask if his connection settings are okay.';
}
$query = "SELECT * FROM coins";
$mysqlquery = mysql_query($query);
if($mysqlquery){ // Query succeed! :D
$result = mysql_fetch_assoc($mysqlquery); // We'll parse em later
}
else
{
echo 'MySQL Query failed! Give the webmaster a slap for his bad coding.';
}
mysql_close();
// We've handeled all the PHP stuff, now we can get started with printing everything
?>
<html>
<body>
<?php
echo '<b><center>US Coins</center></b><br><br>'; // This HTML code needs cleanup too
$i = 0;
while($i < $num){
echo htmlspecialchars($_POST['denom']).'<br>Year: '. htmlspecialchars($_POST['year']) .'<br>Picture: '. htmlspecialchars($_POST['picture']) .'<br>Text: '. $_POST['text'] .'<br><br>';
$i++;
}
?>
</body>
</html>
回答by bkconrad
$_POST
contains the values submitted with the form. There is no reason to be printing it in a loop, as in your case each key has a single value. If you want to output data from your MySQLresults you will need to use something like mysql_fetch_array
in a while
loop, then output the values of each of the arrays this returns.
$_POST
包含随表单提交的值。没有理由循环打印它,因为在您的情况下,每个键都有一个值。如果你想从你的MySQL结果中输出数据,你需要mysql_fetch_array
在while
循环中使用类似的东西,然后输出这个返回的每个数组的值。
A good place to start is the PHP MySQL Manualwhich contains a working example from which you can easily deduce the basics (even if you don't feel like R'ing TFM).
一个很好的起点是PHP MySQL 手册,其中包含一个工作示例,您可以从中轻松推断出基础知识(即使您不喜欢 R'ing TFM)。
回答by Cybernetiquettes
If you do not have any data in your database, the query will return an empty result. So, make sure that you have some data in your database.
如果您的数据库中没有任何数据,则查询将返回空结果。因此,请确保您的数据库中有一些数据。
try something like this...
尝试这样的事情......
$num=mysql_fetch_array(mysql_query("select * from coins"))
while ($i<$num) {
echo $num . "<br />";
}