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

Thread: query empty

  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts

    query empty

    I'm struggling with a query ifelse statement. First, it seems like there's got to be a much simpler way to phrase it. And I can't get it to work. I get "query empty" as a result?

    the page is a series of conditional dropdowns. The user selects continents, and the next table populates with regions, and so on. But if they hit submit after selecting only the first or second of the 4 dropdowns, I want it to still enter what they selected into the table.

    PHP Code:
     if( isset($_POST['Submit']) )
          {

          if( isset(
    $_POST['States']) )
          {
    $descriptor1 trim($_GET['id']);
    $descriptor2 trim($_POST['States']);
    $descriptor3 trim($_POST['continent_regions']);
    $descriptor4 trim($_POST['Continents']);
    $descriptor5 trim($_POST['Country']);

    $query "INSERT INTO table(id,state, continent_regions,continents,country) VALUES ($descriptor1,$descriptor2,$descriptor3,$descriptor4,$descriptor5)";

    } elseif( isset(
    $_POST['Country']) )
          { 
    $descriptor1 trim($_GET['id']);
    $descriptor3 trim($_POST['continent_regions']);
    $descriptor4 trim($_POST['Continents']);
    $descriptor5 trim($_POST['Country']);

    $query "INSERT INTO table(id,continent_regions,continents,country) VALUES ($descriptor2,$descriptor3,$descriptor4,$descriptor5)";

    } elseif( isset(
    $_POST['continent_regions']) )
          { 
    $descriptor1 trim($_GET['id']);
    $descriptor3 trim($_POST['continent_regions']);
    $descriptor4 trim($_POST['Continents']);

    $query "INSERT INTO table(id, continent_regions,continents) VALUES ($descriptor1,$descriptor3,$descriptor4)";

    } elseif( isset(
    $_POST['continent']) )
          {
    $descriptor1 trim($_GET['id']);
    $descriptor4 trim($_POST['Continents']);

    $query "INSERT INTO table(id,continents) VALUES ($descriptor1,$descriptor4)";

    }


    echo 
    $query;

    $result = @mysql_query ($query) or die(mysql_error());  


    if (
    mysql_affected_rows() == 1) {
        echo 
    '<div style="position:relative; left:16px; top:226px;"><font color="red">Your plant location has been added!</font></div>';
        } 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I wouldn't waste my time with all of these if/else checks. Since the record needs all of these fields, that would indicate that any of these can be null, so that's not an issue in regards to building it. A simple:
    PHP Code:
    $descriptor1 = isset($_GET['id']) ? (int)$_GET['id'] : null;
    $descriptor2 = isset($_POST['States']) ? (int)$_POST['state'] : null;
    $descriptor3 = isset($_POST['continent_regions'] ? (int)$_POST['continent_regions'] : null;
    $descriptor4 = isset($_POST['Continents']) ? (int)$_POST['Continents'] : null;
    $descriptor5 = isset($_POST['Country']) ? (int)$_POST['Country'] : null
    I don't know if that's right though. I see you have trimmed these off first, but they are clearly numbers going into the database via the insert calls.

    As for the 'query empty', where is that? I don't see any indication for that output at all.

  • Users who have thanked Fou-Lu for this post:

    turpentyne (11-09-2011)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Thank you, thank you! Ternary operaters! I learned something new today!

    I wish I could answer the "query empty" question, but your snippet solved the problem, so now i'll never know what the heck that was!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by turpentyne View Post
    Thank you, thank you! Ternary operaters! I learned something new today!

    I wish I could answer the "query empty" question, but your snippet solved the problem, so now i'll never know what the heck that was!
    Was it an actual error? I assumed it was a print out from the page somewhere :/
    Alls well that ends well I guess


  •  

    Posting Permissions

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