admin管理员组文章数量:1406149
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'] ;
?>
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'] ;
?>
Share
Improve this question
asked Mar 9, 2014 at 4:06
user3396187user3396187
631 gold badge5 silver badges15 bronze badges
4 Answers
Reset to default 1Place your PHP code before 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
.
Put following PHP code before your HTML,
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)
<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. Like for example create a class HTML having a set of static tag creator methods.
#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>
本文标签: javascriptRetrieve data from database and display in textboxStack Overflow
版权声明:本文标题:javascript - Retrieve data from database and display in textbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744975935a2635515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论