php - Search DB through MYSQLi -


i have 1 simple question can't understand why doesn't work. i'm trying querying db through mysqli , if hard code variable works perfect, if try make dynamic (via input box) displays nothing.

here code:

if(isset($_get['search'])) {     $searchbyinput = $_get['search']; }  $query = 'select * table vin="$searchbyinput"';  if ($stmt = $mysqli->prepare($query)) {     $stmt->execute();      $result = $stmt->get_result();      if($result->num_rows > 0) {         while($row = $result->fetch_assoc()) {             //display table         }     } } 

if i'm 'echo'ing variable inside if statement, displays right value reason doesn't want execute query correctly variable.

i hope can point me in right direction.

thank you

you should use single quoted strings inside query make valid, , should (conveniently) use double quote expand variables inside string in first place:

$query = "select * table vin='$searchbyinput'"; 

even better, let mysql handle binding parameter. solves sql injection vulnerabilities mentioned in comments:

$searchbyinput = ''; if(isset($_get['search'])) {     $searchbyinput = $_get['search']; }  $query = 'select * table vin=?';  if ($stmt = $mysqli->prepare($query)) {     $stmt->bind_param('s', $searchbyinput);      $stmt->execute();      $result = $stmt->get_result();      if($result->num_rows > 0) {         while($row = $result->fetch_assoc()) {             //display table         }     } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -