PHP Error Handling Help

JoshuaLuke

Posting Freak
Jan 29, 2012
529
51
Hey,

I'm making staff apps for Uber, the data from the POST vars insert fine, but they all have to be set according to my PHP, but when they're not set the data still inserts into the database. Also, i'm unsure about how to display a message if a field isn't set.

Here's my code:
Code:
 if (isset($_POST["q1"]) && isset($_POST["q2"]) && isset($_POST["q3"]) && isset($_POST["q4"]) && isset($_POST["q5"]) && isset($_POST["q6"]) && isset($_POST["q7"]) && isset($_POST["q8"]) && isset($_POST["q9"]) && isset($_POST["q10"])&& isset($_POST["q11"]) && isset($_POST["q12"]) && isset($_POST["q13"]) && isset($_POST["q14"]) && isset($_POST["q15"])) 
{
dbquery("INSERT INTO `applications` (`username`, `q1`, `q2`, `q3`, `q4`, `q5`, `q6`, `q7`, `q8`, `q9`, `q10`, `q11`, `q12`, `q13`, `q14`, `q15`, `q16`) VALUES ('".USER_NAME."','".$_POST['q1']."', '".$_POST['q2']."', '".$_POST['q3']."', '".$_POST['q4']."', '".$_POST['q5']."', '".$_POST['q6']."', '".$_POST['q7']."', '".$_POST['q8']."', '".$_POST['q9']."', '".$_POST['q10']."', '".$_POST['q11']."', '".$_POST['q12']."', '".$_POST['q13']."', '".$_POST['q14']."', '".$_POST['q15']."', '".$_POST['q16']."')");
}

Cheers
 

tyr0ne

Active Member
Mar 29, 2012
141
14
You shouldnt do it like that. just verify one post variable, for example

Code:
if( $_POST["formname" ) {
 
}

and then inside that if check your values.

something like

Code:
$err = "";
 
if( empty($_POST["name"]) ) {
    $err = "You have to put your name";
} else {
    if( empty($_POST["somethingelse"]) ) {
        continue with all your checking
    } else {
        your db query to input everything when    you validated all your data
    }
}
 

JoshuaLuke

Posting Freak
Jan 29, 2012
529
51
You shouldnt do it like that. just verify one post variable, for example

Code:
if( $_POST["formname" ) {
 
}

and then inside that if check your values.

something like

Code:
$err = "";
 
if( empty($_POST["name"]) ) {
    $err = "You have to put your name";
} else {
    if( empty($_POST["somethingelse"]) ) {
        continue with all your checking
    } else {
        your db query to input everything when    you validated all your data
    }
}

But they all need to be inserted into the database anyway, so I figured using issets ould be the easiest way. Then I was going to do an !$_POST but it didn't work out. Anyway, any other way of doing it easier?
 

tyr0ne

Active Member
Mar 29, 2012
141
14
Like I said you don't need to check if they're all set to submit the form. You just have to see if you have your form being posted by checking if( isset($_POST["formname"])) or if( $_POST["formname"]). THEN when you know you have a form being posted, you check if they're all set. The way you're doing it permits no error handling whatsoever. Sure it checks if they're all set but you can't give any errors to your user when one is empty.

Checking if they're all set just takes ressources and is uneeded. Instead you use those ressources you would waste to check if each POST has a value in the condition I made.

If you want to be even better, use a for() to go through your post array, that will save you some lines of code.
 

JoshuaLuke

Posting Freak
Jan 29, 2012
529
51
Like I said you don't need to check if they're all set to submit the form. You just have to see if you have your form being posted by checking if( isset($_POST["formname"])) or if( $_POST["formname"]). THEN when you know you have a form being posted, you check if they're all set. The way you're doing it permits no error handling whatsoever. Sure it checks if they're all set but you can't give any errors to your user when one is empty.

Checking if they're all set just takes ressources and is uneeded. Instead you use those ressources you would waste to check if each POST has a value in the condition I made.

If you want to be even better, use a for() to go through your post array, that will save you some lines of code.

What i'm saying is it still inserts into the database even with the issets,
 

tyr0ne

Active Member
Mar 29, 2012
141
14
And that's why I posted a code above that handle errors to a certain extent. With the $err variable you can then echo the error to your user, etc.

So it won't update the SQL unless every value is alright
 

Users who are viewing this thread

Top