SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Conditional IF statement not working

    Hey,

    I cant seem to work out why this if statement does not work. Basically i have populated a dropdown box via a select statement. The default field of the drop down is "Select Category"..

    Now i dont want users to be able to select this category as it does not mean anything, therefore i am trying to use the following code:-

    PHP Code:
      if (isset($_REQUEST['added_by'])) 
     {       
         
    $description mysql_escape_string($description);

         if(
    $role_id == 0)
         {
              echo 
    "Please select a category";
         }
         else
         {
               foreach (
    $_POST as $key=>$value)
              {
                ${
    $key} = addslashes(trim($value));       
              }     
              
              
    $sql="INSERT INTO recruitment_jobs
                    (location_town_city, location_region, location_country, role_id, vacancies, contract, salary_low, salary_high, added_by, uploader_id, description, date_added)
                    VALUES
                    ('
    $location_town_city', '$location_region', '$location_country', '$role_id', '$vacancies', '$contract', '$salary_low', '$salary_high', '$added_by', '$uploader_id', '$description', NOW());";
                   
              
    $mysql_insert mysql_query($sql) or trigger_error(mysql_error() . "<p>$sql</p>"E_USER_ERROR);
              
    $_SESSION['pigeonhole']='Addition successful';
              
    $relocate='index.php';
              
    $job_id=mysql_insert_id();
         }
      } 
    The code above keeps giving me the echo "Please select a category", but does not do the INSERT..

    Can anyone please help

    Regards

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, it's not possible for it to give you "Please select a category" AND perform the insert. They're in the if and else clauses of a conditional, which can be either true or false, not both.

    It sounds like $role_id, whatever that is (it's not set anywhere in this code), is always 0.

    Are you by chance relying on these variables all being created for you via register_globals? That's not just bad practice, but downright dangerous since it's disabled by default. Even if your web host has it enabled now (which is bad), they could upgrade PHP and break your whole site overnight.

  3. #3
    Non-Member thewebhostingdir's Avatar
    Join Date
    Oct 2005
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are you setting in the $role_id variable? echo "Please select a category"; will only fire if IF part is true. Hence, there must be some problem in assigning value to the $role_id variable. Please confirm.

  4. #4
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I understand that it will ONLY do one or the other not both, but what i am saying is if i choose a category that is NOT '0' then it does not do the insert when it should.

    Role id is set further down the page:-

    PHP Code:
                <select name="role_id" class="required addjob-input" id="role_id">
                <?
                  $roles_result 
    mysql_query('SELECT * FROM recruitment_roles ORDER BY ranking, role_id;');
                  while (
    $row=mysql_fetch_array($roles_result))
                  {
                    
    $selected=$row['role_id']==$role_id ' selected="selected"' '';
                    echo 
    "<option value='".$row['role_id']."' $selected>".$row['role_name']."</option>";
                  } 
    // while
                
    ?>
                </select>
    So all i am trying to do is prevent the code to work if the first category is selected..

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Are you sure register_globals is enabled to populate $role_id for you?

    You should be accessing a posted form through $_POST['role_id']

  6. #6
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks guys, i fixed the problem out and managed to do the following:-

    PHP Code:
      if ($_REQUEST['added_by'] && $_POST['role_id'] != '0'
    And added an 'option' before getting the values from the database..

    PHP Code:
                <select name="role_id" class="required addjob-input" id="role_id">
                <option value="0">[Please choose a job category]</option>
                <?
                  $roles_result 
    mysql_query('SELECT * FROM recruitment_roles ORDER BY ranking, role_id;');
                  while (
    $row=mysql_fetch_array($roles_result))
                  {
                    
    $selected=$row['role_id']==$role_id ' selected="selected"' '';
                    echo 
    "<option value='".$row['role_id']."' $selected>".$row['role_name']."</option>";
                  } 
    // while
                
    ?>
                </select>
    Regards


Bookmarks

Posting Permissions

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