将mySql数据库中的数据加载到HTML文本框中

时间:2018-10-02 13:14:34

标签: php html mysql database load

我正在尝试从页面内的数据库表中加载数据。

我在下面报告的

是一个说明性示例,以解释我如何实现它... 实际上,我们谈论的是数据库的100个字段和1000个,并传递表的代码行...

<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$idCantiere = $_GET['idCantiere'];

$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";



    $result = mysqli_query($sql1, $conn);
    
   
    $details = mysqli_fetch_array($result);




    $savedNomeCantiere = $details["nomeCantiere"];
    $savedCodiceCommessa = $details["codiceCommessa"];
    $savedIndirizzoCantiere = $details["indirizzoCantiere"];

var_dump($details);


$result1 = $conn->query($sql1);

echo($nomeCantiere);

?>


<html>
<head>
</head>
<body>


   
        <table width="300" border="0">
          <tr>
            <td>Name</td>
            <td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
          </tr>
          <tr>
            <td>Cost</td>
            <td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
          </tr>
           <tr>
            <td>Cost</td>
            <td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
          </tr>
          
        </table>

<br/>

   
</body>
</html>

我尝试过这种上载类型,该上载类型是在文本框的“值”中放入“回声”,但不起作用。

此行用于通过页面重定向派生“ id”。

$idCantiere = $_GET['idCantiere'];

如果我想尝试var_dump($details),则返回NULL

2 个答案:

答案 0 :(得分:0)

您的代码应该像这样

<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$idCantiere = $_GET['idCantiere'];

$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '{$idCantiere'}";



    $result = $conn->query($sql1);


    $details = $result->fetch_array();




    $savedNomeCantiere = $details["nomeCantiere"];
    $savedCodiceCommessa = $details["codiceCommessa"];
    $savedIndirizzoCantiere = $details["indirizzoCantiere"];

var_dump($details);


$result1 = $conn->query($sql1);

echo($nomeCantiere);

?>


<html>
<head>
</head>
<body>



        <table width="300" border="0">
        <tr>
            <td>Name</td>
            <td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
        </tr>
        <tr>
            <td>Cost</td>
            <td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
        </tr>
        <tr>
            <td>Cost</td>
            <td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
        </tr>

        </table>

<br/>


</body>
</html>

答案 1 :(得分:0)

问题之一是您以错误的顺序传递了$conn$sql1

$result = mysqli_query($sql1, $conn);应该是$result = mysqli_query($conn, $sql1);

  

如果我想尝试var_dump($ details),它将返回NULL

这就是为什么在使用$result之前需要检查它的原因。 mysqli_query如果成功则返回object,如果失败则返回false-http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues

您不需要在这里花哨,但至少应该进行健全性检查。

...
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = mysqli_query($sql1, $conn);

if ($result !== false) {
    $details = mysqli_fetch_array($result);
    ...

此外,我还必须指出"SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";是非常危险的,因为您无法控制$idCantiere。请查找SQL注入及其避免方法。