doutangtan6386 2016-06-19 18:29
浏览 107
已采纳

将数据从MySQL数据库加载到HTML文本框

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条回答

  • dsp1836 2016-06-19 19:39
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决