Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
so i have a php script to search in a database what a person wrote in a search box.
i got this code from a tutorial and edit a bit.
but the problem is that it does not output anything why ?

<pre lang="PHP"><pre><?php
$connection = mysqli_connect('localhost','','',);
$output='';
if(isset($_POST['search'])){
    $searchkey= $_POST['search'];
    $searchkey=preg_replace("#[^0-9a-z]#i", "", $searchkey);

    $query = mysqli_query($connection,"SELECT * FROM books WHERE book LIKE '%$searchkey%'") or die("Could not search!");
    $count = mysqli_num_rows($query);

    if($count == 0){
        $output="There was no search result!";
    }
    else{
        while($row=mysqli_fetch_array($query)){
            $book=$row['books'];

            $output .='<div>'.$book.'</div>';

           // echo "$output";

        }
    }
}
?>




<!DOCTYPE html>
<html>
<head>
<title>library books</title>
</head>
<body>
<form action="index.php" methode="post">
		<input type="text" name="search" placeholder="search your book"/>
		<input type="submit"  value="search"/>
</form>
<?php print("$output");?>
</body>
</html>


What I have tried:

i looked close to the tutorial to see or i missed anything
Posted
Updated 13-Sep-17 14:29pm
v3

Main issue is You spelled method wrong, so there is no $_POST value. Hence never get executed.

Few other notes:
1. Select with * is always a bad programming practice
2. You are doing it now, so you learn it now, about SQL Injection.
 
Share this answer
 
Use the debugger to see what your code does and don't.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
-----
PHP
$query = mysqli_query($connection,"SELECT * FROM books WHERE book LIKE '%$searchkey%'") or die("Could not search!");

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
v2
Look like there is a minor issue per your example. This line will cause an error, I'm assuming there no field/column with the name "books" in the books table.

PHP
$book=$row['books'];


It should be
PHP
$book=$row['book'];
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900