help needed with query.

Discussion in 'PHP' started by SnoozZz, Jun 20, 2008.

  1. #1
    I have 2 tables called 'currency' and 'content'

    currency = cur_id, cur_country, cur_code
    example= 1,USA,USD

    content = id, name, email, country, currency


    Then I have a form with: name, email, country

    on submit insert into 'content' - $name, $email, $country, $currency

    I need a query to automatically insert row 'cur_code' from table currency into row 'currency' of table content according to the country selected

    I hope I explained this well enough.
     
    SnoozZz, Jun 20, 2008 IP
  2. wowla_123

    wowla_123 Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In the form where you are getting the input, populate that form from the table "currency", so that the value should contain country and curreny. For example:

    <select name="select" id="select">
        <option value="USA-USD">USA</option>
        <option value="UK-GBP">UK</option>
      </select>
    HTML:
    For example when the user selects USA, you will get the value "USA-USD". Using split() function, you will get an array in which the 1st element of array will have country name and the 2nd element will have currency. Now you can insert the currency code in the content table
     
    wowla_123, Jun 20, 2008 IP
  3. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanx, i'm getting somewhere now...

    If I did this...

    <?php
    $sql = "SELECT * FROM currency order by cur_country ASC";
    $result = mysql_query($sql);
    while ($record = mysql_fetch_object($result)) {

    ?>


    <option value="<?php echo "$record->cur_country";?>-<?php echo "$record->cur_code";?>"><?php echo "$record->cur_country";?></option>


    <?php
    }
    ?>

    <option value="Other">Other</option>

    </select>



    How would I then split it?
     
    SnoozZz, Jun 20, 2008 IP
  4. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    this works in MS SQL, how to make it work in MySql

    INSERT INTO content (country, currency) VALUES ('$country', (SELECT cur_code FROM cur_country WHERE cur_country = '$country'))
     
    SnoozZz, Jun 20, 2008 IP
  5. SnoozZz

    SnoozZz Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I got it thanx

    <?php
    $country = "$record->country";
    list($country, $currency) = split('[/.-]', $country);
    echo "Country: $country<br />\n";
    ?>
     
    SnoozZz, Jun 20, 2008 IP