0

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.

  • Windows 8
  • Wampserver
  • Notepad++

    I restarted server and PC, but nothing changed. Below is my complete code.

    Find Entries:
    Male
    Female

    <?php
    if(isset($_POST['submit']))
    {
         echo $gender=$_POST['$gender'];
        $connect=mysql_connect("127.0.0.1","root","", "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());
        }
    }
    ?>
    
5
  • print your query and run it in phpmyadmin.than check is all things goes well ? Commented Jan 7, 2014 at 13:57
  • Maybe changing $_POST['$gender'] to $_POST['gender']? Shooting in the dark. Maybe your $gender param is empty. Commented Jan 7, 2014 at 13:59
  • @user3168766 yeah you are putting $gender inside the POST['$gender'] gender you getting form through POST method is not a php variable Commented Jan 7, 2014 at 14:47
  • @Rana Muhammad Waqas I solved my problem, thank you for your co-operation. Commented Jan 7, 2014 at 14:52
  • @Rana Muhammad Waqas Answer having green color sign, was very helpful for me. Commented Jan 7, 2014 at 14:53

5 Answers 5

3

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());
     }
}
?>
Sign up to request clarification or add additional context in comments.

8 Comments

I can send you whole code. If you want to see. It is small code consisting of 34 lines.
@user3168766 Please provide the whole code in your question and also, please provide a print of the POST data, so we can ensure everything is as expected.
@user3168766 Also please clarify how you save the gender in the table, ie M/F or Male/Female. This may be the issue.
I saved Male and Female.
<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> </body > </html>
|
3

mysql_connect should be mysqli_connect

Comments

1

Try this...

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

Removed extra spaces in query

3 Comments

I copied this excerpt and replace my previous query with it. Nothing changed.
as per @The Humble Rat change echo $gender=$_POST['$gender']; to $gender=$_POST['gender'];
@tomas I tried your suggestion. I am sorry to say nothing changed. I restarted server too.
0

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?

5 Comments

Yes I am including submit.
if(isset($_POST['submit'])) you can see this.
Just checking, sometimes it's easy to miss. If it wasn't present then it would explain why nothing else was running.
Add the comments below the question if you need clarification.
@chandrayya-g-k I tried, I was told that my rep wasn't high enough yet. I'll work harder....
0

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());
    }
}
?>

1 Comment

I didn't received any output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.