Forum Moderators: coopster

Message Too Old, No Replies

Conditional statement not being obeyed!

         

SaminOz

1:10 am on Jan 3, 2010 (gmt 0)

10+ Year Member



I have the following conditional statement which checks if an ID already exists and if not calls a function to create it:

if(!isset($job_ID)) {
$job_ID = create_job_ID();
}

I have set it variously as if($job_ID == 0) and if($job_ID < 1) but the function still executes each time and increments the job_ID which is set, is an integer and indisputably exists!

The called function operates thus:

function create_job_ID()
{
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die('Unable to connect to the server. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));

$query = 'INSERT IGNORE INTO ww_job (job_ID)
VALUES (NULL)';
mysql_query($query, $db) or die(mysql_error($db));

$job = mysql_insert_id($db);

return $job;
}

Hoping someone can tell me where I'm going wrong - regards Sam.

rocknbil

6:37 am on Jan 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where does $job_ID initially get set? If this is near the top of a script, you may want to set/get it from $_SESSION . . .

Is this conditional inside a function?

function some_func() {

global $job_ID; // may be the problem

if(!isset($job_ID)) {
$job_ID = create_job_ID();
}
.....

Better yet, if this is the case,

$verified_job_id = some_func($job_ID);
//
//
function some_func($id) {

if(!isset($id)) {
$id = create_job_ID();
}
//.....

return $id;
}

Personally in these cases if I expect a number, this does the trick,

if (! ($job_ID > 0)) {
.....

But any of yours should have too. I'm voting "inside a function . . . "

SaminOz

2:17 pm on Jan 3, 2010 (gmt 0)

10+ Year Member



Thanks for your help. I settled for this:

if(!isset($_POST['job_ID'])) {
$_SESSION['job_ID'] = create_job_ID();
}

this means a job_ID is created for retrieving from the db but not destroyed each time the page submits back to itself (building a list of choices and echoing them out for the user to see).

I guess I'll have to clear the session variable within the session later if I want the user to be able to create two jobs in a single session.