Jump to content

If then statement not passing variables


anthonydamasco

Recommended Posts

I seem to be having trouble throwing a var into a database

$branchemail is the var not being pushed
i have an if/then statement

[code=php:0]
if($nearestlocation =='cherryhill'){
$branchemail ="[email protected]";
$branchphone ="856-482-2222";
}
elseif ($nearestlocation =='pennsauken'){
$branchemail ="[email protected]";
$branchphone ="856-662-2727";
}
    elseif($nearestlocation =='burlington'){
$branchemail ="[email protected]";
$branchphone ="609-387-2900";
}
elseif($nearestlocation =='woodburyheights'){
$branchemail ="[email protected]";
$branchphone ="856-845-3900";
}
elseif($nearestlocation =='atlanticcity'){
$branchemail ="[email protected]";
$branchphone ="609-344-1300";
}
elseif($nearestlocation =='philadelphia'){
$branchemail ="[email protected]";
$branchphone ="215-568-2228";
}
elseif($nearestlocation =='vineland'){
$branchemail ="[email protected]";
$branchphone ="856-794-8282";
}
[/code]

and then i have my query

[code=php:0]
$sql = "INSERT INTO jobsearch VALUES (NULL, '$positiontitle', '$city', '$hourlyrate', '$workinghours', '$positiondescription', '$positiontype', '$nearestlocation', '$timeneeded', '$positionclassification', SYSDATE(), '$expire', '$branchemail', '$branchphone')";
mysql_query($sql) or die ( "Problem with the query: $sql<br>" . mysql_error() );
}
[/code]

everything but $branchemail goes to my database! any ideas, Ive looked at this script for ever, I just dont see it!
Link to comment
https://forums.phpfreaks.com/topic/30668-if-then-statement-not-passing-variables/
Share on other sites

Change your if to a switch()

[code]
<?php

switch($nearestlocation) {
  case 'cherryhill':
    $branchemail ="[email protected]";
    $branchphone ="856-482-2222";   
    break;
  case 'pennsauken':
    $branchemail ="[email protected]";
    $branchphone ="856-662-2727"
    break;
  case 'burlington':
    $branchemail ="[email protected]";
    $branchphone ="609-387-2900";
    break;
  case 'woodburyheights':
    $branchemail ="[email protected]";
    $branchphone ="856-845-3900";
    break;
  case 'atlanticcity' :
    $branchemail ="[email protected]";
    $branchphone ="609-344-1300";
    break;
  case 'philadelphia':
    $branchemail ="[email protected]";
    $branchphone ="215-568-2228";
    break;
  case 'vineland':
    $branchemail ="[email protected]";
    $branchphone ="856-794-8282";
    break;
}

?>
[/code]

See if that helps
A switch is designed to specify different actions based upon a particular value - exactly what you have. Doing a multiple of "if else" statements is inefficient.

You need to verify that $nearestlocation is what you are testing for. Are you sure that the case of the letters in your test is the same as in the value of $nearestlocation?

"philadelphia" != "Philadelphia"

It's probably a good idea to change the switch statement to this:
[code]<?php
switch(strtolower($nearestlocation)) {
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.