简体   繁体   中英

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 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM