Simple PHP Error [closed] - php

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...

Related

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']));

No response to update query in oracle [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
I am a beginner . I made a php html combined page(viewemployees.php) to display my table from oracle data base. It worked fine. Then i made a page to edit my data base table(editemployees.php). that page has two functions. It displays the original table and a simple form which should contain the elements to update the table. When we place values in the form it directs us to a new php page(een.php) where it updates table and returns back to editemployees.php showing the updated table. but i am stuck when i fill in the form AND NOTHING happens. Even I have placed a condition that to echo a line if new value field is null. still that line is not echoed. This is some weird thing. I am posting both of my edit and view pages code click the link. kindly help.
In simple my table update is not working and showing undefined variable error at line 4 of een.php. Why is taht variabl undefined?? can anyone help please
https://www.dropbox.com/sh/xtuvotdy7c9wr1v/AADrNSlC_EJ0YkyDDkhe8mKGa?dl=0
<?php
include("connection.php");
$empid = $_POST['EMPLOYEE ID'];
$field = $_POST['EDIT FIELD'];
$nfield = $_POST['NEW VALUE'];
echo $field;
if( empty($_POST['NEW VALUE'] )){
echo "type new field properly";
}
else
{
$e = filter_var($empid, FILTER_SANITIZE_EMAIL);
$f = filter_var($field, FILTER_SANITIZE_EMAIL);
$nf = filter_var($nfield, FILTER_SANITIZE_EMAIL);
}
if( $e==$empid && $f==$field && $nf==$nfield)
{
if ($field=="age" || $field=="sal"){
$sel = "seleect * from employ";
$st = oci_parse(conn, $sel);
oci_execute($st);
$query = " update employ set $field = $nfield where empid = $empid";
$stmt = oci_parse($conn,$query);
oci_execute($stmt, OCI_COMMIT_ON_SUCCESS);
oci_free_statement($updateTitleInserted);
oci_close($conn);
echo oci_error();
header("Location: home.html");
}
else{
$query = "update employ set $field = '$nfield' where empid = $empid";
$stmt = oci_parse($conn,$query);
oci_execute($stmt,OCI_COMMIT_ON_SUCCESS);
oci_free_statement($updateTitleInserted);
oci_close($conn);
echo oci_error();
echo $field;
}
}
else
echo "wrong data entry go back and enter again";
?>
This line does not make sense.
$empid = $_POST["EMPLOYER ID"];
A post variable name cannot contain a space.

Array element adding using if condtion [closed]

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';

Parse error: syntax error, unexpected T_STRING on $register [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
This is basically my first time using PHP. I'm trying to create a reservation system, but I've been stuck at the registration page for a good while now. I end up getting "Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\feha\registration.php on line 57". Line 57 is:
$register = mysql_query("INSERT INTO 'users' (username, password, fname, lname, address, contact) VALUES ('".$username."','".$password."','".$fname."','".$lname."','".$address."','".$contact."')");
I've tried using a backslash before the "INSERT... and the error no longer appears, but nothing else does, for that matter. It's supposed to display a message if the user has been registered or if there was an error. I've also tried just using single quotes on the values, taking out the quotes on the table name, removing the column names.
It fixes it temporarily, but then another parse error appears further into my program, I fix it somehow, until it shows an error about unexpected $end at line 62 (my closing line). This is my entire code:
<?php
include 'connectdb.php';//database connection
if (isset($_POST['register']))
{
//input fields
$register = addslashes(strip_tags($_POST['register']));
$username = addslashes(strip_tags($_POST['username']));
$fname = addslashes(strip_tags($_POST['fname']));
$lname = addslashes(strip_tags($_POST['lname']));
$address = addslashes(strip_tags($_POST['address']));
$contact = addslashes(strip_tags($_POST['contact']));
$email = addslashes(strip_tags($_POST['email']));
$password = strip_tags($_POST['password']);
$repeat = strip_tags($_POST['repeat']);
//check for values
$errors = array();
if ($username&&$fname&&$lname &&$address&& $contact&& $email&&
$password&&$repeat)
{
if ($password!=$repeat) ///check if pw match
{
die("Your password does not match!");
}//check char length of username
if (strlen($username)<6||strlen($username)>32)
{
die ("Username must be between 6 and 32 characters");
}
//checking for password length
if (strlen(strlen($password)<8||$password)>12)
{
die ("Password must be between 8 - 12 characters");
}
//encrypt password
$password = md5($password);
//check if username already taken
$check = mysql_query ("SELECT * FROM users WHERE username='$username'");
if (mysql_num_rows($check)>=1)
{
die ("Username already taken);
}
if (!empty($errors))
{
foreach ($errors as $error)
{
echo $error, '<br/>';
}
}
else
{
$register = mysql_query("INSERT INTO 'users' (username, password, fname, lname, address, contact) VALUES ('".$username."','".$password."','".$fname."','".$lname."','".$address."','".$contact."')");
echo '<div id="good">You have been registered succesfully! ';}
}
else{echo "Please fill in all the fields";}
}
?>
Should be,
die ("Username already taken");

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