1

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'] ;

            ?>

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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!

Comments

0

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.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.