javascript 从数据库中检索数据并显示在文本框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22278260/
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 database and display in textbox
提问by user3396187
How am i going to display the data from the database into the textbox? pls help
我如何将数据库中的数据显示到文本框中?请帮忙
//Javascript textbox
<div class="Text">
<input class="Text" type="text" value="
<?PHP echo $id?>" name="id" size="19"/>
//PHP MYSQL Connect code
<?php
error_reporting(0);
include('../connection.php');
$id =$_REQUEST['id'];
$result = mysql_query("SELECT * FROM cust WHERE id = '$id'");
$test = mysql_fetch_array($result);
if (!$result)
{
die("Error: Data not found..");
}
$id=$test['id'] ;
?>
回答by Tun Zarni Kyaw
Place your PHP code beforeHTML
将 PHP 代码放在HTML之前
//PHP MYSQL Connect code
<?php
error_reporting(0);
include('../connection.php');
$id =$_REQUEST['id'];
$result = mysql_query("SELECT * FROM cust WHERE id = '$id'");
$test = mysql_fetch_array($result);
if (!$result)
{
die("Error: Data not found..");
}
$id=$test['id'] ;
?>
//Javascript textbox
<div class="Text">
<input class="Text" type="text" value="
<?PHP echo $id?>" name="id" size="19"/>
Note:mysql_*
functions are deprecated. please try to use mysqli_*
or PDO
.
注意:mysql_*
不推荐使用函数。请尝试使用mysqli_*
或PDO
。
回答by Jay Patel
Put following PHP code before your HTML,
将以下 PHP 代码放在 HTML 之前,
PHP
PHP
<?php
$con = new mysqli_connect(host,user,pass,dbname);
$id = $_REQUEST['id'];
$query = "SELECT * FROM cust WHERE id = '$id'";
$result = mysqli_query($query);
if (!$result)
{
die("Error: Data not found..");
}
$test = mysqli_fetch_array($result);
$id=$test['id'] ;
?>
HTML & PHP (inside body)
HTML 和 PHP(正文内部)
<div class="Text">
<input class="Text" type="text" value="<?PHP echo $id; ?>" name="id" size="19"/>
Hope this help you!
希望这对你有帮助!
回答by Jaison Justus
One good approach for element rendering is to make HTML Helper Libraries. Like for example create a class HTML having a set of static tag creator methods.
元素呈现的一种好方法是制作 HTML 帮助程序库。例如,创建一个具有一组静态标签创建者方法的 HTML 类。
#Pseudo HTML helper class - HTML.class.php
class HTML
public static input(type, id, class, data, text)
public static heading(mode, text)
#Pseudo input tag helper - HTML.class.php::input
function input(type, id, value) {
# method create the html string for the given input.
return ["<input type=",type," id=",id," value=",value,"/>"].join('');
}
<?php
$con = new mysqli(host,user,pass,dbname);
$query = "SELECT * FROM cust WHERE id = '$id'";
$result = $con-> query($query);
while ($row = $result->fetch_assoc()){
$value = $row['id'];
echo HTML::input('text', id, $id);
}
?>
I think this just the same as above answers. but i prefer when you write code it should be modular, clean and beautiful. Always use good practices. Thats why i shared my thought here. If you create your own or others helper class help you with faster development also i suggest contributions to it help you in learning. I know that this is not the answer what you are looking, but anyway free to revert back at any time.
我认为这与上述答案相同。但我更喜欢当你编写代码时,它应该是模块化的、干净的和漂亮的。始终使用良好的做法。这就是为什么我在这里分享我的想法。如果您创建自己的或其他帮助类可以帮助您更快地开发,我也建议您对其做出贡献,以帮助您学习。我知道这不是您正在寻找的答案,但无论如何可以随时恢复。