Click here to Skip to main content
13,630,423 members
Rate this:
 
Please Sign up or sign in to vote.
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 13-Sep-17 6:30am
Updated 13-Sep-17 14:29pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
-----
$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[^]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.

$book=$row['books'];


It should be
$book=$row['book'];
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180712.1 | Last Updated 13 Sep 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100