duanheye7423 2018-09-28 09:30
浏览 134
已采纳

如何在HTML按钮中加载mySql中的数据?

I want to load data from my MySql database and put one field of this database into the text of a button :

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

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

  $sql    = "SELECT nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
      // output data of each row
      while ($row = $result->fetch_assoc()) {
          echo "nomeCantiere: " . $row["nomeCantiere"] . " - codieCommessa: " . $row["codieCommessa"] . " - indirizzoCantiere" . $row["indirizzoCantiere"] . "<br>";
      }
  } else {
      echo "0 results";
  }
  $conn->close();
?>
<input type="button" value="button" />

I'm a beginner with this type of programming.

For now I have managed to do is generate a simple "echo".

How can I automatically generate buttons with a value="nomeCantiere"?

  • 写回答

2条回答 默认 最新

  • duanqiao2006 2018-09-28 09:35
    关注

    You are almost done with the code. You just need to add the value here:

    if ($result->num_rows > 0) {
      // output data of each row
      while ($row = $result->fetch_assoc()) {
        //// Look at this line ////
        echo '<input type="button" value="' . $row["nomeCantiere"] . '" />';
      }
    } else {
      echo "0 results";
    }
    $conn->close();
    

    The above will generate a few rows of buttons. If that's what you would need. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效