0

我试图弄清楚如何使我的 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" ?>">
4

3 回答 3

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_*函数了。使用其中一个mysqli_*或 PDO。

请记住,如果某些东西不起作用,您可以检查查询是否mysql_error()有任何错误。

于 2012-04-25T23:19:53.103 回答
0

mysql_result() 的语法错误。按照说明书,应该是

字符串 mysql_result ( 资源 $result , int $row [, mixed $field = 0 ] )

所以正确的使用方法是

mysql_result($result, 1, "first_name");
于 2012-04-25T23:30:13.310 回答
0

您的表单是 GET 方法,在您的 PHP 中使用:

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

如果您的表单具有 GET 方法,您将如何获得 POST['record']?

我想您应该或将您的表格更改为:

method="POST" 

或将您的 $record 在 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>

搜索结果.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; ?>">
于 2012-04-26T08:30:33.977 回答