簡體   English   中英

從數據庫中檢索數據並顯示在文本框中

[英]Retrieve data from database and display in textbox

我如何將數據庫中的數據顯示到文本框中? 請幫忙

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

            ?>

將 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"/>

注意:不推薦使用mysql_*函數。 請嘗試使用mysqli_*PDO

將以下 PHP 代碼放在 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(正文內部)

<div class="Text">
<input class="Text" type="text" value="<?PHP echo $id; ?>" name="id" size="19"/>

希望這對你有幫助!

元素呈現的一種好方法是制作 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);
     }

?>

我認為這與上述答案相同。 但我更喜歡當你編寫代碼時,它應該是模塊化的、干凈的和漂亮的。 始終使用良好的做法。 這就是為什么我在這里分享我的想法。 如果您創建自己的或其他幫助類可以幫助您更快地開發,我也建議對它做出貢獻,以幫助您學習。 我知道這不是您正在尋找的答案,但無論如何可以隨時恢復。

工作解決方案

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM