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

时间:2016-06-19 18:29:43

标签: php mysql html5 forms textbox

我还在学习,任何人都可以帮助我,我的代码有什么问题? 我需要加载,当你点击加载按钮程序将搜索在下拉列表中选择的数据库ID,他们带来名称..等,并在文本框上显示它。 对不起,我的英文。

<?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 个答案:

答案 0 :(得分:0)

问题来自您使用 mysqli 连接数据库,但是当您使用 mysql 调用查询时。

这是代码

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

此方法用于获取数据不安全。我建议你用mysqli

学习pdo或准备好的声明

答案 1 :(得分:0)

您没有使用正确的php标记:(例如<?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>

使用mysqli_querymysqli_fetch_array函数并注意mysqli_query中的第一个参数应该是您犯错误的连接对象:

$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

将下面的行移到第一个PHP块的顶部,或者在第一个PHP块中未定义$conn

$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);
}