晚上好......
这里的php问题的典型新手。这是针对个人项目,而不是寻找解决方案的大学生,需要从我的网站呈现数据库。下面的代码存放在Wordpress网站中,您将使用WP插件使用的短代码代替。
我已经与我的托管公司验证了我的数据库访问信息,我知道这不是问题..
我遇到的问题是我收到了消息; "无法检索您的请求"。
DEFINE变量已经用Laughing Squid验证,应该是正确的。您将看到一系列echo函数,以查看脚本运行时问题的确切位置,并且我已经完成了#34;连接测试3"和"连接测试4"永远不会渲染。
如果我在页面中输入搜索条件,我会在此处与您会面,并在此处发送消息"。
非常感谢任何帮助......
//脚本跟随
<html>
<head>
<style/type = "text/css">
table {
background-color: #FCF;
}
th {
width: 150px;
text-align: left;
}
</style>
</head>
<body>
<h1>Race Search</h1>
<form method = "post" action="search.php">
<input type = "hidden" name = "submitted" value = "true"/>
<label> Search Race:
<select name = "race">
<option value = "date">Race Date</option>
<option value = "city">Location</option>
<option value = "race">Race</option>
</select>
</label>
<label>Search Criteria: <input type="text" name="criteria" /></label>
</form>
//short code from Wordpress plug-in
[allowphp useadvancedfilter]
DEFINE ('DB_USER', 'Removed');
DEFINE ('DB_PSWD', 'Removed');
DEFINE ('DB_HOST', 'Removed');
DEFINE ('DB_NAME', 'Removed');
$dbcon = mysql_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);
//connection test 1
echo "connection1";
$race = $_POST['race'];
$criteria = $_POST['criteria'];
//connection test 2
echo "connection2";
$query = "SELECT * FROM Races WHERE $category = '$criteria'";
//connection test 3
echo "connection3";
$result = mysqli_query($dbcon, $query) or die('Cannot retrieve your request');
//connection test 4, never gets here....
echo "connection4";
$num_rows = mysqli_num_rows($result);
echo "$num_rows results found";
echo "<table>";
echo "<tr><th>Race Date</th><th>Location</th><th>Event</th> </tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['Date'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Race Name'];
echo "</td></tr>";
}
echo "</table>";
//short code from Wordpress plug-in
[/allowphp useadvancedfilter]
</body>
</html>
答案 0 :(得分:0)
您正在使用mysq_connect和mysqli_query。您应该使用mysqli_connect,它们不可互换。
如果您可以更新您的选择选项值以匹配您的列名称,则可以使用以下内容:
$where = '';
if (isset($_POST['race'])) {
$race = $POST['race'];
$where = ' WHERE `'.$race."` = '".mysql_real_escape_string($_POST['criteria'])."'";
}
$query = 'SELECT * FROM `Races`'.$where;
如果不是仍然可以,但您需要使用switch语句或其他东西将$ race设置为正确的值以匹配您的列名。无论如何,您可能希望确保$ _POST [&#39; race&#39;]是预期值。