1

I am still learning, can anyone help me, What wrong in my code? I need to load when you click on the Load button program will search the database ID selected in the dropdown, and them bring the name .. etc and show it on textbox. Sorry, for my English.

<?php

        $servername = "localhost";
        $username = "estgv15592";
        $password = "estgv155922016";
        $dbname = "estgv15592";
        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 


           if(isset($_POST["loadbtn"]))
        {

            $id = (integer) $_POST["id"];

            $query = "SELECT NOME, MORADA, PRECO FROM FICHA_DE_OBRA WHERE ID_FICHAOBRA = '$id' ";
            $result = mysqli_query($conn, $query);
            $details = mysql_fetch_array($result);

            $nome = $details["NOME"];
            $morada = $details["MORADA"];
            $preco = $details["PRECO"];
        }

        $sql = "SELECT * FROM FICHA_DE_OBRA";

        $result = mysqli_query($conn, $sql);

         echo '<form id="form" method="post">';
            echo "<select name ='id'>";
            echo "<option value=''>Selecione Número ficha Obra</option>";

            while($row = mysqli_fetch_array($result))

              {
              echo "<option value='" . $row['ID_FICHAOBRA'] . "'>" . $row['ID_FICHAOBRA'] . "</option>";
              }
              echo "</select>";

            $conn->close();
            ?> 


          <input type="submit" value="Load" name="loadbtn">
          <table width="300" border="0">
          <tr>
          <td>Name</td>
          <td><input type="text" name="upName" style="text-align:right" value="<?php echo $nome;?>"/></td>
        </tr>
         <tr>
          <td>Cost</td>
          <td><input type="text" name="upCost" style="text-align:right" value="<?php echo $morada;?>" /></td>
        </tr>
        <tr>
          <td>Active</td>
          <td><input type="text" name="upActive" style="text-align:right" value="<?php echo $preco;?>" /></td>
        </tr>
    </table>
</div>
<br/>

</form>
2
  • 2
    Welcome to StackOverflow. Please clarify your question: the last phrase is very long and difficult to understand. Commented Jun 19, 2016 at 18:37
  • I'm not clear on what the problem is. Commented Jun 19, 2016 at 18:59

2 Answers 2

1

You are not using proper php tag: (e.g. <?php echo $preco;?>):

<tr>
  <td>Name</td>
  <td><input type="text" name="upName" style="text-align:right" value="<?php echo $nome; ?>"/></td>
</tr>
<tr>
  <td>Cost</td>
  <td><input type="text" name="upCost" style="text-align:right" value="<?php echo $morada; ?>" /></td>
</tr>
<tr>
  <td>Active</td>
  <td><input type="text" name="upActive" style="text-align:right" value="<?php echo $preco; ?>" /></td>
</tr>

Use mysqli_query and mysqli_fetch_array function and note that first argument in mysqli_query should be the connection object where you made the mistake:

$result = mysqli_query($conn, $query);    // first PHP block
$result = mysqli_query($conn, $sql);      // second PHP block

$details = mysqli_fetch_array($result);   // first PHP block
$row = mysqli_fetch_array($result)        // second PHP block

And move below lines to the top of your first PHP block, or $conn would be undefined in your first PHP block:

$servername = "localhost";
$username = "estgv15592";
$password = "your_password";
$dbname = "estgv15592";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but I made the changes and does not work.
0

The problem is came from your connection to database you use mysqli in connection but you when call queries you use mysql.

This is the code

<?php
    $servername = "localhost";
    $username = "estgv15592";
    $password = "********";
    $dbname = "estgv15592";
    $conn = mysql_connect($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 

    if(isset($_POST["loadbtn"]))
    {  
        $id = intval($_POST["id"]);

        $query = "SELECT NOME, MORADA, PRECO FROM FICHA_DE_OBRA WHERE  ID_FICHAOBRA = '$id' ";
        $result = mysql_query($query, $conn);
        $details = mysql_fetch_array($result);

        $nome = $details["NOME"];
        $morada = $details["MORADA"];
        $preco = $details["PRECO"];
     }
?>


<?php
    $sql = "SELECT * FROM FICHA_DE_OBRA";
    $result = $conn->query($sql);


    echo '<form id="form" method="post">';
    echo "<select name ='id'>";
    echo "<option value=''>Selecione Número ficha Obra</option>";

    while($row = mysqli_fetch_array($result))
    {
        echo "<option value='" . $row['ID_FICHAOBRA'] . "'>" . $row['ID_FICHAOBRA'] . "</option>";
    }
    echo "</select>";


    $conn->close();
?> 

<input type="submit" value="Load" name="loadbtn">
    <table width="300" border="0">
      <tr>
        <td>Name</td>
        <td><input type="text" name="upName" style="text-align:right" value="<? echo $nome; ?>" /></td>
      </tr>
      <tr>
        <td>Cost</td>
        <td><input type="text" name="upCost" style="text-align:right" value="<? echo $morada; ?>" /></td>
      </tr>
      <tr>
        <td>Active</td>
        <td><input type="text" name="upActive" style="text-align:right" value="<? echo $preco; ?>" /></td>
      </tr>
    </table>
   </div>
   <br/>
  </form>

  </body>
  </html>

</div>

this method you use to get data not secure. I advise you to learn pdo or prepared statement with mysqli

1 Comment

I had already done it, that's not the problem. See this s31.postimg.org/m96zazuyj/Sem_t_tulo.jpg

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.