NarcosZTK_10
NarcosZTK_10

Reputation: 147

How load data from mySql in an HTML button?

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

Upvotes: 2

Views: 787

Answers (2)

Sarath Mohan
Sarath Mohan

Reputation: 167

You can echo html tags in php ie.

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

or

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

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

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. :)

Upvotes: 2

Related Questions