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

问题描述 投票:2回答:2

我想从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
2个回答
2
投票

你差不多完成了代码。你只需要在这里添加值:

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

以上将生成几行按钮。如果那就是你需要的。 :)


0
投票

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

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

要么

echo "<input type='button' value='". $row["nomeCantiere"] ."'/>"
© www.soinside.com 2019 - 2024. All rights reserved.