SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Aug 10, 2009, 03:45 #1
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();
}
}
Can anyone please help
Regards
-
Aug 10, 2009, 03:50 #2
- 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.Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Aug 10, 2009, 03:52 #3
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.
-
Aug 10, 2009, 03:57 #4
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>
-
Aug 10, 2009, 04:44 #5
- 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']Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Aug 10, 2009, 05:50 #6
Hey,
Thanks guys, i fixed the problem out and managed to do the following:-
PHP Code:if ($_REQUEST['added_by'] && $_POST['role_id'] != '0')
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>
Bookmarks