php search script for a book database using mysql - php

I have a database of about 3000 books and i want to be able to search my database for books either by titles, subjects or authors.. I've done my best trying to write a script using php and mysql but i still didn't get it right. can anyone assist please. Below is how far I've come with the script.. and an example of the table in my mysql database
<?php
if (#$find == "")
// Otherwise we connect to our Database
mysql_connect("localhost", "root", "erhun") or die(mysql_error());
mysql_select_db("books") or die(mysql_error());
// We perform a bit of filtering
#$find = strtoupper($find);
#$find = strip_tags($find);
#$find = trim ($find);
#$search = strip_tags(stripslashes(mysql_real_escape_string(#$db, #$_POST["search"])));
//query the database
#$query = ("SELECT * FROM `project` WHERE (`author1` LIKE '%$search%' OR `main_title` `LIKE '%$search%' OR `subj1` LIKE '%$search%')");
//displaying the data
#$results=mysql_query(#$query) or die(mysql_error ());
if(mysql_num_rows(#$results) >= 1)
//here the table starts
echo "<table id='results'>";
while($row=mysql_fetch_array($results))
{
echo "<tr><td><img src='image1/".$row['image']."' height='100px' width='90px'></td><td valign='top'>
<b><a href='details.php?id=".$row['book_id']."'>".$row['main_title']."<br/>
By: ".$row['author1']."</a></b><br/>
<b>Call no:</b> ".$row['call_no']."<br/>
<b>Type:</b> ".$row['item_type']."<br/>
<b>Year:</b> ".$row['publdate']."</td></tr>";
}
echo "</table>";
?>
my table contains these different fields
Full texts
book_id
image
main_title
author1
call_no
item_type
publdate
publplace
publshr
item_home
item_status
subj1
subj2

Try like
#$query = "SELECT * FROM `project` WHERE (`author1` LIKE '%$search%' OR `main_title` LIKE '%$search%' OR `subj1` LIKE '%$search%')";
You have an extra quote ` after "main_title"

UPDATED :
Try changing this line :
#$search = strip_tags(stripslashes(mysql_real_escape_string($db, #$_POST["search"])));
to :
#$search = strip_tags(stripslashes(mysql_real_escape_string( #$_POST["search"])));
The only thing I needed to change was dropping the $db variable, just passing in the search string, and it seems to work fine. It might be that $db was needed for some other reason so I recommend going back to the documentation you got that from and investigate further.
P.S. I'd still recommend using MATCH AGAINST. If your subject, title and author fields have an appropriate FULLTEXT index you can use MATCH AGAINST e.g. :
#$query = "SELECT * FROM `project` WHERE MATCH (subj1, main_title, author) AGAINST ('$search' IN NATURAL LANGUAGE MODE)";
This query would require that you have a fulltext index over all three columns:
ALTER TABLE project ADD FULLTEXT(subj1, main_title, author);
The list of columns in the index must match the list of columns you want to MATCH AGAINST in the query for the sake of optimisation I believe. You'd have to create other indexes if you wanted to be able to match against individual columns separately. That query also worked on my test page and it should better support normal search queries with a bit of experimentation.
P.P.S. If you have a choice of DB I would recommend using something with more support for different types of text searching. The only relational databases I'm really familiar with are PostgreSQL and MySQL and PostgreSQL has a lot better support for text searching.

Related

Searching multiple MySQL tables from an HTML form and returning the data in alphabetical order

I'd like to search 3 separate tables I've created from 1 form and return the data in alphabetical order. Currently I can search 3 tables separately with use of a drop-down box (from my form) to select a table but I've fallen short at querying them simultaneously and returning all the data in alphabetical order. I've been trying to solve it but I'm struggling a lot.
Currently what my program searches through separate tables for what the user previously input into my form. Now I would like to be able to search through all my tables in 1 go and return the information in alphabetical order, meaning some values from tables might be spread out.
I have 3 tables: "Insecttable", "birdtable" and "butterflytable"
There are 3 controllers: "ControllerInsectTable", ControllerBirdTable and "ControllerButterflyTable"
I'm trying to make another controller: "ControllerAllTables" that can search through all tables.
HTML:
<form name="searchForm" id="searchForm" method="POST" action="ControllerAllTables.php">
Search for: <input type="text" name="aSearch">
<input type="submit" name="searchButton" value="Search">
</form>
PHP:
// Collect Data
// If an input has been given
if(isset($_POST["aSearch"])) {
$searchq = $_POST["aSearch"];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq); //Can only search words
// Select statements if keywords match
$sql = "SELECT * FROM insecttable WHERE insectName LIKE '%$searchq%'";
$sql1 = "SELECT * FROM butterflytable WHERE butterflyName LIKE '%$searchq%'";
$sql2 = "SELECT * FROM birdtable WHERE birdName LIKE '%$searchq%'";
}
// Tests if the code been inserted
if ($conn->query($sql && $sql1 &&sql2)=== TRUE){
echo "The rows you have searched for are:";
} else {
echo "Connection failed: ";
echo $conn->error;
}
// Show fields
$result = $conn->query($sql && $sql1 &&sql2);
// Output data of each row
if ($result-> num_rows> 0) {
readfile("ViewReturn.html");
while($row = $result-> fetch_assoc()) {
// echo "ID: ".$row["id"]. "<br>";
echo "Insect: ".$row["insectName"]. "<br><br>";
echo "Bird: ".$row["birdName"]. "<br><br>";
echo "Butterfly: ".$row["butterflyName"]. "<br><br>";
}
} else {
echo "0 results";
}
I cut out some of the bits of my code that didn't affect my question, like making connections and such.
At the moment, I have no idea how to return values in order, and I'm seriously stuck at searching multiple tables from one query. I've looked at "joins" but I really don;'t understand them.
As you can tell I'm not very good at PHP, and I hope I can soon rid it from my life. I've been completely unsuccessful in this section of my program and I'm looking for help and criticism. I know it's a lot to ask but I'm really stuck, thanks.
You'll want to use the UNION operator to combine the query results, being sure to order the data after it has been combined. This can affect performance, but hopefully your result sets aren't too large.
SELECT name -- because we NEVER use select *
FROM
(
SELECT insectName AS name FROM InsectTable WHERE insectName LIKE '$searchq%'
UNION ALL
SELECT butterflyName AS name FROM ButterflyTable WHERE butterflyName LIKE '$searchq%'
UNION ALL
SELECT birdName AS name FROM BirdTable WHERE birdName LIKE '$searchq%'
)
Also, you might want to consider a redesign of the database. If the items in your tables are all related then they are effectively a super class. If you Google "SQL super class design" you should be able to find some good patterns for this.
Also, appending the word "Table" to the end of all of your table names is not something that is usually done. If your table holds data about insects then it's "Insect" or "Insects" (I'll ignore the singular/plural debate for now). The fact that it's a table is already self-evident.

passing parameter to select statement from url

I have a database with a table which has two columns, lets say aa_id and bb_id - each of the the columns is a foreign key relating to another table and both columns are making a composite key for this particular table. there are several rows containing either the same aa_id and different bb_id or the same bb_id and different aa_id.
using pdo I want to extract the rows of the same - let's say - aa_id and I want to do this passing the parameter value in url. so the result of the select statement should be several rows and they should be saved as - for example - an array.
I have tried to do this with following code:
$sql = sprintf("select aa_id, bb_id from a_table where aa_id=:aa_id");
$res = $db->query($sql);
$rows = $res->fetch(PDO::FETCH_ASSOC);
foreach($rows as $key=>$value)
{
echo $key . " - " . $value . "</br>";
}
And it give no result.
I does work if I state the value of aa_id in the query like this
$sql = sprintf("select aa_id, bb_id from a_table where aa_id=191919");
, but it extracts no data if I put the value in url.
I am not really sure what to search for in the web because I don't know what's the notation called (if it is). If somebody could tell me what may be wrong with the code or give me directions to what I should look for in the web among tutorials or documentation I will be grateful. Perhaps somebody could recommend a good source of knowledge about mysql, php and pdo... Thanks in advance.
Well yeah, :indicator doesn't just automatically load in $_GET['indicator'], you need to manually bind it.
Assuming the URL ends with, ?aa_id=191919, your code might look something like this:
$sql = "select aa_id, bb_id from a_table where aa_id=:aa_id";
$res = $db->prepare($sql);
$res->bindValue(':aa_id', $_GET['aa_id'], PDO::PARAM_INT);
$res->execute();
$rows = $res->fetch(PDO::FETCH_ASSOC);
while($row=$res->fetch(PDO::FETCH_ASSOC))
{
print_r($row);
}

Delete one column from table, Update another with PHP MYSQL

I have two tables. One table is the matches table (e2wedstrijden) and another table is my scoring table with the points earned etc. (e2teams).
Now I have that I can delete a match from the e2wedstrijden table. And this is working fine.
But I want that if I delete a match from that table. It also add or decrease points to the table ("e2teams"). I tried to compare the tables but this is not working.
So I want for example:
If($row['thuisscore'] == $row['uitscore']) what are to row names in my e2wedstrijden table. So if these two are the same (like 0-0 or 1-1 or something) Than it needs to decrease 1 point from the table e2teams. But only by the teams that are the same as the rows "Thuisteam" and "Uitteam" in my e2wedstrijden table. So the Row Thuisteam (in "e2wedstrijden") Needs to find the same result in ("e2teams") row Team. And this needs to be done the same with the Row Uitteam (in "e2wedstrijden") Needs to find the same result in ("e2teams")
Thuisteam and Uitteam = Dutch for hometeam and awayteam. I think my fault is that the system can't link the 'Thuisteam' from e2wedstrijden to the Team in e2teams but don't know how to solve it
This is my deletematches.php, It deletes the match but doesn't decrease or adds points:
<?php
if(!isset($_COOKIE['E2ingelogd'])) {
header("location:../../index.php");
}
include "../../connect.php";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("login", $dbhandle);
$result = mysql_query("SELECT * FROM e2wedstrijden WHERE ID = ".$_GET['del']."");
while($row = mysql_fetch_assoc($result)){
if( $row['thuisscore'] == $row['uitscore']){
echo $row['thuisscore'];
mysql_query("UPDATE e2teams SET Punten = Punten-1 WHERE Team ='".$row['Thuisteam']."'");
mysql_query("UPDATE e2teams SET Gespeeld = Gespeeld-1 WHERE Team = ('".$row['Thuisteam']."'");
mysql_query("UPDATE e2teams SET Verloren = Gelijk-1 WHERE Team ='".$row['Uitteam']."'");
echo "Team is deleted";
}else{
echo 'Update Error!';
}
}
$table_1_delete = mysql_query("DELETE FROM e2wedstrijden WHERE ID = ".$_GET['del']."");
?>
This is my e2teams table:
And this is my E2wedstrijden table:
So i need something like:
UPDATE e2teams SET Punten = Punten-1 WHERE Team = Look in table ("e2wedstrijden) deleted Thuisteam and deleted Uitteam
Hope you can help
You've placed an extra parentheses in the 2nd query for "gespeeld" right after the equal sign:
mysql_query("UPDATE e2teams SET Gespeeld = Gespeeld-1
WHERE Team = ('".$row['Thuisteam']."'");
Is this what isn't updating?
Without being 100% sure on how your data model works, it might make sense at refactoring what you have. Something that might be useful would be to create a view of the summary table and just update the data from the child/master table.... aggregating in the view layer. Views in mysql can be seen here.
If you are stuck with the data model you have (legacy application, etc.) you can possibly look at triggers if you have to modify data in two tables you might want to consider stored procedures or triggers, discussed here and here.
The third thing that comes to mind, is around correlated sub-queries and how you could reference the another table in a sort of update-from. However, you're ID's aren't surrogate keys in this situation.
Also, have a look at sql injection; I haven't looked at PHP in a while but those sql statements kind of look like they are created with sting composition
Good luck,

Comparing strings returned from a mySql query

I am querying a large number of codes from my database, and need to have some validation before a user can input another code in to the database.
An example code would be this:
TD-BR-010212-xxxxxxxx
Where TD represents a promotion, BR represents a place, the numbers represent a date, and the rest are random.
My problem is that before the code is entered into the DB, I want to check to see if the date and place for that code already exists, as they should not be allwed to enter a code from the same place and date.
I assume it would be something within a loop as I already have:
$location_part_of_td = $code[2].$code[3];
$date_part_of_td = $code[4].$code[5].$code[6].$code[7].$code[8].$code[9];
$trade_day_result = mysql_query('SELECT * from wp_scloyalty WHERE promotion_type = trade-day') or die(mysql_error()); // Pulls all trade day codes from the database and checks the date part of the code.
// the date part exists with the same area part, user cant redeem.
while($info = mysql_fetch_array( $trade_day_result ))
{
$code = $info["product"];
}
But Im just not sure about the best way to check the strings..
You can use a MySQL LIKE clause to get entries in your DB that resemble your code.
Example:
$code_exists = mysql_query(
"SELECT 'a' FROM table_name WHERE column_name LIKE 'TD-BR-010212-%'"
);
if(mysql_num_rows($code_exists) > 0) {
// The specified place/date is taken
} else {
// No promotion at place BR on the specified date.
}
The '%' is used as a wildcard in SQL LIKE clauses.
You have two approach to solving this issue. Assuming you have access to alter the table.
Add a unique constraint to the table base off of the two columns.
Or Your approach by selecting all of the Location and Date, and see if it return any results.
SQL: SELECT COUNT(*) as counter FROM table where column = 'TD-BR-010212-%'
And check to see if counter return > 0;
I would use the LIKE statement in your SELECT and pull entries that start with the same promotion, place, and date. Unfortunately I don't know how your table looks so bear with me:
$promo_query = "SELECT * FROM wp_sclocalty WHERE column_name LIKE 'TD-BR-010212-%'";
$promo_result = mysql_query($promo_query);
if(mysql_num_rows($promo_result) == 0) {
// the promo code has NOT been used
} else {
// the promo code HAS been used
}
try this query
$part_code=substr($code, 0)
$records =mysql_query("select id from tableName where SUBSTRING(code,1,12)= $part_code");
if(mysql_num_rows($records) > 0)
{
// Duplicate exit
}
else
{
// insert code in DB
}
If you can, you'll get better performance and easier coding if you break apart the code into different fields when you save the data in each row. That way you can write queries that specifically check values for the components pieces of the code - you can even set rules in the database (like UNIQUE) to ensure that some parts are kept unique.
Specifically, I'd suggest:
create table your_table (
[... your other columns ...]
promotion char(2),
place char(2),
pr_date date,
pr_ident varchar(50)
)
Your first row would be ([...], 'TD','BR','2012-01-02', 'xxxxxxxx'). And queries would not require unpacking the formatted string - you could say things like "where promotion = 'TD' and place in ('BR','XX') ...". Simple, eh?

Trigger database search result with multiple keywords

I have a PHP search script that searches a MySQL database to answer questions. Currently my database is set out like this...
idquestion = what is the timeanswer = The time is 12:00
This data is then sent to my PHP code:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("database");
if(!empty($_GET['q'])){
$query=mysql_real_escape_string(trim($_GET['q']));
$searchSQL="SELECT * FROM questions WHERE `question` LIKE '%{$query}%' LIMIT 1";
$searchResult=mysql_query($searchSQL);
while($row=mysql_fetch_assoc($searchResult)){
$results="{$row['answer']}";
}
echo $results;
}
?>
Currently, the users query must contain exactly the same text as the question field. My question is; how can I make this work so it triggers a certain for several keywords?
I hope you can understand my question
I think full test search index can help
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html SOUNDEX function can be useful in this context too.
You can split query to keywords and generate dynamic SQL but it is inefficient (performance issues SQL injection attacks ) see http://php.net/manual/en/function.split.php
Alternative is to add table QUESTION_KEYWORD(QUESTION_ID,KEYWORD), split question and search this table to find best QUESTION_ID, but full text search index is more efficient. In fact full text search index uses similar data structure to optimize test search.
For an easy quick-fix based on your comment you could do something as simple as this:
// Assuming query string is ?q=make,sponge,cake
$searchSQL="SELECT * FROM questions WHERE ";
$count = 0;
foreach (explode(',', $_GET['q']) as $query) {
$searchSQL .= "`question` LIKE '%" .mysql_real_escape_string(trim($query)) . "%' ";
if ($count++ > 0) {
$searchSQL .= ' OR ';
}
}
$searchSQL .= ' LIMIT 1';
$searchResult = mysql_query($searchSQL);

Categories