Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all
I am try to make search engine on my post page but this message show on page (
Warning: Use of undefined constant search_query - assumed 'search_query' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\CMS\All_post.php on line 721) in echo "Some RESULT"; i am try to print something but not work also

What I have tried:

 <?php
            
            
        if(isset($_POST['submit'])) {
            
        $search = $_POST['search'];
            
        $query = "SELECT * FROM posts WHERE post_title LIKE '%$search%' ";   
        $query_search = mysqli_query($connection,$query); 
        }
//        if(!query_search) {
//        die("QUERY FAILED" . mysqli_error($connection));
//        }
//            $count = mysqli_num_rows($query_search);
//            if($count == 0){
//            echo "<h1> no result</h1>";
//               
//            }else {
//                
//                echo "Some RESULT";
//            }
//            }   
           
        ?>
Posted
Updated 19-May-18 22:53pm

1 solution

Quote:
Warning: Use of undefined constant search_query - assumed 'search_query' (this will throw an Error in a future version of PHP)

PHP
<?php


if(isset($_POST['submit'])) {

	$search = $_POST['search'];

	$query = "SELECT * FROM posts WHERE post_title LIKE '%$search%' ";
	$query_search = mysqli_query($connection,$query);
	// $query_search is defined inside a conditional structure
}
//  if(!query_search) { // thus it may not exist at this point
//      die("QUERY FAILED" . mysqli_error($connection));
//  }
//  $count = mysqli_num_rows($query_search);
//  if($count == 0){
//      echo "<h1> no result</h1>";
//  }else {
//      echo "Some RESULT";
//  }
//}

?>


PHP
$query = "SELECT * FROM posts WHERE post_title LIKE '%$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
 
Comments
MuhammadNaamh 20-May-18 5:16am    
Thank you ....
If I convert this code to function will it be the same problem about the "SQL injection"?
Patrice T 20-May-18 5:20am    
Read the links for explanations and solution about SQL injection

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


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