【问题标题】:How load data from mySql in an HTML button?如何在 HTML 按钮中从 mySql 加载数据?
【发布时间】:2019-03-04 06:34:30
【问题描述】:

我想从我的 MySql 数据库中加载数据并将该数据库的一个字段放入按钮的文本中:

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

我是这类编程的初学者。

目前我已经设法生成一个简单的"echo"

如何自动生成带有value="nomeCantiere" 的按钮?

【问题讨论】:

    标签: php html mysql web-applications


    【解决方案1】:

    您几乎完成了代码。您只需在此处添加值:

    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();
    

    上面会生成几行按钮。如果那是你需要的。 :)

    【讨论】:

    • 哈哈...有用吗? @NarcosZTK_10
    • @NarcosZTK_10 很高兴你解决了这个问题。祝你今天过得愉快。将等待 5 分钟...:)
    【解决方案2】:

    你可以在php中回显html标签 IE。

    echo "<button>". $row["nomeCantiere"] ."</button>"
    

    echo "<input type='button' value='". $row["nomeCantiere"] ."'/>"
    

    【讨论】:

    • &lt;input type="button"&gt; 不是&lt;button&gt;
    • @RiggsFolly 不一样吗?或者这是因为 NarcosZTK_10 的代码是一个
    • 因为 OP 的代码使用了&lt;input type="button"&gt;
    • @RiggsFolly 是因为提交问题吗?我们可以在 中指定 type=button
    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多