doubian19900911 2016-08-26 16:50
浏览 10
已采纳

使用用户输入在我的数据库上进行查询

I am writing some code that will allow the user to enter there location and month of the year and be presented with stars viewable that month from their location. I have my database completed and query working on mySQl but trying to implement it on the web page and have come accross some difficulties, I have two inputs the user can make and the program below that runs the function, If anyone can spot any errors, The function will not output any data when location and month entered but when nothing is entered and submit pressed it outputs events but cannot figure out why.

The search function is below and then the event. php is below that

<?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;
}
}

this code below is the event page that calls the search function above ?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);


}
?>

This below is where the user inputs their location and month

Search Events

Search ">
<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>

If anyone can see any errors or mistakes it would be great help the more I work on it the further away I am getting

  • 写回答

1条回答 默认 最新

  • doujiu8826 2016-08-26 16:55
    关注

    Well the easiest error I can see from a limited snapshot like this is you are passing the month through $search_term1 HOWEVER you are searching for city with $sanitized1 which leads me to believe you have the month and city reversed in the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类