通过php构建搜索结果页面

时间:2012-04-27 14:10:33

标签: php mysql search post

我需要构建HTML页面来显示和组织我的结果。我在哪里放置HTML?我试过了,我得到了错误。它是否进入PHP脚本?在php脚本之后?之前?困惑。

<?php

if (isset($_POST['Submit'])) {
if (!empty($_POST['reg'])) {

    $record = $_POST['reg'];

    $query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
    $result = mysql_num_rows($query);

    if ($result != 0) {

        $row = mysql_fetch_array($query);

        $connect_date = $row['connect_date'];
        $reg = $row['reg'];
        $first_name = $row['first_name'];
        $last_name = $row['last_name'];
        $nickname = $row['nickname'];
        $gender = $row['gender'];
        $birthday = $row['birthday'];
        $home_state = $row['home_state'];
        $national = $row['national'];
        $location = $row['location'];


    } else {
        header("Location: search_error1.php");
        exit;
    }

} else {
    header("Location: search_error2.php");
    exit;
}
}
?>

1 个答案:

答案 0 :(得分:2)

PHP可嵌入到HTML中,因此您可以在php文件中编写html标记。您只需将所有PHP代码放在<?php ?>

<?php
    //php code
?>
<html>
<!-- ... -->
</html>
<?php /* php again */ ?>

PHP解释器只是忽略在php标记(<?php ?>)之外写的所有文本,并将其直接写入stdout。

所以对你来说就是这样:

<?php

// ...

if ($result != 0) {
    while($row = mysql_fetch_array($query)) {
        $connect_date = $row['connect_date'];
        $reg = $row['reg'];
        $first_name = $row['first_name'];
        $last_name = $row['last_name'];
        $nickname = $row['nickname'];
        $gender = $row['gender'];
        $birthday = $row['birthday'];
        $home_state = $row['home_state'];
        $national = $row['national'];
        $location = $row['location'];
?>
  First name: <?php echo $first_name ?><br/>
  Last name: <?php echo $last_name ?><br/>
  ...
<?php
    }
}

// ...

?>