Umar Farooq
Umar Farooq

Reputation: 3

PHP code executes but no output

I am trying to learn "searching elements from mysql database using php".

For this I created a database named randomdata. In randomdata database there is a table named randomtable. In this table there are four columns: Name, Surname, Email and Gender.

I want to search people by there Gender. For this I tried following query.

 $query="SELECT * FROM randomtable WHERE Gender =' ".$gender . " ' ";

I tried both, GET and POST functions. But still I am not able to take output. I am using these.

Upvotes: 0

Views: 145

Answers (5)

The Humble Rat
The Humble Rat

Reputation: 4696

It looks like the issue is with assigning the POST variable to $gender

Currently you are using

echo $gender=$_POST['$gender'];

Please try changing this to

$gender=$_POST['gender'];

UPDATE

After testing your code it seems the isset is the issue. There is never a POST['Submit'].

To fix this you need the name attribute in the submit input ie

<input type="submit" Value="Search" name="Submit"/>

Also in the query you have spaces either side of the $gender variable. I now have the code working, try with this.

<html>
<body>
 Find Entries: <br> 
 <form action="" method="POST"> 
 <input type="radio" name="gender" Value="Male"> Male </input>
 <br> 
 <input type="radio" name="gender" Value="Female"> Female </input>
 <br> 
 <input type="submit" Value="Search"/> 
</form> 

<?php
if(isset($_POST['gender']))
{
     //print_r($_POST);
     $gender=$_POST['gender'];
     $connect=mysqli_connect("127.0.0.1","root","password", "randomdata");
     if($connect)
     {
        //echo 'I am connected';
        $query="SELECT * FROM randomtable WHERE Gender = '".$gender . "' ";
        //echo  $query;
        $results=mysqli_query( $connect,$query);
        while($row = mysqli_fetch_array($results))
        {
            echo $row['Name']  . "<br/>" . $row['Surname']  . "<br/>" . $row['Email']  .   "<br/>" ;
        }
     }
     else
     {
        die(mysql_error());
     }
}
?>

Upvotes: 3

Yami
Yami

Reputation: 1425

I guess you were intending to assign the "submit" index to the $gender variable instead of the "gender" index? Try the code below:

<?php
if(isset($_POST['submit']))
{
    $gender = $_POST['submit'];
    $connect = mysql_connect("127.0.0.1", "root", "", "randomdata");
    if($connect)
    {
    $query = 'SELECT * FROM randomtable WHERE Gender = "' .$gender .'"';
        $results = mysqli_query($connect, $query);
        while($row = mysqli_fetch_array($results))
        {
            echo $row['Name']  . "<br/>" . $row['Surname']  . "<br/>" . $row['Email']  .   "<br/>" ;
        }
    }
    else
    {
        die(mysql_error());
    }
}
?>

Upvotes: 0

tomsowerby
tomsowerby

Reputation: 498

Is there no output at all? Very odd indeed.

What are you posting?

Are you sure that you're including 'submit' in your test post?

If this doesn't help, perhaps there's a more severe error that's not allowing the script to run? Are you able to see the php or apache error logs?

Upvotes: 0

Tom&#225;s
Tom&#225;s

Reputation: 3561

mysql_connect should be mysqli_connect

Upvotes: 3

user1844933
user1844933

Reputation: 3417

Try this...

$query="SELECT * FROM randomtable WHERE Gender ='".$gender . "'";

Removed extra spaces in query

Upvotes: 1

Related Questions