2

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 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ha ha... Is that helpful? @NarcosZTK_10
@NarcosZTK_10 Glad that you got it solved. Have a nice day. Will wait for 5 mins... :)
0

You can echo html tags in php ie.

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

or

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

4 Comments

<input type="button"> not a <button>!
@RiggsFolly isn't it the same ? Or this is because NarcosZTK_10's code was an <input type="button"> ?
Because the OP's code uses an <input type="button">
@RiggsFolly is it because of the submit issue? we can specify type=button in <button> right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.