php 从 mySQL 数据库中检索数据到表单中

标签 php mysql html

我有一个注册表单,我喜欢使用 mySQL 查询来检索表单输入字段中的数据。

这是超长表格的一部分:

<form class="form-horizontal" id="registration" method='post' action='ConnectDB.php' onsubmit='return ValidateForm(this);'>
    <fieldset>
        <h1 style="margin-top: px;float:right; color:#62A1D5"><br><b>registration</b></h1>
        <table id="TBackGround">
            <td style="padding-left:10px; padding-right:10px;">
                <p id="pd"  style="margin-top:5px; margin-right:2px; font-size:16px; text-decoration:underline"><b><br>details:</b><p> 
                <p style="line-height: 1.3em" class="details">
                <div class="control-group"> 
                    <label class="control-label">ID </label>
                    <input type="text"name="studentID"  align= "right" class="textbox" ><span id="errorID"></span> <br/>  
                </div>
                <div class="control-group"> 
                    <label class="control-label" >First name</label>
                    <input type="text" name="Fname"  class="textbox" ><span id="errorFirstName"> <br/>
                </div>
</form>

如何将检索到的数据设置为加载到表单的输入字段中? 我有一个检索记录 ID 的查询,但我不知道如何将整个查询结果设置到字段上。

我的 php 查询:

<?php 

if(isset($_POST['submit']))
{
$query = $_POST['query']; 
$min_length = 1;
if(strlen($query) >= $min_length)
{ 
$query = htmlspecialchars($query); 
$query = mysql_real_escape_string($query); 
echo "<table border='0' width='300' align='center' cellpadding='1' cellspacing='1'>";
echo "<tr align='center' bgcolor='#002C40'> 
?>
<td height='35px' width='150px'>id</td> <td>first name</td>

</tr>"; 
$raw_results = 

mysql_query("SELECT * FROM student WHERE (`idStudent` LIKE '%".$query."%') OR (`FirstName` LIKE '%".$query."%')"); 
if(mysql_num_rows($raw_results) > 0)
{
while($results = mysql_fetch_array($raw_results))
{ 

echo "<tr align='center' bgcolor='#0f7ea3'>

 <td height='25px'> "
 .$results['idStudent']."</td> <td>".$results['FirstName']."</td>






</tr>" ;
}

}
else{ 
echo "<tr align='center' bgcolor='#6C0000'>

<td colspan='2' height='25px'>No results</td><tr>"; 
echo "</table>"; 
} 
}
else{ 
echo "Minimum length is ".$min_length;
}} 

?>

最佳答案

您需要从 php 文件输出表单布局。 这是可以帮助您的小示例。

文件register.php

if (!isset($_POST['submit'])) {
    mysql_connect();
    $res = mysql_query("SELECT * FROM users WHERE user_id=1");
    $user = array_map('htmlspecialchars', mysql_fetch_assoc($res));

    echo <<<CUT
<form action="register.php" method="post" >
    <lable>Name:</label> <input type="text" name="name" value="{$user['name']}" />
    <lable>Country:</label> <input type="text" name="name" value="{$user['country']}" />
    <input type="submit" name="submit" value="Submit" />
</form>
CUT;
} else {
    //handle submited data here
}

关于php 从 mySQL 数据库中检索数据到表单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112029/

相关文章:

mysql - UNION 在 ORDER BY 和 LIMIT 之后

html - 如何在多个文件中使用基本的html代码结构

php - Laravel Eloquent 级联删除

php - 将端口 80 更改为 8085 后,wamp 服务器 localhost 无法正常工作

javascript - 在 Laravel 4 中使用 jquery Ajax

php - 仅从连接表中获取指定列

javascript for 循环只返回最后一个元素

java - 未知错误 : MySQLTransactionRollbackException 1213

java - 使用 java.util.regex 的 HTML 正则表达式

html - 是否有使用 languages 为非空格删除空格的 CSS 解决方案?