-8

this is my search_candidate.php file

<?php

   $name = $_GET['name'];
   $sql = "SELECT * FROM candidates WHERE Name = $name";
   $query = mysql_query( $sql );
          if(mysql_num_rows($query) == "")
          {
          echo "no result found";
          }
          echo "<table>";

          echo "<thead></thead>";
                while( $row = mysql_fetch_array( $query ) )
                     {
                      echo "<tr></tr>";
                     }
          echo "</table>";
?>
Rizier123
  • 57,440
  • 16
  • 89
  • 140
  • Are you getting any syntex error? – Amit Verma Jan 23 '15 at 12:44
  • `$query = mysql_query( $sql ) or die(mysql_error());` and see the error you're not checking for. – Funk Forty Niner Jan 23 '15 at 12:49
  • 1
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** **[DANGER! You need to prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)** – Jay Blanchard Jan 23 '15 at 15:27

3 Answers3

4
SELECT * FROM candidates WHERE Name = $name

$name is a string and needs to be in quotes like '$name'

But even after that fix you wont get anything because your loop doesnt print any data. It just opens and closes new rows without anything inside.

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Hanky Panky
  • 45,969
  • 8
  • 69
  • 95
2

Try this code it will work.

<?php

   $name = $_GET['name'];
   $sql = "SELECT * FROM candidates WHERE Name = '$name'";
   $query = mysql_query( $sql );
          if(mysql_num_rows($query) == "")
          {
          echo "no result found";
          }
          echo "<table>";

          echo "<thead></thead>";
                while( $row = mysql_fetch_array( $query ) )
                     {
                      echo "<tr><td>".$row['name']."</td></tr>";
                     }
          echo "</table>";
?>

Copied code from question and make changes on 2 errors $name string must be in quotes and added <td>".$row['name']."</td> in your loop to show something.

Your code allow any one to inject so try to use PDO or MySQLi connection.

Huzoor Bux
  • 1,022
  • 4
  • 20
  • 45
1

Seems name is string so add a quote to $name in query

$sql = "SELECT * FROM candidates WHERE Name = '$name'";

Note :- mysql_* has been deprecated use mysqli_* or PDO.

Rakesh Sharma
  • 13,570
  • 4
  • 35
  • 42