Jump to content

[SOLVED] verify input


pwes24

Recommended Posts

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?

Link to comment
Share on other sites

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()"; }

Link to comment
Share on other sites

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);
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.