(问题非常详细,因为太久没有阅读:“我的猜测是我使用的 MYSQL_FETCH_ARRAY 错误。”)
你好!以下代码的目的是在数据库中进行基本搜索。数据通过表单传递。我使用的教程是由“Frost of Slunked.com”编写的,它是一个基本的注册/登录 php MySQL 教程,效果很好。我设法编写了一个 woking table-updater 函数和 form-submit 以向选定的对象添加新数据(这样可以按预期工作。
config.php - 连接到 MySQL 服务器,选择数据库,启动会话,需要 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 - 具有多种功能(登录、createRide、注册等)。大多数函数的目的是从提交的 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 表单提交的测试数据: From: TEST01 To: 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 when
<= '5000' (sql2 变量
(行变量
在此之后,我在 phpMyAdmin SQL 命令行中插入了 SQL 查询,并得到了我正在搜索的数据。
根据我自己的知识和在 google、php.net 和 w3chools.com 上的各种搜索,我多次尝试找出问题所在。我的猜测是我使用错误的 MYSQL_FETCH_ARRAY。