Array element adding using if condtion [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want get an array of errors using if condition
my code is below
$errors = array();
if(empty($name))
$errors[] = 'Name required';
elseif(empty($usernmae))
$errors[] ='Username required';
final output i expect
$errors = array([0]=>'Name Required',[1]=>'username required');
but it returns only one array element
$errors = array([0]=>'Name Required');
anyone know

elseif(empty($usernmae))
It will only do this as an else, so you won't ever have both errors returned.
If you want both, you need to do it as two if's:
if(empty($name))
$errors[] = 'Name required';
if(empty($usernmae))
$errors[] ='Username required';

Use only if instead of else if
$errors = array();
if(empty($name))
$errors[] = 'Name required';
if(empty($username))
$errors[] ='Username required';

Related

More than one preg_match queries? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am a newbie to php and trying to write the code that validates my form.now the problem is I am having trouble in this line of code:
$fullname_pattern = "/[a-zA-Z]+/";
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$email_pattern = "/^[a-zA-Z]+([a-zA-Z0-9]+)?#[a-zA-Z]{3,50}\.(com|net|org)/";
$password = $_POST['password'];
$password_pattern = "/(.){6,12}/";
if(preg_match($fullname_pattern,$fullname)) &&
(preg_match($email_pattern,$email)) &&
(preg_match($password_pattern,$password))
{
header("Location: home.php");
}
else
header("Location: registration.php");
Don't know what to do!
if(preg_match($fullname_pattern,$fullname))
Remove Last ")"
And add here (preg_match($password_pattern,$password)))
Like this:
if(preg_match($fullname_pattern,$fullname)
&& preg_match($email_pattern,$email)
&& preg_match($password_pattern,$password)
) {

looping through results from mysql [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am having a little bit of trouble searching through my resulting array from mysql. I have the following code:
if($result=$link->query("select s_abbreviation from northwoods.state")) {
if($result->num_rows) {
while($row=$result->fetch_assoc()) {
if(!preg_match("/".$STATE."/", $row['s_abbreviation'])) {
$error=true;
echo "Please enter a valid two letter state abbreviation for the state that your address is located in!";
}
}
}
}
The resulting array has 59 rows and I am trying to match the $STATE pattern to at least one of the rows, otherwise echo an error. The problem is that it matches 1 of the 59 rows but still echos 58 errors because it fails to match the other 58 rows. Is there a way or modifier i can use so that it searchs through all of the rows and if it finds one match then no errors are sent?
Try it
if($result=$link->query("select s_abbreviation from northwoods.state")) {
if($result->num_rows) {
$error = true;
while($row=$result->fetch_assoc()) {
if(preg_match("/".$STATE."/", $row['s_abbreviation'])) {
$error = false;
}
}
if($error) {
echo "Please enter a valid two letter state abbreviation for the state that your address is located in!";
}
}
}
Let me know, if there is any issue.
Even if your code is not "bleeding edge", here is what to do to fix it:
if($result=$link->query("select s_abbreviation from northwoods.state")) {
if($result->num_rows) {
while($row=$result->fetch_assoc()) {
if(!preg_match("/".$STATE."/", $row['s_abbreviation'])) {
$error=true;
}
}
}
}
if ($error) {
echo "Please enter a valid two letter state abbreviation for the state that your address is located in!";
} else {
echo "OK";
}

Why are my if elseif statemtnes being triggered when input entered is correct? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My if elseif statements are being triggered every time I hit submit and it is only specific ones. I have checked Stack for similar problems but they all have to do with statements not triggering. I have also tried to validate my form multiple times and no matter what, right or wrong, these specific ones are triggered:
Username: strlen statement
Password: strlen statement
E-Mail: FILTER_VALIDATE_EMAIL
Empty Check is triggered even with all fields filled
Any help with correcting this would really be appreciated.
//Spam Check
if($spam > 2){
$error .= '<div class="problem">Too many accounts have been created from this IP Address. Please contact us if you would like to create more.</div>';
}
//Username Check
if(!preg_match("/^[a-zA-Z0-9 ]*$/",$user)){
$error .= '<div class="problem">You may only use letters and numbers in your username.</div>';
}
elseif(strlen($user) < 3 OR strlen($user) > 20){
$error .= '<div class="problem">Please use an username between 3 and 20 characters.</div>';
}
elseif($taken > 0){
$error .= '<div class="problem">This username has already been taken. Please choose another.</div>';
}
//Password Check
if(!preg_match("/^[a-zA-Z0-9 ]*$/",$pass1)){
$error .= '<div class="problem">Do not use special characters in your password.</div>';
}
elseif(strlen($pass1) < 5 OR strlen($pass1) > 20){
$error .= '<div class="problem">Your password must be at least 5 characters and no more than 20.</div>';
}
elseif($pass1 != $pass2){
$error .= '<div class="problem">You did not confirm your passwords correctly. Please make sure they are the same.</div>';
}
//E-Mail Check
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error .= '<div class="problem">The E-Mail Address entered was invalid. Please use another.</div>';
}
elseif(strlen($email) > 50){
$error .= '<div class="problem">This E-Mail Address is too large for us to store. Please use a different one.</div>';
}
//Empty Check
if(empty($user) OR empty($pass1) OR empty($pass2) OR empty($email)){
$error .= '<div class="problem">You need to fill out all fields to create an account.</div>';
}
//Variables
$user = mysqli_real_escape_string($_POST['username']);
$pass1 = mysqli_real_escape_string($_POST['pass1']);
$pass2 = mysqli_real_escape_string($_POST['pass2']);
$email = mysqli_real_escape_string($_POST['email']);
You are using a mysqli function on POST data -- your variables aren't coming from SQL, are they? Use trim() and strip_tags() or something similar to sanitize your data (but kudos for sanitizing to begin with, and using mysqli rather than mysql functions.)
Try something like this for all of your variables and see if your IF statements start evaluating correctly. If they are all NULL, they are probably all returning false right now.
$user = strip_tags(trim($_POST['username']));

Simple PHP Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
<?php
$text = $_POST['username', 'bio', 'service', 'age'];
if (empty($text )) {
echo "You forgot to fill in a field.";
}
else
echo "Passed.";
?>
I know this seems like a really simple question but I cannot figure this out for the life of me. Thank you a bunch if you guys can figure this out for me.
Parse error: syntax error, unexpected ',', expecting ']' in /home/burtonmo/public_html/v2/add.php on line 2
You can only access one element at a time in an array. Change your code to something like
$username = $_POST['username'];
$bio = $_POST['bio'];
$service = $_POST['service'];
$age = $_POST['age'];
if (empty($username) || empty($bio) || empty($service) || empty($age)) {
echo "You forgot to fill in a field.";
}
else
echo "Passed.";
There are quite a few ways to check variables are set, the way your doing is syntactically incorrect hence the error:
Here is a dynamic way that you can loop through an array of expected keys and check the corresponding $_POST[*]. Good for some situations and not for others:
$form_keys = array('username','bio','service','age');
$errors = array();
//check is POST
if($_SERVER['REQUEST_METHOD']=='POST'){
//loop $form_keys array
foreach ($form_keys as $form_key){
//check the corresponding $_POST[*] - if empty fill error
if(empty($_POST[$form_key])){
$errors[$form_key] = '*required field';
}
}
//error should be empty if all went well
if(empty($errors)){
//good - do something
}else{
//bad - show errors
//$errors contained which fields were empty
}
}
Or you can do it the standard way which is the best way IMO as you can assign different errors can validate based on other types like is_numeric(). Though its obviously a longer piece of code.
$errors = array();
//check is POST
if($_SERVER['REQUEST_METHOD']=='POST'){
//check user
if(empty($_POST['username'])){
$errors['username'] = '*required field';
}
//bio
if(empty($_POST['bio'])){
$errors['bio'] = '*required field';
}
//service
if(empty($_POST['service'])){
$errors['service'] = '*required field';
}
//age
if(empty($_POST['age'])){
$errors['age'] = '*required field';
}
//error should be empty if all went well
if(empty($errors)){
//good - do something
}else{
//bad - show errors
//$errors contained which fields were empty
}
}
Good Luck Hope it helps...

Why doesn't this work? :/ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I don't understand why this doesn't work? It's a register form checking if fields are filled in,password is equal to retype password, and if it doesn't already exist in database.
I get this error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/a4550840/public_html/newreg.php on line 32
But I already put a ';' at line 32 ... I don't understand why this error occurs.
Any help is appreciated :).
EDIT: Fixed that error ^ and added the mysql_real_escape_string , but it doesn't register the information to the database for some reason?
EDIT: It works now :), took away the quotations from the query
<?php
include ('connect.php');
if ($_POST['submit']) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$repassword = mysql_real_escape_string($_POST['repassword']);
$email = mysql_real_escape_string($_POST['email']);
if ($username && $password && $repassword && $email){
if ($password == $repassword) {
$check = mysql_query("SELECT * FROM members WHERE username='$username' ");
$countrows = mysql_num_rows($check);
if ($countrows == 0){
mysql_query("INSERT INTO members ('username','password','email') VALUES ('$username','$password','$email') ");
} else {
echo 'Username already exists';
}
} else {
echo 'Passwords don'\t match';
}
} else {
echo 'Fill in the fields';
}
} else {
echo 'Register please';
}
?>
You have a problem here:
echo 'Passwords don't match';
You need scape single quote as:
echo 'Passwords don\'t match';
or
echo "Passwords don't match";
NOTE: Your code is vulnerable to sql injection, you should use mysql_real_scape_string() before to pass yours parameters as sql query.
I suggest:
$username = mysql_real_scape_string($_POST['username']);
$password = mysql_real_scape_string($_POST['password']);
$repassword = mysql_real_scape_string($_POST['repassword']);
$email = mysql_real_scape_string($_POST['email']);
TIP: When your are testing (in dev environment) mysql querys, you should combine die(mysql_error()) at the end of line to check if you has a problem like as:
mysql_query('your sql') or die(mysql_error()).
If you have an error, this cause your app die an show the mysql error.
See this reference.
This error shows the earliest time it encounters a problem. The problem is on that line, or on a previous line. In this case you didn't escape a quote, so the parser found the rest of your string while it expected a , or ;. If you look at the colouring of your code, you'll see that more easily. The correct line would be
echo 'Passwords don\'t match';

Categories