Search data between two specific year?

Discussion in 'PHP' started by jimjack145, Mar 21, 2007.

  1. #1
    Hi All,

    I have tabel called "candidate" in that i have two fields call Graduation Passing year and Post Graduation Passing year.
    In that table i have more than thousands number of candidate. Here i want to make a search php form. Where i can get data of specific candidate who pass between specific year.

    I have make a form for search with below fields:
    Graduation Candidate search : Year From and Year To fields and submit button to call query form and display result.

    however i tried my end but not able to make such query into Mysql. Please help me make mysql query to display candidate as per my year criteria search.

    Jimi
    ---------------
    Here is first search.php code
    // search Graduation result

    Graduation candidate seach<BR>
    <form name="search1" action="graduation.php" method=post>
    From : <input type=text size="10" name=from value=""> <BR><BR>
    To : <Input type=text size="10" name=to value=""><BR><BR>
    <center><input type="submit" value="Submit"></center>
    </form>
    ---------------
    2) gaduation.php

    <?php

    $loginname = "";

    $password = "";

    $database = "";

    $con = mysql_connect("localhost", "$loginname", "$password") or die("could not connect");
    mysql_select_db("$database");

    $query = "SELECT * FROM candidate_foreign WHERE value BETWEEN" .$_post['from']. "AND '".$_post['to']."'";

    $result = mysql_query ("$query");

    echo"<table border=1><TH>Year</TH>";

    while ($in = mysql_fetch_array($result))
    {
    echo"<tr>";
    echo"<td>". $in['value'] . "<td>";
    echo"</tr>";
    }

    echo "</table>";

    mysql_close($con);
    ?>
    ---------------------
    Getting error as - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/graduation.php on line 18

    Please help me to get appropriate result from the query.

    Jimi
     
    jimjack145, Mar 21, 2007 IP
  2. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $query = "SELECT * FROM candidate_foreign WHERE `Graduation Passing year` BETWEEN" .$_post['from']. "AND '".$_post['to']."'";

    $query = "SELECT * FROM candidate_foreign WHERE `Post Graduation Passing year` BETWEEN" .$_post['from']. "AND '".$_post['to']."'";
     
    jitesh, Mar 21, 2007 IP
  3. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thks for your reply but getting an error as below:

    mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/graduation.php on line 18

    What i do now? and also i want to check data from "value" column...

    JIMI
     
    jimjack145, Mar 21, 2007 IP
  4. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $query = "SELECT * FROM candidate_foreign WHERE value BETWEEN<space>" .$_post['from']. "<space> AND<space> '".$_post['to']."'";
     
    jitesh, Mar 21, 2007 IP
  5. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    now i have following two pages and running without getting any error. But not giving me any result except column name as "year". Please check code of two php file below:
    -----------------------------------------
    1) search.php

    // search Graduation result

    Graduation candidate seach<BR>
    <form name="search1" action="graduation.php" method=post>
    From : <input type=text size="10" name=from value=""> <BR><BR>
    To : <Input type=text size="10" name=to value=""><BR><BR>
    <center><input type="submit" value="Submit"></center>
    </form>
    -----------------------------------------
    2) graduation.php
    <?php

    $loginname = "";

    $password = "";

    $database = "";

    $con = mysql_connect("localhost", "$loginname", "$password") or die("could not connect");
    mysql_select_db("$database");

    $query = "SELECT * FROM candidate_foreign WHERE value BETWEEN '" .$_post['from']. "' AND '".$_post['to']."'";

    $result = mysql_query ("$query");

    echo"<table border=1><TH>Year</TH>";

    if(mysql_num_rows($result)){
    while ($in = mysql_fetch_array($result))
    {
    echo"<tr>";
    echo"<td>". $in['value'] . "<td>";
    echo"</tr>";
    }
    }

    echo "</table>";

    mysql_close($con);

    ?>
     
    jimjack145, Mar 21, 2007 IP
  6. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think code is ok just prb with query
    Try this
    $query = "SELECT * FROM candidate_foreign WHERE value BETWEEN " .$_post['from']. " AND ".$_post['to'];
     
    jitesh, Mar 21, 2007 IP
  7. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    now getting an error like as below:
    mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/graduation.php on line 18

    May be now query is wrong. What u say?

    JIMI
     
    jimjack145, Mar 21, 2007 IP
  8. aditan22

    aditan22 Peon

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    try to check $_post['from'] and $_post['to'] before use them:

    echo 'from='.$_post['from'];
    echo 'to='.$_post['to'];

    what's the result?
     
    aditan22, Mar 21, 2007 IP
  9. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    write

    echo $query ; check what are getting then try to run this line in mysql(phpmyadmin or .....)
     
    jitesh, Mar 21, 2007 IP
  10. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I got blank result. Is there any wrong in my code.

    Jimi
     
    jimjack145, Mar 21, 2007 IP
  11. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I got blank result. Is there any wrong in my code?

    Jimi
     
    jimjack145, Mar 21, 2007 IP
  12. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    place the query in mysql and check what its results ....
     
    jitesh, Mar 21, 2007 IP
  13. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Without value reference from search form i run that graduation.php i got result . what would be the problem in my code?
    I run below query
    ---------------
    <?php

    $loginname = "";

    $password = "";

    $database = "";

    $con = mysql_connect("localhost", "$loginname", "$password") or die("could not connect");
    mysql_select_db("$database");

    $query = "SELECT * FROM candidate_foreign WHERE value BETWEEN 2000 and 2006;

    $result = mysql_query ("$query");

    echo"<table border=1><TH>Year</TH>";

    if(mysql_num_rows($result)){
    while ($in = mysql_fetch_array($result))
    {
    echo"<tr>";
    echo"<td>". $in['value'] . "<td>";
    echo"</tr>";
    }
    }

    echo "</table>";

    mysql_close($con);

    ?>
    --------------
    Jimi
    Just want to assign value of 2000 and 2006 from my search form.
     
    jimjack145, Mar 21, 2007 IP
  14. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Print these lines at front of graduation.php

    echo "<pre>";
    print_r($_POST);

    and check that are you getting ur post values ?
     
    jitesh, Mar 21, 2007 IP
  15. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #15
    $_post['from']. " AND ".$_post['to'];

    Capital
    $_POST['from']
    $_POST['to']
     
    jitesh, Mar 21, 2007 IP
  16. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    thks its work and giving me result now...

    thank you your urgent help :)

    Hai friend i have one more query regarding php. Would u like to help me in that.

    I have code with that i am paging my data. I just want to sort my data as per every column title. Do u have any idea how would i sort data through column name. I have mulitple column in my table.

    Hope you would help in this prob too..

    Regards,
    JIMI
     
    jimjack145, Mar 21, 2007 IP
  17. itrana123

    itrana123 Peon

    Messages:
    177
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    yes i think it is the easiest way to check where the problem is in the PHP code or in SQL query try this ...
     
    itrana123, Mar 21, 2007 IP
  18. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    I just inner join my table with other table and run below query now again its not giving me year. What wrong in query below:


    "SELECT `first_name`, `last_name`, `address1`, `address2`, `city`, `state`, `current_employer`, `entered_by`, `owner`, `email1` FROM candidate INNER JOIN candidate_foreign ON candidate.candidate_id = candidate_foreign.assoc_id WHERE candidate_foreign.field_name = 'IITYear' AND value >= '" .$_POST['from']. "' AND value <= '".$_POST['to']."'";
     
    jimjack145, Mar 21, 2007 IP
  19. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #19
    " .$_POST['from']. " AND value <= ".$_POST['to']


    Remove quates form $_POST['from'] and $_POST['to']
     
    jitesh, Mar 21, 2007 IP
  20. jimjack145

    jimjack145 Guest

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Once again i am here for need ur help...

    I tried mentioned code below at my end:

    --------
    <?php

    echo 'from='.$_POST['from'];
    echo 'to='.$_POST['to'];

    $loginname = "";

    $password = "";

    $database = "";

    $con = mysql_connect("localhost", "$loginname", "$password") or die("could not connect");
    mysql_select_db("$database");

    $query = "SELECT 'first_name', 'last_name', 'address1', 'address2', 'city', 'state', 'current_employer', 'entered_by', 'owner', 'email1' FROM candidate INNER JOIN candidate_foreign ON candidate.candidate_id = candidate_foreign.assoc_id WHERE candidate_foreign.field_name = 'IITYear' AND value >= '" .$_POST['from']. "' AND '".$_POST['to']."'";

    $result = mysql_query ("$query");

    echo"<table border=1><TH>First Name</TH><TH>Address</TH><TH>Last Name</TH><TH>City</TH><TH>State</TH><TH>Email</TH>";

    while ($in = mysql_fetch_array($result))
    {
    echo"<tr>";
    echo"<td>". $in['first_name'] . "<td>";
    echo"<td>". $in['address1'] . "<td>";
    echo"<td>". $in['last_name'] . "<td>";
    echo"</tr>";
    }


    echo "</table>";

    mysql_close($con);

    ?>
    -------
    When i run at my end with help of search.php . I have been not getting any error. but also i not getting any value from table too. I am getting error as below:
    ----------
    from=2006to=2009

    First Name Address Last Name City State Email
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    first_name address1 last_name
    --------------

    Please help as u did last day.

    JIMI
     
    jimjack145, Mar 21, 2007 IP