Jump to content

Else If not working...


justapilot

Recommended Posts

First.. I am new to PHP and teaching myself so be gentle :)

 

I have a page that pulls info from a database via includes which just contain a select statement.

 

The URL contains a variable. Depending on which variable is set, depends on which include to use. The code is below, but it seems to ignore the elseif statements and go just used the include at the end of the else statement. I am sure my syntax is wrong but I cannot figure it out.. please help :) A side note.. the first IF statement works..

 

Thanks for your help!

 

David

 

 

<?php if(isset($_POST['companyname'])):// job detail has been added

 

 

 

$companyname = $_POST['companyname'];

$address = $_POST['address'];

$contact = $_POST ['contact'];

$phone = $_POST['phone'];

$email = $_POST ['email'];

$lastcdate = $_POST['lastcdate'];

$positionapplied = $_POST['positionapplied'];

$results = $_POST ['results'];

$sql = "INSERT INTO jobs SET

dateapplied=CURDATE(),

companyname='$companyname',

address='$address',

contact='$contact',

phone='$phone',

email='$email',

lastcdate=CURDATE(),

positionapplied='$positionapplied',

results='$results'";

if(@mysql_query($sql)) {

echo "<p> $companyname has added to the database.</p>";

}else{

echo "<p>Error adding $companyname to the database!</p>" .mysql_error() . " </p>";

}

 

 

include 'includes/pulljobs.php';

 

elseif (isset($_REQUEST['cdate'])):

 

include 'includes/pulljobscdate.php';

 

elseif (isset($_REQUEST['company'])):

 

include 'includes/pulljobscompany.php';

 

else: include 'includes/pulljobs.php';

 

endif;

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/163538-else-if-not-working/
Share on other sites

You need to start it with an if statement:

 

include 'includes/pulljobs.php';

if (isset($_REQUEST['cdate']))

    include 'includes/pulljobscdate.php';

elseif (isset($_REQUEST['company']))

    include 'includes/pulljobscompany.php';

else
    include 'includes/pulljobs.php';

Link to comment
https://forums.phpfreaks.com/topic/163538-else-if-not-working/#findComment-862843
Share on other sites

I strongly recommend that you not use if():, else:, or endif;, especially since you use the much clearer if() {} else {} at other points in your code.  stick with the {} version.  I'm pretty sure the way you have it set up right now that the opening if only affects the $companyname=$_POST['companyname']; line, a bit like doing

 

if(isset($_POST['companyname']))
  $companyname = $_POST['companyname'];

 

which is the same as

 

if(isset($_POST['companyname'])) {
  $companyname = $_POST['companyname'];
}

 

Link to comment
https://forums.phpfreaks.com/topic/163538-else-if-not-working/#findComment-862844
Share on other sites

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.