将PHP合并到HTML表单中

时间:2011-10-01 15:28:48

标签: php mysql html

我想知道是否有人可以帮助我。

我正在尝试整合功能,允许管理员使用电子邮件地址作为搜索条件在mySQL数据库中搜索用户详细信息。搜索完成后,我希望我的表单中的“第一个”和“姓氏”字段可以在检索到的记录中完成。

我认为搜索在表单刷新时有效,但它不会检索任何可见信息。

我只是想知道是否有可能请有人可以查看下面的代码,让我知道我哪里出错了。

非常感谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <?php
        require("phpfile.php");  

        // Opens a connection to a MySQL server  

        $connection=mysql_connect ("hostname", $username, $password);  
        if (!$connection) { die('Not connected : ' . mysql_error());}  

        // Set the active MySQL database  

        $db_selected = mysql_select_db($database, $connection);  
        if (!$db_selected) {  
        die ('Can\'t use db : ' . mysql_error());  
        }  


        $email = $_POST['email'];
        $result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%$email%'"); 

        ?> 
        <title>Map!</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="js/gen_validatorv4.js" type="text/javascript"></script>
    </head>
    <h1>&nbsp;</h1>
    <form name="userpasswordreset" id="userpasswordreset" method="post">
        <div class="container">
            <p align="justify">&nbsp;</p>
            <div class="title1">
                <h2>User Details </h2>
            </div>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="26%" height="25"><strong>Email Address</strong></td>
                    <td width="4%">&nbsp;</td>
                    <td width="70%"><input name="email" type="email" id="email" size="50" /></td>
                </tr>
                <tr>
                    <td height="25"><strong>Confirm Email Address </strong></td>
                    <td>&nbsp;</td>
                    <td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
                </tr>

                <tr>
                    <td height="25"><label>
                        <input type="submit" name="Submit" value="Search" />
                        </label></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td height="25">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td height="25"><strong>First Name </strong></td>
                    <td>&nbsp;</td>
                    <td><input name="fname" type="text" id="fname" size="30" value="<?php echo $row['forename']; ?>" /> </td>
                </tr>
                <tr>
                    <td height="25"><strong>Last Name </strong></td>
                    <td>&nbsp;</td>
                    <td><input name="lname" type="text" id="lname" size="30"  value="<?php echo $row['surname']; ?>" /></td>
                </tr>
                <tr>
                    <td height="25">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>

                <tr>
                    <td height="25"><strong>Password</strong></td>
                    <td>&nbsp;</td>
                    <td><input name="pass" type="password" id="pass" size="30" /></td>
                </tr>
                <tr>
                    <td height="25"><strong>Confirm Password </strong></td>
                    <td>&nbsp;</td>
                    <td><input name="conf_pass" type="password" id="conf_pass" size="30" /></td>
                </tr>
                <tr>
                    <td height="25">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td height="25"><strong>Password Hint </strong></td>
                    <td>&nbsp;</td>
                    <td><input name="hint" type="text" id="hint" size="30" /></td>
                </tr>
                <tr>
                    <td height="25">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td height="25">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
            </table>

        </div>
    </form>
    <script language="JavaScript" type="text/javascript">
        // Code for validating the form
        var frmvalidator = new Validator("userpasswordreset");
        frmvalidator.addValidation("email","req","Please provide your email address"); 
        frmvalidator.addValidation("email","email","Please enter a valid email address");
        frmvalidator.addValidation("conf_email","eqelmnt=email", "The confirmed email address is not the same as the email address"); 
    </script>

</div>
    </body>
    </html>

更新代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <?php
            require("phpfile.php");  

            // Opens a connection to a MySQL server  

            $connection=mysql_connect ("hostname", $username, $password);  
            if (!$connection) { die('Not connected : ' . mysql_error());}  

            // Set the active MySQL database  

            $db_selected = mysql_select_db($database, $connection);  
            if (!$db_selected) {  
            die ('Can\'t use db : ' . mysql_error());  
            }  
            $email  = mysql_real_escape_string($_POST['email']); // make the value safe for in the query 
            $result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%$email%'");  
            $rows   = array(); 

            while ($row = mysql_fetch_assoc($result)) { 
            $rows[] = $row; 
            } 

            // $rows is now a multi dimensional array with values found by the query 

            ?> 
<title>Map</title>
<script src="js/gen_validatorv4.js" type="text/javascript"></script>
<h1>&nbsp;</h1>
<form name="userpasswordreset" id="userpasswordreset" method="post">
    <p align="justify">&nbsp;</p>
    <div class="title1">
        <h2>Your Details </h2>
    </div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="26%" height="25"><strong>Email Address </strong></td>
            <td width="4%">&nbsp;</td>
            <td width="70%"><input name="email" type="email" id="email" size="50" /></td>
        </tr>
        <tr>
            <td height="25"><strong> Confirm Email Address </strong></td>
            <td>&nbsp;</td>
            <td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
        </tr>

        <tr>
            <td height="25"><label>
                <input type="submit" name="Submit" value="Search" />
                </label></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="25"><strong>First Name </strong></td>
            <td>&nbsp;</td>
            <td><input name="fname" type="text" id="fname" size="30" /></td>
        </tr>
        <tr>
            <td height="25"><strong>Last Name</strong></td>
            <td>&nbsp;</td>
            <td><input name="lname" type="text" id="lname" size="30" /></td>
        </tr>
        <tr>
            <td height="25">&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="25">&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="25"><strong>Password</strong></td>
            <td>&nbsp;</td>
            <td><input name="pass" type="password" id="pass" size="30" /></td>
        </tr>
        <tr>
            <td height="25"><strong>Confirm Password </strong></td>
            <td>&nbsp;</td>
            <td><input name="conf_pass" type="password" id="conf_pass" size="30" /></td>
        </tr>
        <tr>
            <td height="25">&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="25"><strong>Password Hint </strong></td>
            <td>&nbsp;</td>
            <td><input name="hint" type="text" id="hint" size="30" /></td>
        </tr>
        <tr>
            <td height="25">&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td height="25">&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>

</form>
<script language="JavaScript" type="text/javascript">
    // Code for validating the form
    // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
    // for details
    var frmvalidator = new Validator("userpasswordreset");
    frmvalidator.addValidation("email","req","Please provide your email address"); 
    frmvalidator.addValidation("email","email","Please enter a valid email address");
    frmvalidator.addValidation("conf_email","eqelmnt=email", "The confirmed email address is not the same as the email address"); 
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

您是否错过了针对查询实际运行mysql_fetch_assoc函数的步骤?仅将mysql_query的结果分配给变量$result是不够的!

在致电mysql_query后添加以下内容:

if($result)
{
    $row = mysql_fetch_assoc($result));
}

修改: mysql_fetch_assoc PHP documentation

答案 1 :(得分:1)

要获得查询产生的所有行,您需要使用mysql fetch函数(有几个)。

preffered方法绝对是mysql_fetch_assoc

代码的相关部分如下所示:

$email  = mysql_real_escape_string($_POST['email']); // make the value safe for in the query
$result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%$email%'"); 
$rows   = array();

while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}

// $rows is now a multi dimensional array with values found by the query

答案 2 :(得分:-2)

简单地回顾一下你的代码,我认为你没有正确地在你的sql查询中传递电子邮件的价值。尝试使用它。

$result = mysql_query("SELECT * FROM userdetails WHERE emailaddress like '%" . $email . "%'");