Search by PHP Mysql

Khaled Claude Boussoffara

I want to get some variables from a form and i will use those variables to make a search bar for example:

$var=$_POST['var']

I want to put this variable in a request like this:

$SQL = 'SELECT * FROM Table ORDER By $var'

Any suggestions please? How can I transform this request to a dynamic request? thank you :)

The code is:

<form>
     <lable for="Variable">
     <input type ="text" name="variable" placeholder="Search by : ">
</form> 
<?php
    $variable = $_POST['variable'] 
    sql = 'SELECT * FROM Total ORDER BY $variable'; 
?>
Václav

At first,

use method="POST" and action="file.php" (but action is not strictly needed, in some cases like processing by AJAX)

<form method="POST" action="file.php">
   <lable for="Variable">
   <input type ="text" name="variable" placeholder="Search by : ">
</form>

At second you need sanitize input taken from form (it means, you have to eliminate anything that would harm your pages - with this PDO or else layer can help).

At third, you need to rewrite

sql = 'SELECT * FROM Total ORDER BY $variable'

to

sql = "SELECT * FROM Total ORDER BY $variable"

or

sql = 'SELECT * FROM Total ORDER BY '.$variable

because else used variable would be used as is written, instead its content (given by form).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related