php 单击按钮将数据从 MySQL 数据库加载到 HTML 文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11273234/
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
Load data from MySQL database to HTML textboxes on button click
提问by Isuru
I have a small form for updating existing records.
我有一个用于更新现有记录的小表格。


I'm loading the Service IDs to the dropdown box using PHP. And when the user clicks the Loadbutton, it is supposed to display the other details related to that ID in the textboxes below. Here is the code I have so far.
我正在使用 PHP将服务 ID加载到下拉框中。当用户单击加载按钮时,应该会在下面的文本框中显示与该 ID 相关的其他详细信息。这是我到目前为止的代码。
<html>
<head>
</head>
<body>
<?php
//Database initialization
require_once("db_handler.php");
$conn = iniCon();
$db = selectDB($conn);
$query = "SELECT * FROM taxi_services ORDER BY SID";
$result2 = mysql_query($query, $conn);
?>
<div id="upserv">
<b id="caption2">Update location</b>
<br/><br/>
<form name="upServForm" action="<?php echo $PHP_SELF; ?>" method="post" >
<?php
$dropdown = "<select name='codes'>";
while($row = mysql_fetch_assoc($result2))
{
$dropdown .= "\r\n<option value='{$row['SID']}'>{$row['SID']}</option>";
}
$dropdown .= "\r\n</select>";
?>
Service ID <?php echo $dropdown; ?> <input type="submit" value="Load" name="loadbtn">
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="upName" style="text-align:right" value="<? echo $savedName; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="upCost" style="text-align:right" value="<? echo $savedCost; ?>" /></td>
</tr>
<tr>
<td>Active</td>
<td><input type="checkbox" name="upActive" value="<? echo $savedActive; ?>" /></td>
</tr>
</table>
</div>
<br/>
<div id="buttons">
<input type="reset" value="Clear" /> <input type="submit" value="Save" name="updatebtn" />
</div>
</form>
<?php
if(isset($_POST["loadbtn"]))
{
$id = $_POST["codes"];
$query = "SELECT Name, Cost, Active FROM taxi_services WHERE SID = '$id' ";
$result = mysql_query($query, $conn);
$details = mysql_fetch_array($result);
$savedName = $details["Name"];
$savedCost = $details["Cost"];
$savedActive = $details["Active"];
}
?>
</body>
</html>
The query gets executed just fine but the data doesn't get displayed in the textboxes. Can anyone please tell me what I am missing here?
查询执行得很好,但数据没有显示在文本框中。谁能告诉我我在这里缺少什么?
Thank you.
谢谢你。
回答by Thomas
Your query has to be before the output:
您的查询必须在输出之前:
Also note the typecast (integer)of the id to secure against sql injections.
还要注意(integer)id的类型转换以防止 sql 注入。
Also note the security issues with $PHP_SELFhttp://php.about.com/od/learnphp/qt/_SERVER_PHP.htmI changed the code to $_SERVER['SCRIPT_NAME']
还要注意$PHP_SELFhttp://php.about.com/od/learnphp/qt/_SERVER_PHP.htm的安全问题
我将代码更改为$_SERVER['SCRIPT_NAME']
ALso note to not use register_globalsand disable it in the configuration if you can (use $_SERVER['SCRIPT_NAME'] instead of$SCRIPT_NAME`) : http://www.php.net/manual/en/security.globals.php
还请注意,register_globals如果可以(使用$_SERVER['SCRIPT_NAME'] instead of$SCRIPT_NAME`),请不要在配置中使用和禁用它:http://www.php.net/manual/en/security.globals.php
If you learn php from a book and this is based on sourcecode out of this book you should throw it away immediately.
如果你从一本书中学习 php 并且这是基于本书的源代码,你应该立即扔掉它。
<?php
//Database initialization
require_once("db_handler.php");
$conn = iniCon();
$db = selectDB($conn);
$query = "SELECT * FROM taxi_services ORDER BY SID";
$result2 = mysql_query($query, $conn);
if(isset($_POST["loadbtn"]))
{
$id = (integer) $_POST["codes"];
$query = "SELECT Name, Cost, Active FROM taxi_services WHERE SID = '$id' ";
$result = mysql_query($query, $conn);
$details = mysql_fetch_array($result);
$savedName = $details["Name"];
$savedCost = $details["Cost"];
$savedActive = $details["Active"];
}
?>
<html>
<head>
</head>
<body>
<div id="upserv">
<b id="caption2">Update location</b>
<br/><br/>
<form name="upServForm" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" >
<?php
$dropdown = "<select name='codes'>";
while($row = mysql_fetch_assoc($result2))
{
$dropdown .= "\r\n<option value='{$row['SID']}'>{$row['SID']}</option>";
}
$dropdown .= "\r\n</select>";
?>
Service ID <?php echo $dropdown; ?> <input type="submit" value="Load" name="loadbtn">
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="upName" style="text-align:right" value="<? echo $savedName; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="upCost" style="text-align:right" value="<? echo $savedCost; ?>" /></td>
</tr>
<tr>
<td>Active</td>
<td><input type="checkbox" name="upActive" value="<? echo $savedActive; ?>" /></td>
</tr>
</table>
</div>
<br/>
<div id="buttons">
<input type="reset" value="Clear" /> <input type="submit" value="Save" name="updatebtn" />
</div>
</form>
</body>
</html>

