将mysql数据库中的数据加载到html文本框中

bqf10yzr  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(463)

我正在尝试从页面中的数据库表加载数据。
我在下面的报告是一个示范性的例子,来解释我想如何实现它。。。实际上,我们讨论的是数据库的100个字段和1000个字段,并传递表的行代码。。。

  1. <?php
  2. $servername = "localhost";
  3. $username = "progettocantiere";
  4. $password = "";
  5. $dbname = "my_progettocantiere";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10. die("Connection failed: " . $conn->connect_error);
  11. }
  12. $idCantiere = $_GET['idCantiere'];
  13. $sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
  14. $result = mysqli_query($sql1, $conn);
  15. $details = mysqli_fetch_array($result);
  16. $savedNomeCantiere = $details["nomeCantiere"];
  17. $savedCodiceCommessa = $details["codiceCommessa"];
  18. $savedIndirizzoCantiere = $details["indirizzoCantiere"];
  19. var_dump($details);
  20. $result1 = $conn->query($sql1);
  21. echo($nomeCantiere);
  22. ?>
  23. <html>
  24. <head>
  25. </head>
  26. <body>
  27. <table width="300" border="0">
  28. <tr>
  29. <td>Name</td>
  30. <td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
  31. </tr>
  32. <tr>
  33. <td>Cost</td>
  34. <td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
  35. </tr>
  36. <tr>
  37. <td>Cost</td>
  38. <td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
  39. </tr>
  40. </table>
  41. <br/>
  42. </body>
  43. </html>

我尝试过这种上传方式,在文本框的“value”中添加“echo”,但它不起作用。
此行用于通过页面重定向派生“id”。

  1. $idCantiere = $_GET['idCantiere'];

如果我想试试 var_dump($details) 它返回空值

pftdvrlh

pftdvrlh1#

其中一个问题是你通过了 $conn 以及 $sql1 顺序不对。 $result = mysqli_query($sql1, $conn); 应该是 $result = mysqli_query($conn, $sql1); 如果我想尝试var\u转储($details),它将返回null
这正是你需要检查的原因 $result 在使用之前。 mysqli_query 返回一个 object 如果成功了或者 false 如果失败了-http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues。
你不需要在这里花哨,但你至少应该做一个精神检查。

  1. ...
  2. $sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
  3. $result = mysqli_query($sql1, $conn);
  4. if ($result !== false) {
  5. $details = mysqli_fetch_array($result);
  6. ...

另外,我必须指出 "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'"; 因为你无法控制 $idCantiere . 请查阅sql注入以及如何避免它。

tcbh2hod

tcbh2hod2#

你的代码应该是这样的

  1. <?php
  2. $servername = "localhost";
  3. $username = "progettocantiere";
  4. $password = "";
  5. $dbname = "my_progettocantiere";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10. die("Connection failed: " . $conn->connect_error);
  11. }
  12. $idCantiere = $_GET['idCantiere'];
  13. $sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '{$idCantiere'}";
  14. $result = $conn->query($sql1);
  15. $details = $result->fetch_array();
  16. $savedNomeCantiere = $details["nomeCantiere"];
  17. $savedCodiceCommessa = $details["codiceCommessa"];
  18. $savedIndirizzoCantiere = $details["indirizzoCantiere"];
  19. var_dump($details);
  20. $result1 = $conn->query($sql1);
  21. echo($nomeCantiere);
  22. ?>
  23. <html>
  24. <head>
  25. </head>
  26. <body>
  27. <table width="300" border="0">
  28. <tr>
  29. <td>Name</td>
  30. <td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
  31. </tr>
  32. <tr>
  33. <td>Cost</td>
  34. <td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
  35. </tr>
  36. <tr>
  37. <td>Cost</td>
  38. <td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
  39. </tr>
  40. </table>
  41. <br/>
  42. </body>
  43. </html>
展开查看全部

相关问题