...

View Full Version : query empty



turpentyne
11-09-2011, 05:49 PM
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.


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>';
}

Fou-Lu
11-09-2011, 06:01 PM
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:


$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.

turpentyne
11-09-2011, 10:52 PM
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!

Fou-Lu
11-09-2011, 11:45 PM
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 :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum