Search.php无效,无法显示任何结果

时间:2012-04-25 23:07:16

标签: php mysql forms search input

我正在试图弄清楚如何让我的search.php脚本与mySQL一起工作。我无法获得要显示的信息。不确定问题出在哪里。

第1页:

<form action="search_result.php" method="GET">
    <input type="text" name="reg" />
    <input type="submit" value="Search" />
</form>

第2页:

<?php
$host="localhost";
$username="XXXXXXXXXXX";
$password="XXXXXXXXXXX";
$db_name="XXXXXXXXXXXX";
$tbl_name="reg_add";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$record = $_POST['record']; // if coming from e.g. a form
$result=mysql_query(" SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");


$row = mysql_fetch_assoc($result);
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$reg = $row['reg'];
?>

<input  name="reg" value="<? echo "$record" ?>">

<input  name="first_name" value="<? echo "$first_name" ?>">

<input  name="last_name" value="<? echo "$last_name" ?>">

3 个答案:

答案 0 :(得分:0)

mysql_connect("$host", "$username", "$password") 

不是你的问题,但不需要围绕变量引用:

mysql_connect($host, $username, $password);
mysql_select_db($db_name);

您应该在某处设置变量$record

$record = $_POST['record']; // if coming from e.g. a form
$result=mysql_query(" SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");

这是错的:

$first_name=mysql_result($result,"first_name");
$last_name=mysql_result($result,"last_name");
$reg=mysql_result($result,"reg"); 

应该是:

$row = mysql_fetch_assoc($result);
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$reg = $row['reg'];

另外:你不应再使用mysql_* functions了。使用mysqli_*或PDO。

请记住,如果某些内容无效,您可以检查mysql_error()以查看执行查询时出现的任何错误。

答案 1 :(得分:0)

mysql_result()的语法错误。根据{{​​3}},它应该是

  

string mysql_result(resource $ result,int $ row [,mixed $ field = 0])

所以使用它的正确方法就像

mysql_result($result, 1, "first_name");

答案 2 :(得分:0)

你的表格是方法GET,你在PHP中使用它:

$record = $_POST['record']; // if coming from e.g. a form

如果您的表单采用GET方法,您将如何获得POST ['记录']?

我猜您应该将表单更改为:

method="POST" 

或将php中的$记录更改为:

$record = $_GET['record'];

试试这个版本:

你形成:

    <form action="search_result.php" method="POST">
    <input type="text" name="reg" id="reg" />
    <input type="submit" name="Submit" id="Submit" value="Search" />
</form>

search_result.php:

<?php

$host       ="localhost";
$username   ="XXXXXXXXXXX";
$password   ="XXXXXXXXXXX";
$db_name    ="XXXXXXXXXXXX";
$tbl_name   ="reg_add";

/* Connect to MySQL database */
mysql_connect("$host", "$username", "$password") or die("Error connecting to database");
mysql_select_db("$db_name")or die("Error selecting database");

$error = '';

if (isset($_POST['Submit'])) {


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

        $record = $_POST['reg']; // if coming from e.g. a form

        $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);

            $first_name = $row['first_name'];
            $last_name = $row['last_name'];
            $reg = $row['reg'];

        } else {

            $error = 'No result have been found!';

        }

    } else {

        $error = 'You have not entered the search field, <a href="javascript:history.back(1)">Go back</a>.';

    }
}

if (!empty($error)) { echo $error; } 
?>


<input  name="reg" value="<? echo $record; ?>">

<input  name="first_name" value="<? echo $first_name; ?>">

<input  name="last_name" value="<? echo $last_name; ?>">