[英]Retrieve data from database and display in textbox
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'] ;
?>
Place your PHP code before HTML将 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.注意:不推荐使用mysql_*
函数。 please try to use mysqli_*
or PDO
.请尝试使用mysqli_*
或PDO
。
Put following PHP code before your HTML,将以下 PHP 代码放在 HTML 之前,
<?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'] ;
?>
<div class="Text">
<input class="Text" type="text" value="<?PHP echo $id; ?>" name="id" size="19"/>
Hope this help you!希望这对你有帮助!
One good approach for element rendering is to make HTML Helper Libraries.元素呈现的一种好方法是制作 HTML 帮助程序库。 Like for example create a class HTML having a set of static tag creator methods.例如,创建一个具有一组静态标签创建者方法的 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.我知道这不是您正在寻找的答案,但无论如何可以随时恢复。
Working Solution工作解决方案
<?php
include("database.php");
$db=$conn;
// fetch query
function fetch_data(){
global $db;
$query="SELECT * FROM users WHERE username='vii'"; // change this
$exec=mysqli_query($db, $query);
if(mysqli_num_rows($exec)>0){
$row= mysqli_fetch_all($exec, MYSQLI_ASSOC);
return $row;
}else{
return $row=[];
}
}
$fetchData= fetch_data();
show_data($fetchData);
foreach($fetchData as $data){
$firstname=$data['udid'];} // change this 'udid' to your table field
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Retrieve Contact</title>
</head>
<body>
<input type=text value= <?php echo $firstname; ?>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.