按钮单击将数据从MySQL数据库加载到HTML文本框

时间:2012-06-30 10:05:07

标签: php mysql html

我有一个小表格来更新现有记录。

enter image description here

我正在使用PHP将服务ID 加载到下拉框中。当用户单击加载按钮时,它应该在下面的文本框中显示与该ID相关的其他详细信息。 这是我到目前为止的代码。

<html>
<head>
</head>
<body>

<?php
//Database initialization
require_once("db_handler.php");

$conn = iniCon();
$db = selectDB($conn);

$query = "SELECT * FROM taxi_services ORDER BY SID";
$result2 = mysql_query($query, $conn);

?>

<div id="upserv">
<b id="caption2">Update location</b>
<br/><br/>
    <form name="upServForm" action="<?php echo $PHP_SELF; ?>" method="post" >
        <?php
        $dropdown = "<select name='codes'>";
        while($row = mysql_fetch_assoc($result2)) 
        {
            $dropdown .= "\r\n<option value='{$row['SID']}'>{$row['SID']}</option>";
        }
        $dropdown .= "\r\n</select>";
    ?>
     Service ID  <?php echo $dropdown; ?> <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 $savedName; ?>" /></td>
          </tr>
          <tr>
            <td>Cost</td>
            <td><input type="text" name="upCost" style="text-align:right" value="<? echo $savedCost; ?>" /></td>
          </tr>
          <tr>
            <td>Active</td>
            <td><input type="checkbox" name="upActive" value="<? echo $savedActive; ?>" /></td>
          </tr>
        </table>
</div>
<br/>
<div id="buttons">
    <input type="reset" value="Clear" /> <input type="submit" value="Save" name="updatebtn" />
</div>
    </form>

<?php

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

    $query = "SELECT Name, Cost, Active FROM taxi_services WHERE SID = '$id' ";
    $result = mysql_query($query, $conn);
    $details = mysql_fetch_array($result);

    $savedName = $details["Name"];
    $savedCost = $details["Cost"];
    $savedActive = $details["Active"];
}

?>

</body>
</html>

查询执行得很好,但数据没有显示在文本框中。谁能告诉我这里缺少什么?

谢谢。

1 个答案:

答案 0 :(得分:3)

您的查询必须在输出之前:

还要注意id的类型转换(integer)以防止sql注入。

另请注意$PHP_SELF http://php.about.com/od/learnphp/qt/_SERVER_PHP.htm的安全问题 我将代码更改为$_SERVER['SCRIPT_NAME']

另请注意,如果可以,请使用register_globals并在配置中将其停用(使用$_SERVER['SCRIPT_NAME'] instead of $ SCRIPT_NAME`):http://www.php.net/manual/en/security.globals.php

如果您从一本书中学习php并且这是基于本书中的源代码,您应立即将其丢弃。

<?php

//Database initialization
require_once("db_handler.php");

$conn = iniCon();
$db = selectDB($conn);

$query = "SELECT * FROM taxi_services ORDER BY SID";
$result2 = mysql_query($query, $conn);

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

    $query = "SELECT Name, Cost, Active FROM taxi_services WHERE SID = '$id' ";
    $result = mysql_query($query, $conn);
    $details = mysql_fetch_array($result);

    $savedName = $details["Name"];
    $savedCost = $details["Cost"];
    $savedActive = $details["Active"];
}

?>

<html>
<head>
</head>
<body>

<div id="upserv">
<b id="caption2">Update location</b>
<br/><br/>
    <form name="upServForm" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" >
        <?php
        $dropdown = "<select name='codes'>";
        while($row = mysql_fetch_assoc($result2)) 
        {
            $dropdown .= "\r\n<option value='{$row['SID']}'>{$row['SID']}</option>";
        }
        $dropdown .= "\r\n</select>";
    ?>
     Service ID  <?php echo $dropdown; ?> <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 $savedName; ?>" /></td>
          </tr>
          <tr>
            <td>Cost</td>
            <td><input type="text" name="upCost" style="text-align:right" value="<? echo $savedCost; ?>" /></td>
          </tr>
          <tr>
            <td>Active</td>
            <td><input type="checkbox" name="upActive" value="<? echo $savedActive; ?>" /></td>
          </tr>
        </table>
</div>
<br/>
<div id="buttons">
    <input type="reset" value="Clear" /> <input type="submit" value="Save" name="updatebtn" />
</div>
    </form>

</body>
</html>