php - 数据库查询php MySQL - 没有显示搜索结果

标签 php mysql forms form-submit

(问题非常详细,太久没读:“我猜是我错误地使用了 MYSQL_FETCH_ARRAY。”)

您好!以下代码的目的是在数据库中进行基本搜索。数据通过表单传递。我使用的教程是由“Frost of Slunked.com”编写的,它是一个基本的注册/登录 php MySQL 教程,效果很好。我设法编写了一个有效的表更新程序函数和表单提交以将新数据添加到所选内容(因此按预期工作。

config.php - 连接到 MySQL 服务器,选择数据库,启动 session ,需要 functions.php(包括作者评论)

<?php
/*****************************
    File: includes/config.php
    Written by: Frost of Slunked.com
    Tutorial: User Registration and Login System
******************************/
// start the session before any output.
session_start();

// Set the folder for our includes
$sFolder = ''; 

/***************
    Database Connection 
        You will need to change the user (user) 
        and password (password) to what your database information uses. 
        Same with the database name if you used something else.
****************/
mysql_connect('localhost', 'myusername', 'mypassword') or trigger_error("Unable to connect to the database: " . mysql_error());
mysql_select_db('tormex') or trigger_error("Unable to switch to the database: " . mysql_error());

/***************
    password salts are used to ensure a secure password
    hash and make your passwords much harder to be broken into
    Change these to be whatever you want, just try and limit them to
    10-20 characters each to avoid collisions. 
****************/
define('SALT1', '24859f@#$#@$');
define('SALT2', '^&@#_-=+Afda$#%');

// require the function file
require_once 'functions.php';

// default the error variable to empty.
$_SESSION['error'] = "";

// declare $sOutput so we do not have to do this on each page.
$sOutput="";
?>

functions.php - 具有多种功能(登录、创建乘车、注册等)。大多数函数的目的是从提交的 HTML 表单中获取值,然后维护所需的操作——我只会提到我的 searchRide 函数(我猜它有错误或至少,必须用它做点什么)和 createRide功能正常。

<?php ...

unction searchRide($pWhen_min, $pWhen_max, $pFrom, $pTo){
    if (!empty($pWhen_min) && !empty($pWhen_max) && !empty($pFrom) && !empty($pTo)) {
        global $sql2, $query2;
        $sql2 = "SELECT * FROM ride WHERE `from` ='$pFrom' AND `to` = '$pTo' AND `when` >= '$pWhen_min' AND `when` <= '$pWhen_max' ";
        $query2 = mysql_query($sql2) or trigger_error("Query Failed: " . mysql_error());
    }
}
function createRide($pFrom, $pTo, $pWhen, $pSeats, $pPrice, $pCar){
    if (!empty($pFrom) && !empty($pTo) && !empty($pWhen) && !empty($pSeats) && !empty($pPrice) && !empty($pCar)){
        $sql = "SELECT id FROM users WHERE username= '" . $username . "' LIMIT 1";
        $result = mysql_query($sql);
        if(!$result) {
            trigger_error("ELKURTAD " . mysql_error());
        }
        $row = mysql_fetch_array($result);
        $sql = "INSERT INTO ride (`from`, `to`, `when`, `seats`, `price`, `car`, `u_id`)
        VALUES ('" . $pFrom . "', '" . $pTo . "', '" . $pWhen . "', 
        '" . $pSeats . "', '" . $pPrice . "', '" . $pCar . "', '" . $result . "');";


        $query = mysql_query($sql) or trigger_error("Query Failed: " . mysql_error());

        if ($query) {
            return TRUE;
        } 
    }
    return FALSE;
}

...?>

searchRide.php - 检查专用于获取搜索过滤器值的变量是否有任何值; (在 else 语句中)如果没有值,则不会提交表单并显示 searchRide 表单,并且在结果传递给 searchRide.php ( $_SERVER['PHP_SELF'] ) 的变量之后

<?php
require_once 'config.php';

$sOutput .= '<div id="searchRide-body">';

if (isset($_GET['action'])) {
    switch (strtolower($_GET['action'])) {
        case 'searchride':
        if (isset($_POST['when_min']) && isset($_POST['when_max']) && isset($_POST['from']) && isset($_POST['to'])) {
            if (searchRide($_POST['when_min'], $_POST['when_max'], $_POST['from'], $_POST['to'])) {
                while($row = mysql_fetch_array($query2)){
                    $sOutput .= "' ID: '" .$row['id'] . "' <br />
                    When: '" . $row['when'] . "' <br />
                    From: '" . $row['from'] . "' <br />
                    To: '" . $row['to'] . "' <br />
                    Seats left: '" . $row['seats'];

                }
            }
        }
    }
}else{
    if (isset($_SESSION['error'])) {
        $sError = '<span id="error">' . $_SESSION['error'] . '</span><br />';
    }

    $sOutput .= '<h2>Search for rides</h2>
        ' . $sError . '
        <form name="searchride" method="post" action="' . $_SERVER['PHP_SELF'] . '?action=searchride">
            From: <input type="text" name="from" value=* /><br />
            To: <input type="text" name="to" value=* />
            When_min: <input type="text" name="when_min" value=* />
            When_max: <input type="text" name="when_max" value=* />
            <br /><br />
            <input type="submit" name="submit" value="Search" />
        </form>
        <br />
        <h4>Would you like to <a href="index.php">Go back?</a></h4>';
}   
echo $sOutput . "<br />";

echo "TEST string" . "<br />";
echo $query2 . " query2<br /> ";
echo $sql2 . " sql2<br />";
echo $row . "<br />";
?>

在这段代码的最后你可以看到一些打印的变量,这些变量用于在提交searRide表单后检查它们的值。 我使用以下数据更新了我的数据库,并使用 phpMyAdmin 检查了确切的值,以便我可以使用现有数据测试搜索:

来自:TEST01 至:TEST02 时间:500 座位数:5 价格:7 汽车:沃尔沃

用searchRide表单提交的测试数据: 来自:TEST01 至:Test02 When_min: 1 Whn_max:3000

在 searchRide 表单上按下 Search 按钮后,这些是以下结果(浏览器显示的内容):

(s输出变量

测试写入文本

资源 id #5(query2 变量

SELECT * FROM ride WHERE from ='TEST01' AND to = 'TEST02' AND when >= '1' AND <= '5000' (sql2 变量

(行变量

在此之后,我在 phpMyAdmin SQL 命令行中插入了 SQL 查询并得到了我正在搜索的数据。

根据我自己的知识和在 google、php.net 和 w3chools.com 上的各种搜索,我多次尝试找出可能的问题。 我的猜测是我错误地使用了 MYSQL_FETCH_ARRAY。

最佳答案

以下条件不成立

if (searchRide($_POST['when_min'], $_POST['when_max'], $_POST['from'], $_POST['to'])) {

因为您没有从 searchRide 函数返回任何值,您需要返回 true 才能进入条件。

关于php - 数据库查询php MySQL - 没有显示搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13757416/

相关文章:

PHP 和 Mysqli 以及存储过程

java - 如何制作Web App在线编译运行Java/C/PHP代码?

php - 意外的右花括号 } 错误

尝试使用用户输入删除时出现 Python 连接器错误

javascript - 从网页运行 Google App 脚本

php - 生日前剩余的天数 - laravel - carbon

php - 将 GD 图像转换回二进制数据

mysql - 如何对临时表使用 IN 运算符

Javascript/jQuery 在计时器后点击

php - 表单验证 - jQuery 验证是否足够以及如何巧妙地验证 PHP 中的数据?