php - Multiple IF statements in simple form validation -
i newbie , trying implement simple validation script after reading up, can't see how can have multiple ifs sql insert if required fields met. rather having multiple else statements, syntax approach having form validation ifs , if 1 of them fails, correct error shown , sql not execute?
if(isset($_post ['submit'])){ $user_id = get_current_user_id(); $caterr = $ratingerr = $titleerr = $texterr = ""; if (empty($_post["category"])) { $caterr = "category required"; } else { //do insert below! } if (empty($_post["rating"])) { $ratingerr = "rating required"; } else { //do insert below! } if (empty($_post["post_name"])) { $posterr = "title required"; } else { //do insert below! } if (empty($_post["text"])) { $texterr = "text required"; } else { //do insert below! } //pdo query begins here... $sql = "insert forum(id, category, rating, post_name, text
use 1 variable error messages , concatenate in branches, in end if variable still empty string won't insert. (and don't need of empty else blocks contain nothing comment.)
$err = ""; if (empty($_post["category"])) { $err .= "<br/>category required"; } if (empty($_post["rating"])) { $err .= "<br/>rating required"; } if (empty($_post["post_name"])) { $err .= "<br/>title required"; } if (empty($_post["text"])) { $err .= "<br/>text required"; } //pdo query begins here... if($err=='') { $sql = "insert forum(id, category, rating, ..."; ... }
Comments
Post a Comment