簡體   English   中英

使用PHP從Mysql數據庫填充數據的文本字段

[英]Populating Text Field with Data from Mysql Database with PHP

我正在嘗試使用來自MySQL數據庫的數據填充文本字段,但是我的代碼似乎無法正常工作。

任何幫助將不勝感激。

這是代碼:

<?php include ("../config.php");

// Connect to server and select database.
mysql_select_db($db, $con);

// get value of id that sent from address bar
$id = $_GET['ID'];


// Retrieve data from database
$sql="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($sql);

echo '<form name="Edit" method="post" action="update.php">

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Name:</strong></td>
<td align="center"><strong>Contact:</strong></td>
<td align="center"><strong>Division:</strong></td>
<td align="center"><strong>Rank:</strong></td>
</tr>';
            while ($row = mysql_fetch_assoc($result))
            {
            //we will echo these into the proper fields

        echo '<tr>';
        echo '<td align="center"><input name="Name" type="text" id="Name" value="'; echo $row['Name']; echo '"></td>';
        echo '<td align="center"><input name="Contact" type="text" id="Contact" value="'; echo $row["Contact"]; echo'" size="15"></td>';
        echo'<td>
        <select name="Divison">
        <option value="L4D2">L4D2</option>
        <option value="CSS">CSS</option>
        <option value="GMOD">GMOD</option>
        <option value="TF2">TF2</option>
        </select>
        </td>
        <td>
        <select name="Rank">
        <option value="Division Leader">Division Leader</option>
        <option value="Admin">Administrator</option>
        <option value="Member">Member</option>
        </select>
        </td>
        </tr>';


echo '<tr>';
        echo '<td align="center"><input type="submit" name="Submit" value="Submit"></td>
        </tr>
</table>
</td>
</form>';
}
echo '
</tr>
</table>
</body>
</html>';


// close connection
mysql_close();

?>

完整修復了我的答案:

<?php include ("../config.php");

// Connect to server and select database.
mysql_select_db($db, $con);

// get value of id that sent from address bar
$id = $_GET['ID'];

// Retrieve data from database
$sql="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($sql);

echo '<form name="Edit" method="post" action="update.php">
            <table width="400" border="0" cellspacing="1" cellpadding="0">
                <tr>
                    <td align="center"><strong>Name:</strong></td>
                    <td align="center"><strong>Contact:</strong></td>
                    <td align="center"><strong>Division:</strong></td>
                    <td align="center"><strong>Rank:</strong></td>
                </tr>';
while ($row = mysql_fetch_assoc($result)) {
    //we will echo these into the proper fields

    echo '<tr>
                <td align="center"><input name="Name" type="text" id="Name" value="'.$row['Name'].'"></td>
                <td align="center"><input name="Contact" type="text" id="Contact" value="'.$row["Contact"].'" size="15"></td>
                <td>
                    <select name="Divison">
                        <option value="L4D2">L4D2</option>
                        <option value="CSS">CSS</option>
                        <option value="GMOD">GMOD</option>
                        <option value="TF2">TF2</option>
                    </select>
                </td>
                <td>
                    <select name="Rank">
                        <option value="Division Leader">Division Leader</option>
                        <option value="Admin">Administrator</option>
                        <option value="Member">Member</option>
                    </select>
                </td>
            </tr>';
}
echo     '<tr>
                <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
            </tr>
           </table>
        </form>';

// close connection
mysql_close();
?>

現在用這個替換整個編碼並回復... ??

只是一個猜測,但是我認為您在db中的列名稱將全部為小寫。 但是在您的php中,您有$ row [“ Contact”]。 我認為應該是$ row [“ contact”]

這段代碼:

$sql="SELECT * FROM members WHERE id='$id'";

應該是這樣的

$sql="SELECT * FROM members WHERE id='{$id}'";

也使用

$result=mysql_query($sql) or die(mysql_error());

用於調試目的。

刪除mysql_close(),它應該可以工作

確保使用具有合法ID(例如example.com?ID=1)的查詢字符串。 如果要發布表單,並且表單的post方法是post,則應使用$ _POST ['ID']而不是$ _GET ['ID']。

根據之前的評論,您網址中的ID為小寫,將$ _GET ['ID']更改為$ _GET ['id']

我可以這樣做嗎? 讓有問題的其他人不必閱讀所有評論

這個網站的新手,謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM