(问题非常详细,太长时间没有阅读:“我的猜测是我正在使用错误的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 - 具有多个功能(login,createRide,Register等)。大多数函数的目的是从提交的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表单上的搜索按钮后,这些是以下结果(浏览器显示的内容):
(s输出变量
TEST WRITE TEXT
资源ID#5(query2变量
SELECT * FROM ride WHERE from
='TEST01'和to
='TEST02'和when
&gt; ='1'和when
&lt; ='5000 '(sql2变量
(行变量
在此之后,我在phpMyAdmin SQL命令行中插入了SQL查询,并生成了我正在搜索的数据。
在google,php.net和w3chools.com上用我自己的知识和各种搜索来尝试多次弄清楚可能出现什么问题。 我的猜测是我使用MYSQL_FETCH_ARRAY错误。
答案 0 :(得分:1)
以下条件不起作用
if (searchRide($_POST['when_min'], $_POST['when_max'], $_POST['from'], $_POST['to'])) {
因为你没有从searchRide函数返回任何值,你需要返回true以进入条件。