views:

69

answers:

2

I am struggling to find out the syntactically correct way in which to add on more variables and rows to these statements:

/* WANT TO ADD ON FIVE MORE $_POST[''] */

if(isset($_POST['check_prof']) && $_POST['check_prof'] == 'checked') {
$check_prof = "checked";
}else{
$check_prof = "unchecked";
} 

/* SAME HERE, WANT TO ADD THE OTHER FIVE IN HERE AS WELL */

$query = "UPDATE `Users` SET `check_prof` = '" . $check_prof . "' WHERE `id` = '" . $auth->id . "' LIMIT 1";
    mysql_query($query,$connection);
    $auth->refresh();
    }
A: 

Assuming I'm understanding what you mean, try this:

<?php
$arr = array('check_prof' => 'unchecked',
            'check_student' => 'unchecked');
foreach($arr as $field => $checked) {
    if (isset($_POST[$field]) && $_POST[$field] == 'checked') {
        $arr[$field] = 'checked';
    }
    // I don't know where $auth is coming from
    $query = "UPDATE `Users` SET `$field` = '" . $arr[$field] . "' WHERE `id` = '" . $auth->id . "' LIMIT 1";
    mysql_query($query,$connection);
    $auth->refresh();
}
?>

You would simply need to add fields as needed to $arr for it to loop through.

Slokun
I just want to add on multiples...if(isset($_POST['check_prof']) }else{$check_prof = "unchecked";} f(isset($_POST['check_blog']) }else{$check_blog = "unchecked";} but don't know the proper syntax to add them on to the same statement, just want to add on multiple variables and post values etc
Ralph The Mouf
another words, what you see in the original question is just for check_prof, but want to add on check_blog,check_question etc... to the same statment
Ralph The Mouf
+1  A: 
Syntax Error
yes exactly! Thank you!
Ralph The Mouf