1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What's wrong with this code? Rep Boost!

Discussion in 'PHP' started by cgo85, Dec 9, 2008.

  1. #1
    not working... need to get field name from table (mod_merch) and then get a result by matching with the location in table (infusion).

    
    <?php
    //Get product ID
    $id = $_GET['aid'];
    
    //connect to db
    include 'redirect_dbconfig.php';
    include 'redirect_dbopen.php';
    
    //Get info about product from table
    $sql = "SELECT * 
    FROM mod_merch 
    WHERE prod1 = '$id' or prod2 = '$id' or prod3 = '$id' or prod4 = '$id' or prod5 = '$id' or prod6 = '$id'";
    $result = mysql_query($sql);
    $r = mysql_fetch_array($result);
    
    //Set a variable with the product name
    $first = $r['fname'];
    $last = $r['lname'];
    $fieldname = $r['mysql_field_name($id)'];
    
    //Search for a row with the product name
    $sql = "SELECT * FROM $fieldname.infusion WHERE fname = '$first' and lname = '$last'";
    $result = mysql_query($sql);
    $rs = mysql_fetch_array($result);
    
    //Print the $r variable
    print_r($rs);
    
    include 'redirect_dbclose.php';
    ?>
    
    PHP:
     
    cgo85, Dec 9, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Do you know where the problem is?

    I woud start by checking the output after the first query/result to determine where the problem is.

    Also, this script is completely open to SQL injection. You need to sanitise the GET variable to prevent someone from making an injection attack.

    Something as simple as:
    $id = (int)$_GET['aid'];
    will prevent it assuming that the aid is always a number.
     
    jestep, Dec 9, 2008 IP
  3. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    juust, Dec 9, 2008 IP
  4. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Juust, when replacing:

    $fieldname = $r['mysql_field_name($id)'];

    with:

    $fieldname=array_search($id, $r);

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 24
    2

    new code:

    <?php
    //Get product ID
    $id = (int)$_GET['aid'];
    
    //connect to db
    include 'redirect_dbconfig.php';
    include 'redirect_dbopen.php';
    
    //Get info about product from table
    $sql = "SELECT * 
    FROM mod_merch 
    WHERE prod1 = '$id' or prod2 = '$id' or prod3 = '$id' or prod4 = '$id' or prod5 = '$id' or prod6 = '$id'";
    $result = mysql_query($sql);
    $r = mysql_fetch_array($result);
    
    //Set a variable with the product name
    $first = $r['fname'];
    $last = $r['lname'];
    $fieldname=array_search($id, $r);
    
    //Search for a row with the product name
    $sql = "SELECT * FROM $fieldname.infusion WHERE fname = '$first' and lname = '$last'";
    $result = mysql_query($sql);
    $rs = mysql_fetch_array($result);
    
    //Print the $r variable
    print_r($rs);
    echo "$fieldname";
    
    include 'redirect_dbclose.php';
    ?>
    PHP:
     
    cgo85, Dec 9, 2008 IP
  5. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php
    //Get product ID
    $id = (int)$_GET['aid'];
    
    //connect to db
    include 'redirect_dbconfig.php';
    include 'redirect_dbopen.php';
    
    //Get info about product from table
    $sql = "SELECT * 
    FROM mod_merch 
    WHERE prod1 = '$id' or prod2 = '$id' or prod3 = '$id' or prod4 = '$id' or prod5 = '$id' or prod6 = '$id'";
    $result = mysql_query($sql);
    $r = mysql_fetch_array($result);
    
    //Set a variable with the product name
    $first = $r['fname'];
    $last = $r['lname'];
    $fieldname = mysql_field_name($result, 0);
    
    //Search for a row with the product name
    $sql = "SELECT * FROM $fieldname.infusion WHERE fname = '$first' and lname = '$last'";
    $result = mysql_query($sql);
    
    echo "$fieldname";
    
    include 'redirect_dbclose.php';
    ?>
    PHP:
    How can I manipulate: $fieldname = mysql_field_name($result, 0);

    To be the field in which the 'id' is actually found?
     
    cgo85, Dec 9, 2008 IP
  6. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <?php
    //Get product ID
    $id = (int)$_GET['aid'];
    
    //connect to db
    include 'redirect_dbconfig.php';
    include 'redirect_dbopen.php';
    
    //Get info about product from table
    $sql = "SELECT * 
    FROM mod_merch 
    WHERE prod1 = '$id' or prod2 = '$id' or prod3 = '$id' or prod4 = '$id' or prod5 = '$id' or prod6 = '$id'";
    $result = mysql_query($sql);
    $r = mysql_fetch_array($result);
    
    //Set a variable with the product name
    $first = $r['fname'];
    $last = $r['lname'];
    $fieldnum=array_search($id, $r);
    $fieldname = mysql_field_name($result, $fieldnum);
    
    //Find value for where name & field meet
    $sql = "SELECT * FROM $fieldname.infusion WHERE fname = '$first' and lname = '$last'";
    $result = mysql_query($sql);
    
    include 'redirect_dbclose.php';
    ?>
    PHP:
     
    cgo85, Dec 9, 2008 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    
    //Search for a row with the product name
    $sql = "SELECT * FROM $fieldname.infusion WHERE fname = '$first' and lname = '$last'";
    $result = mysql_query($sql);
    
    $output = mysql_fetch_array($result);
    
    print_r($output);
    
    
    PHP:
    If there are multiple results returned you will need to loop through them.

    
    //Search for a row with the product name
    $sql = "SELECT * FROM $fieldname.infusion WHERE fname = '$first' and lname = '$last'";
    $result = mysql_query($sql);
    
    while($output = mysql_fetch_array($result)):
    
    print_r($output);
    
    endwhile;
    
    PHP:
     
    jestep, Dec 9, 2008 IP
  8. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Actually, there will only be one result. But what you did gave me this:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 26

    did i structure: $fieldname.infusion correctly? Trying to tell it to only get result within that field

    I really appreciate your help jestep!
     
    cgo85, Dec 9, 2008 IP
  9. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    edit:(damn I am too slow for you folk)

    about mysql_field_name($id) : that counts the columns and returns the name of column($id), if you use $id=0 it returns the first column of the table.

    array_search($id, $r) picks the first key from array $r where value=$id.

    if mysql_fetch_array() returns a numeric ([1] => value) array
    it can be forced to return an associative array ([fieldname] => value)
    by specifiying mysql_fetch_array($sql, MYSQL_ASSOC)

    
    $r = mysql_fetch_array($sql, MYSQL_ASSOC);
    $first = $r['fname'];
    $last = $r['lname'];
    $fieldname = array_search($id, $r);
    
    PHP:
    that should work ?
     
    juust, Dec 9, 2008 IP
    cgo85 likes this.
  10. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Actually, that part works fine... if i do an echo "$fieldname" it outputs the correct field name. I'm having a problem with second part. Where I need to output the value where that field MEETS the fname/lname.


    Basically I just want a echo "$result";... but that's not working
     
    cgo85, Dec 9, 2008 IP
  11. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #11
    You will need to debug the query. That error means that the query failed.

    if(!$result = mysql_query($sql)):

    die(mysql_error());

    endif;
     
    jestep, Dec 9, 2008 IP
    cgo85 likes this.
  12. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #12
    man this sucks! That doesn't seem to work... it seems like it'd be so easy to just get the output from that query.
     
    cgo85, Dec 9, 2008 IP
  13. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Check it out... I think I got it

    <?php
    //Get product ID
    $id = (int)$_GET['aid'];
    
    //connect to db
    include 'redirect_dbconfig.php';
    include 'redirect_dbopen.php';
    
    //Get info about product from table
    $sql = "SELECT * 
    FROM mod_merch 
    WHERE prod1 = '$id' or prod2 = '$id' or prod3 = '$id' or prod4 = '$id' or prod5 = '$id' or prod6 = '$id'";
    $result = mysql_query($sql);
    $r = mysql_fetch_array($result);
    
    //Set a variable with the product name
    $first = $r['fname'];
    $last = $r['lname'];
    $fieldnum=array_search($id, $r);
    $fieldname = mysql_field_name($result, $fieldnum);
    
    //Find value for where name & field meet
    $sql = "SELECT $fieldname FROM infusion WHERE fname = '$first' and lname = '$last'";
    $result = mysql_query($sql);
    $rs = mysql_fetch_array($result);
    $link = $rs["$fieldname"];
    echo "$link";
    
    include 'redirect_dbclose.php';
    ?>
    PHP:
     
    cgo85, Dec 9, 2008 IP
  14. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #14
    correct me where I go wrong :
    * pick firstname/lastname + specific productfield
    from mod_merch
    where any of the product-fields contains the id
    * pick records from infusion where firstname and lastname match
    * echo the value in the corresponding productfield (prod1...prod6)

    in that case :
    
    $result = mysql_query("SELECT $fieldname FROM infusion WHERE fname = '$first' and lname = '$last'");
    while($output = mysql_fetch_assoc($result)) {
     echo $output[$fieldname];
    }
    
    PHP:
    should do it ?
     
    juust, Dec 9, 2008 IP