Variable Passing Problem in Mysql Query

Discussion in 'PHP' started by nile1483, Jan 18, 2008.

  1. #1
    Hello Friends,

    I just take a simple form with one select option and one text box i am trying to pass value through variable there are nothing result

    <form action="employee2.php" method="post" >
    Search by

    <select name="select">
    <option value="employee_id">Employee Id</option>
    <option value="employee_name">employee_name</option>
    </select>

    Enter :
    <input type="text" name="employee_name2" />
    <input name="submit" type="submit" value="Search" />

    </form>


    if(isset($HTTP_POST_VARS["submit"]))
    {

    $employee_id_temp = $HTTP_POST_VARS["select"];
    $emp_name = $HTTP_POST_VARS["employee_name2"];


    }


    and query

    $query = "SELECT * FROM emp_detail WHERE '$employee_id_temp' = '$emp_name'" or die(mysql_error());


    or i also try with

    $query = "SELECT * FROM emp_detail WHERE '".$employee_id_temp."' = '".$emp_name."' " or die(mysql_error());

    There are no any result, if you have any idea tell me
     
    nile1483, Jan 18, 2008 IP
  2. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. use $_POST instead of $HTTP_POST_VARS

    2. don't name your select box "select" - that's making things confusing

    3. are you doing anything with your query besides creating a text string? As it stands, your query doesn't actually do anything. try this:

    
    // add code to connect to your db here
    
    $query = "SELECT * FROM emp_detail WHERE " . $employee_id_temp . " = '" . $emp_name . "'";
    $res = mysql_query($query) or die($query . "<BR><BR>" . mysql_error());
    while ($row = mysql_fetch_assoc($res))
    {
        //do something with the row data, like:
        echo $row[$employee_id_temp];
    }
    
    Code (markup):
     
    james_r, Jan 18, 2008 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    4. Make sure you always sanitise and escape any strings that come from the user before you use them in a database query. Otherwise people can mess with your database in all sorts of awful ways.
     
    SmallPotatoes, Jan 18, 2008 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    are you still facing problem

    Regards

    Alex
     
    kmap, Jan 19, 2008 IP