【问题标题】:Making a query on my database using user input使用用户输入对我的数据库进行查询
【发布时间】:2017-01-03 09:55:09
【问题描述】:

我正在编写一些代码,允许用户在那里输入一年中的位置和月份,并在该月从他们的位置显示可查看的星星。我已经完成了我的数据库并在 mySQl 上进行了查询,但尝试在网页上实现它并遇到了一些困难,我有两个用户可以输入的输入,下面的程序运行该函数,如果有人能发现任何错误,该函数在输入位置和月份时不会输出任何数据,但是当没有输入任何内容并按下提交时,它会输出事件但无法弄清楚原因。

下面是搜索功能,然后是事件。 php低于那个

<?php
/**
* Performs a search
*
* This class is used to perform search functions in a MySQL database
*
*
*  
*/
class search {
/**
* MySQLi connection
* @access private
* @var object
*/
private $mysqli;

/**
* Constructor
*
* This sets up the class
*/
public function __construct() {
// Connect to our database and store in $mysqli property
$this->connect();
}
/**
* Database connection
* 
* This connects to our database
*/
private function connect() {
$this->mysqli = new mysqli( 'localhost', 'conor', 'trevor29', 'site_db' );
}

/**
* Search routine
* 
* Performs a search
* 
* @param string $search_term The search term
* 
* @return array/boolen $search_results Array of search results or false
*/
public function search($search_term, $search_term1) {
// Sanitize the search term to prevent injection attacks
$sanitized = $this->mysqli->real_escape_string($search_term);
$sanitized1 = $this->mysqli->real_escape_string($search_term1);


// Run this Query
$query =$this ->mysqli->query("
SELECT event_name FROM event
INNER JOIN event_month
ON event.event_id = event_month.event_id
INNER JOIN month
ON event_month.month_id = month.month_id
INNER JOIN location
ON location.hemisphere = event.hemisphere
WHERE month_name LIKE '%{$sanitized}%'
AND city LIKE '%{$sanitized1}%'
");

// Run the query
//$query = $this->mysqli->query("
//SELECT *
//FROM event inner join location
//ON event.event_id = location.event_id 
//WHERE city LIKE'%{$sanitized}%'
//");

// Check results
if ( ! $query->num_rows ) {
  return false;
}

// Loop and fetch objects
while( $row = $query->fetch_object() ) {
  $rows[] = $row;
}

// Build our return result
$search_results = array(
  'count' => $query->num_rows,
  'results' => $rows,
);

return $search_results;
}
}

下面这段代码是调用上面搜索功能的事件页面 ?php

//getname
session_start();

//Check if search data was submitted

$search_results="";

if  (isset( $_GET['s'])){

// Include the search class
require_once( dirname( __FILE__ ) . '/class-search.php' );

// Instantiate a new instance of the search class
$search = new search();

// Store search term into a variable
$search_term =($_GET['s']);
$search_term1 =($_GET['m']);


// Send the search term to our search class and store the result
$search_results = $search->search($search_term, $search_term1);


}
?>

下面是用户输入他们的位置和月份的地方

搜索事件

搜索 ">
<form action= "" method = "get">
<div class= "form-field">
<label for="search-field">Search</label>
<input type = "type" name ="m" placeholder = "Enter the month" results="5" value = "<?php $search_term1; ?>">
<input type ="submit" value = "Search">
 </div>

如果任何人都可以看到任何错误或错误,那将是很大的帮助,我越努力越远

【问题讨论】:

  • <input type = "text" ....也是,s在哪里
  • 也没有s 名称属性/输入,也没有回显value = "&lt;?php $search_term1; ?&gt;。这应该怎么做? results="5"...调试你的代码。
  • 此外,如果搜索词是可选的,则条件应该是 OR 而不是 AND
  • if (isset( $_GET['s'])){...} 永远不会发生。
  • 你在下面有答案;现在问他们。它不在我的手中。

标签: php database querying


【解决方案1】:

嗯,我可以从像这样的有限快照中看到的最简单的错误是您正在通过 $search_term1 传递月份但是您正在搜索 city$sanitized1 这让我相信您的月份和城市颠倒了在查询中。

【讨论】:

  • 谢谢,我已经看这个很久了,我会发现它之前已经有一段时间了
  • 它现在似乎正在显示结果,但是如果按钮在没有输入的情况下提交,它会输出数据库中的所有事件并且不知道为什么
  • 因为您使用的是like like '%%'get all 相同,您可能需要编写一些额外的代码来处理空字段或if ($field == '') then $field = 'xxxxxxxxxxxxxx'; 以防止这种情况发生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
相关资源
最近更新 更多