作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的网站有一个搜索表单,运行得很好,但后来我更改了整个登录系统,这导致我的网站出现一些错误。现在我似乎无法修复的唯一错误是我网站内的搜索表单。我认为这很奇怪,因为该表单不使用登录表单中的任何变量。当我测试找出问题时,我发现当我的搜索脚本部分进入 while 循环时,它就会中断。如果您有任何建议或帮助,我们将不胜感激。我将在下面提供我的代码。
hub.php
<?php
// this file connects to the database
include("includes/connect.inc.php");
session_start();
if (isset($_SESSION['id'])) {
$uid = $_SESSION['id'];
}
//If the search input was submitted then run
if(isset($_POST['search'])){
// turn that the user searched into a varible
$searchQ = $_POST['search'];
// delete any symbols for security
$searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);
// Define varibles before the loop
$searchArray = array();
$searchIndex = 0;
// Search through these columns inside the main database
$searchQuery = mysqli_query("SELECT * FROM database WHERE
title LIKE '%" . mysqli_escape_string( $searchQ ) . "%'
");
// count the number of results
$searchCount = mysqli_num_rows($searchQuery);
if($searchCount != 0){
while($row = mysqli_fetch_array($searchQuery)){
$titleSearch = $row['title'];
$dateSearch = $row['date'];
// buile the array which will hold all the results
$searchArray[$searchIndex] = array($titleSearch, $dateSearch);
$searchIndex++;
}
}
}
// End of search php
<form id="search" action="hub.php" method="POST">
<div id="searchIcon"></div>
<input type="search" name="search" placeholder="Search">
</form>
这是连接文件|它用于许多其他工作正常的表单和循环。
<?php
$host = "MYHOSTINGDOMAIN";
$username = "MYUSERNAME";
$password = "MYPASSWORD";
$db = $username;
$connect = mysqli_connect($host, $username, $password, $db) or die(mysqli_connect_error());
我收到此错误
Warning: mysqli_query() expects at least 2 parameters, 1 given in /services/webpages/d/i/digitalicon.ca/public/hub.php on line 42
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /services/webpages/d/i/digitalicon.ca/public/hub.php on line 45
最佳答案
好的,根据提供的更新信息,您似乎是通过 mysqli_* 函数进行连接,然后在该脚本中使用 mysql_* 函数(切换到 mysqli 可能是登录更改的一部分)。
因此您需要更新脚本以使其变为:
<?php
// this file connects to the database
include("includes/connect.inc.php");
session_start();
if (isset($_SESSION['id'])) {
$uid = $_SESSION['id'];
}
// //If the search input was submitted then run
if(isset($_POST['search'])){
// turn that the user searched into a varible
$searchQ = $_POST['search'];
// delete any symbols for security
$searchQ = preg_replace("#[^0-9a-z]#i", "", $searchQ);
// Define varibles before the loop
$searchArray = array();
$searchIndex = 0;
// Search through these columns inside the main database
$searchQuery = mysqli_query("SELECT * FROM database WHERE
title LIKE '%" . mysqli_escape_string( $searchQ ) . "%' or
date LIKE '%" . mysqli_escape_string( $searchQ ) . "%'
");
// count the number of results
$searchCount = mysqli_num_rows($searchQuery);
if($searchCount != 0){
while($row = mysqli_fetch_array($searchQuery)){
$titleSearch = $row['title'];
$dateSearch = $row['date'];
// buile the array which will hold all the results
$searchArray[$searchIndex] = array($titleSearch, $dateSearch);
$searchIndex++;
}
}
}
// End of search php
这包括我对实际查询数据库的初步修复,您之前只是在其中转义字符串。
关于当我进入 While 循环时 PHP 搜索不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31363851/
我是一名优秀的程序员,十分优秀!