bugzy Posted April 13, 2012 Share Posted April 13, 2012 I have this if statement with validations and mysql query <?php if(isset($_POST['submit'])) { $errors = array(); $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { $errors[] = $fieldname; } } $fields_with_length = array('menu_name' => 30); foreach($fields_with_length as $fieldname => $maxlength) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } } if(empty($errors)) { //Perform Update $id = mysql_prep($_GET['subj']); $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); $query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible} WHERE id = {$id}"; $result = mysql_query($query, $connection); if(mysql_affected_rows() == 1) { //success $message = "Record has been successfuly updated!"; } else { //failed $message = "The subject update failed"; $message .= "<br>". mysql_error(); } } else { //Error Occured $message = "There were ". count($errors) . " error in the form"; } } ?> I'm having problem on this line <?php if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) ?> this line is for validation of my radio button which has a 0 and 1 value. && $_POST[$fieldname] != 0 Eventhough the textbox is not set and is empty, it's still bypassing my if statement and still executing the update query... Quote Link to comment https://forums.phpfreaks.com/topic/260882-need-help-in-my-if-condition/ Share on other sites More sharing options...
xyph Posted April 13, 2012 Share Posted April 13, 2012 empty($_POST[$fieldname]) && $_POST[$fieldname] != 0 Will never be true. Anything that would resolve to 0 would also resolve TRUE with empty. You could try using !== '0'. $_POST values will always be strings, and the !== forces it to equal in value and type. Quote Link to comment https://forums.phpfreaks.com/topic/260882-need-help-in-my-if-condition/#findComment-1337141 Share on other sites More sharing options...
bugzy Posted April 13, 2012 Author Share Posted April 13, 2012 empty($_POST[$fieldname]) && $_POST[$fieldname] != 0 Will never be true. Anything that would resolve to 0 would also resolve TRUE with empty. You could try using !== '0'. $_POST values will always be strings, and the !== forces it to equal in value and type. Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/260882-need-help-in-my-if-condition/#findComment-1337144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.