Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing an ID field through to PHP

    I think this is a PHP question, and not MySQL?

    I have a MySQL database which I'm searching via PHP, and it produces a list perfectly well.

    I want to be able to click on a button on any of the found records in the list, to go to a detail page for that record.

    I have a problem:

    The ID field I'm passing as a hidden field is always from the last record in the list, regardless of which record's button I click. I imgaine this is because by the time the button is pressed, the ID field has been incremented? But how do I get round it please?

    Many thanks in advance. :-)
    iBook G4/1.2 12" / OS 10.4.8
    PHP 5.1.6-2
    MySQL 4.1.21
    phpMyAdmin 2.9.0.3

  • #2
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, it's probably because you increment your number and echo it after going through all the records. If you post your code here i will change it so it returns the correct id.

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much.

    I know I'm not doing anything with the ID yet, apart from printing it, but I will put that code in once I know I'm passing the right data. Then I will test for the ID field, and if it's present, will make the detail page.

    PHP Code:
    <?php

    $key
    =$_POST['key'];
    echo 
    $key '<br><br>';

    $num=0;
    $user="xxxxxxxx";
    $password="xxxxxxxx";
    $database="xxxxxxxx";

    $search_company=$_POST['company'];

    $search_classification=$_POST['classification'];
    mysql_connect(localhost,$user,$password);
    @
    mysql_select_db($database) or die("Unable to select database");


    // get data - classification only (clicked link from results not found list)
    if ((isset($_GET['classification'])) && ($_GET['classification']!=''))
    {

        
    $search_classification=$_GET['classification'];

        
    mysql_connect(localhost,$user,$password);
        @
    mysql_select_db($database) or die("Unable to select database");

        
    $query="SELECT * FROM contacts WHERE (classification='$search_classification') ORDER BY company ASC";
        
    $result=mysql_query($query);
        
    $num=mysql_numrows($result);
        
    mysql_close();
    }
    else

    // post data
    {
        
    // both classification and company
        
    if (($search_classification != "") && ($search_company != ""))
            {
                
    $query="SELECT * FROM contacts WHERE (company LIKE '$search_company%') or (classification LIKE '$search_classification%') ORDER BY company ASC";
            }

        
    // classification only
        
    if (($search_classification != "") && ($search_company == ""))
        {
            
    $query="SELECT * FROM contacts WHERE (classification LIKE '$search_classification%') ORDER BY company ASC";
        }

        
    // company only
        
    if (($search_classification == "") && ($search_company != ""))
        {
        
    $query="SELECT * FROM contacts WHERE (company LIKE '$search_company%') ORDER BY company ASC";
        }

        
    // run the query
        
    $result=mysql_query($query);

        
    $num=mysql_numrows($result);
        
    mysql_close();
    }

    // results found
    if ($num!==0)
    {
        
    // results header...
        
    echo '<h2>' $num ' results for ';

        
    // ...both classification and company
        
    if (($search_classification != "") && ($search_company != ""))
        {echo 
    'classification  "' $search_classification '" and company  "' $search_company '" :</h2>';}

        
    // ...classification only
        
    if (($search_classification != "") && ($search_company == ""))
        {echo 
    'classification "' $search_classification '" :</h2>';}

        
    // ...company only
        
    if (($search_classification == "") && ($search_company != ""))
        {echo 
    'company "' $search_company '" :</h2>';}

        
    // results list
        
    echo '<hr>';
        
    $i=0;
        while (
    $i $num
        {

            
    // get the results
            
    $id=mysql_result($result,$i,"contactID");
            
    $company=mysql_result($result,$i,"company");

            
    $address=mysql_result($result,$i,"address");
            
    $phone=mysql_result($result,$i,"phone");
            
    $classification=mysql_result($result,$i,"classification");
            
    $website=mysql_result($result,$i,"website");


            
    // display the results
            
    echo
    'id: ' $id '<br>
    company: ' 
    $company '<br>
    address: ' 
    $address '<br>
    phone: ' 
    $phone '<br>
    classification: ' 
    $classification '<br>
    <a href="http://' 
    $website '">http://' $website '</a><br><br>
    open within frame:<br>
    <a href="frame.php?website=' 
    $website .'" target="_blank">' $website '</a><br><br>
    <input type="Submit" value="details">
    <input type="hidden" name="key" value="' 
    $id '"><br>
    <hr>
            '
    ;
            
    $i++;
        }
    }
    else 
    {
        
    // no results found
        
    echo
    '<h2>Sorry! No Results Found. Please Select from one of the following Classifications</h2>

    <a href="results.php?classification=supermarket">Supermarket</a><br>
    <a href="results.php?classification=department store">Department Store<br></a>
    <a href="results.php?classification=designers">Designers</a>
        '
    ;
    }
    ?>
    iBook G4/1.2 12" / OS 10.4.8
    PHP 5.1.6-2
    MySQL 4.1.21
    phpMyAdmin 2.9.0.3

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's okay, I've got it!

    Someone on another forum told me I needed separate forms for each button, and it works brilliantly.

    Thanks for reading my question :-)
    iBook G4/1.2 12" / OS 10.4.8
    PHP 5.1.6-2
    MySQL 4.1.21
    phpMyAdmin 2.9.0.3


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •