pwes24 Posted January 28, 2008 Share Posted January 28, 2008 This is a small issue I believe, but I just can't get it right. Please help. I'm like to store login info for new users. This script should check user input and if empty, should return error msg and not save anything on the database table. But my code saves whatever is sent even if some sections are empty. What should I do? if ($name == "") { $error .= "Enter your name"; } if ($username == "") { $error .= "Enter username"; } if($password == "") { $error .= "Enter password"; } else { $query = "insert into 'login' values()"; I have shortened to get to the point. If I only use one 'if' and one 'else' statement it works well. What can I do? Quote Link to comment https://forums.phpfreaks.com/topic/88134-solved-verify-input/ Share on other sites More sharing options...
vicodin Posted January 28, 2008 Share Posted January 28, 2008 Use Else If Quote Link to comment https://forums.phpfreaks.com/topic/88134-solved-verify-input/#findComment-450940 Share on other sites More sharing options...
clown[NOR] Posted January 28, 2008 Share Posted January 28, 2008 try this $error = array(); if (empty($name)) { $error[] = "No name"; } if (empty($username)) { $error[] = "No username"; } if (empty($password)) { $error[] = "No password"; } if (!empty($error)) { foreach ($error as $error1) { echo $error1."<br />"; } else { $wuery = "insert into 'login' values()"; } Quote Link to comment https://forums.phpfreaks.com/topic/88134-solved-verify-input/#findComment-450941 Share on other sites More sharing options...
toplay Posted January 28, 2008 Share Posted January 28, 2008 You forgot the "else" on the "if" conditions, however, the "ifelse" approach will show only one error at a time (and your ".=" notation will not really come into play/work). Here's another approach: <?php // Example where it collects all errors to display at once. $blnContinue = TRUE; $arrError = array(); if (empty($name)) { $arrError[] = 'Enter your name'; $blnContinue = FALSE; } if (empty($username)) { $arrError[] = 'Enter a username'; $blnContinue = FALSE; } if (empty($password)) { $arrError[] = 'Enter a password'; $blnContinue = FALSE; } if ($blnContinue) { $query = "insert into 'login' values()"; // check query worked and display good message } else { // display errors found echo implode('<br/>', $arrError); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88134-solved-verify-input/#findComment-450943 Share on other sites More sharing options...
clown[NOR] Posted January 28, 2008 Share Posted January 28, 2008 my bad... it's 5.30 am here =) Quote Link to comment https://forums.phpfreaks.com/topic/88134-solved-verify-input/#findComment-450963 Share on other sites More sharing options...
pwes24 Posted January 28, 2008 Author Share Posted January 28, 2008 Thanks everyone! Much love Quote Link to comment https://forums.phpfreaks.com/topic/88134-solved-verify-input/#findComment-450966 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.