SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    21
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to receive info from 2 tables from the same database in the same page?

    I have 1 big form that will receive information from 2 different tables (table1 and table2) in the same database.

    All info will be placed in the same form where you can save it all to a new table.

    My issue is when some of the row in both table (table1 and table2) have the same name how do I call the info from the correct table. So fare I have:

    1.
    The URL that will call the info looks like:
    PHP Code:
    .info.php?id=320&folder=ras 

    2.
    The query:

    PHP Code:
    if(!$_POST["submit"])
    {
    //INFO FORM TABLE 1
    $query mysql_query("select country, state, name, dob from table1 where id = '".$_GET["id"]."' ");
    $row mysql_fetch_row($query);
    $country $row[0];
    $state $row[1];
    $name $row[2];
    $dob $row[3];

    //INFO FORM TABLE 2
    $query mysql_query("select country, state, name, dob from table2 where folder = '".$_GET["folder"]."' ");
    $row mysql_fetch_row($query);
    $country $row[0];
    $state $row[1];
    $name $row[2];
    $dob $row[3]; 

    3.
    The vaiable on the website:
    PHP Code:
    From table 1: //1 <?=$country?> //2 <?=$state?> //3 <?=$name?> //4 <?=$dob?> 
    From table 2: //1 <?=$country?> //2 <?=$state?> //3 <?=$name?> //4 <?=$dob?>

    Im not sure how to do this, but I think that I need to change one of the query to something like:
    PHP Code:
    $query2 mysql_query("select country, state, name, dob from table2 where folder = '".$_GET["folder"]."' ");
    $row mysql_fetch_row($query2);
    $country $row[0];
    $state $row[1];
    $name $row[2];
    $dob $row[3]; 

    But if I do that what variable should I use to call the info in table2?
    PHP Code:
    <?=$2”country”?>
    don’t work.

    Please advice.

  2. #2
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    21
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I found the selution my self:

    //INFO FORM TABLE 2
    $query = mysql_query("select country, state, name, dob from table2 where folder = '".$_GET["folder"]."' ");
    $row = mysql_fetch_row($query);
    $country2 = $row[0];
    $state2 = $row[1];
    $name2 = $row[2];
    $dob2 = $row[3];
    And in the html code use: <?=$country2?>

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    409
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind that there is no need to use variable names that match the column names in your database, it just makes it easier to figure out what information is being worked with. So any other variable name will do as long as you're consistent where you use it. Just as you've chosen to suffix with a digit, you could also have chosen to include the table name, or anything else that helps. In fact using 'id_country' and 'folder_country' might make things clearer. Doesn't matter, though, as long as they're different if they need to be.

    Also if you're learning about PHP as I am, have a look at using PDO or mysqli libraries to access your database rather than the older mysql statements - apparently many hosting companies are starting to discontinue support for the older mysql statements and it might be easier to work through now with one of the newer access methods, rather than get used to the 'old' way.
    http://www.firenza.net - my homage to a car from the 1970s


Tags for this Thread

Bookmarks

Posting Permissions

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