Add input's errors in array with for - php

I have one problem in PHP code. It did not give me whatever output. Where my mistake?
if (isset($_POST['regBtn'])) {
$fname = strip_tags(trim($_POST['fname']));
$lname = strip_tags(trim($_POST['lname']));
$email = strip_tags(trim($_POST['email']));
$password = strip_tags(trim($_POST['password']));
$errMsg = array();
for($i=0; $i<=3;$i++) {
if (strlen($fname) < 0) {
$errMsg[$i] = "Գրեք Ձեր անունը ամբողջությամբ։";
} elseif (strlen ($lname) <0) {
$errMsg[$i] = "Գրեք Ձեր ազգանունը ամբողջությամբ";
} elseif (strlen($email) < 0) {
$errMsg[$i] = "Գրեք Ձեր էլ․ հասցեն";
} elseif (strlen($password) < 6) {
$errMsg = "Գաղտնաբառը պետք է պարունակի առնվազն 6 նիշ";
} }
var_dump ($errMsg);
}
else {
}

Several problems here, assuming that you have a form input with name="regBtn" which is required for this code to run:
strlen will not be < 0 though it may be 0. Use empty or a number greater than 0 if needed.
elseif will only execute if the if fails so just use if.
No need to loop, just use multiple if blocks and dynamically append to the error array [].
if (isset($_POST['regBtn'])) {
$fname = strip_tags(trim($_POST['fname']));
$lname = strip_tags(trim($_POST['lname']));
$email = strip_tags(trim($_POST['email']));
$password = strip_tags(trim($_POST['password']));
$errMsg = array();
if (empty($fname)) { // or use strlen < 2 or < 3 or whatever like $password
$errMsg[] = "Գրեք Ձեր անունը ամբողջությամբ։";
}
if (empty($lname)) {
$errMsg[] = "Գրեք Ձեր ազգանունը ամբողջությամբ";
}
if (empty($email)) {
$errMsg[] = "Գրեք Ձեր էլ․ հասցեն";
}
if (strlen($password) < 6) {
$errMsg[] = "Գաղտնաբառը պետք է պարունակի առնվազն 6 նիշ";
}
var_dump ($errMsg);
}
Also, you probably want to trim after strip_tags in case it leaves space(s):
$fname = trim(strip_tags($_POST['fname']));

Related

Auto Description From Tags

I'm trying to generate auto description from tags.
The code was working but after updating my site to Laravel 6 in stop working. I need to get it back working.
if( !empty( $request->description ) )
{
$description = Helper::checkTextDb($request->description);
}
else
{
$a_key = explode(",", strtolower($request->tags));
if(count($a_key) == 0)
$description = 'This is a great thing';
else
{
$description_get_keys = '';
foreach ($a_key as &$value)
{
if($value == end($a_key) && count($a_key) != 1)
$description_get_keys = $description_get_keys.' and '.$value.'.';
else if(count($a_key) == 1)
$description_get_keys = $value.'.';
else if (count($a_key) > 1 && $a_key[0] == $value)
$description_get_keys = $value;
else
$description_get_keys = $description_get_keys.', '.$value;
}
$description = 'This is a great thing about '.$description_get_keys;
}
}
I see a couple things that could possibly be an issue, not knowing what came before this code.
I will assume that the $request variable is an instance of Illuminate\Http\Request and that it is available in the function, right?
Try this updated code:
if($request->has('description'))
{
$description = Helper::checkTextDb($request->description);
}
else if ($request->has('tags'))
{
if (strpos($request->tags, ',') === false)
{
$description = 'This is a great thing';
}
else {
$a_key = explode(",", strtolower($request->tags));
$a_count = count($a_key);
$description_get_keys = '';
for ($i = 0; $i < $a_count; $i++)
{
if ($a_count == 1) {
$description_get_keys = "{$a_key[$i]}.";
}
else {
// first
if ($i === 0) {
$description_get_keys = $a_key[0];
}
// last
else if ($i === $a_count - 1) {
$description_get_keys .= " and {$a_key[$i]}.";
}
// middle
else {
$description_get_keys .= ", {$a_key[$i]}";
}
}
}
$description = "This is a great thing about {$description_get_keys}";
}
}
I wrote that quick so hopefully there are no errors.

Check and modify Capitalized letters in string

I'm trying to figure out how to make this code work.
I input some text via variable into code:
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
You will see that for the Genome there is a Nn and for the Fenome there is a Ee
Upon this happening I need it to give a 50/50 chance for a compared result to be EITHER Ee OR Nn - The code I have right now can only check them individually (so sometimes I ger Ee AND Nn, other times I will get ee and nn) and I feel there could be a much easier method of achieving this than what I'm trying:
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
if ($gepieces[0] === $fepieces[0]) {
$ss = $gepieces[0];
} else {
$ss = rand(1,2);
if ($ss == 1) {
$ss = $gepieces[0];
} else {
$ss = $fepieces[0];
}
}
if ($gepieces[1] === $fepieces[1]) {
$ee = $gepieces[1];
} else {
$ee = rand(1,2);
if ($ee == 1) {
$ee = $gepieces[1];
} else {
$ee = $fepieces[1];
}
}
if ($gepieces[2] === $fepieces[2]) {
$ff = $gepieces[2];
} else {
$ff = rand(1,2);
if ($ff == 1) {
$ff = $gepieces[2];
} else {
$ff = $fepieces[2];
}
}
if ($gepieces[3] === $fepieces[3]) {
$nn = $gepieces[3];
} else {
$nn = rand(1,2);
if ($nn == 1) {
$nn = $gepieces[3];
} else {
$nn = $fepieces[3];
}
}
if ($gepieces[4] === $fepieces[4]) {
$oo = $gepieces[4];
} else {
$oo = rand(1,2);
if ($oo == 1) {
$oo = $gepieces[4];
} else {
$oo = $fepieces[4];
}
}
echo $cubgender." - ".$ss."/".$ee."/".$ff."/".$nn."/".$oo."<br/>";
}
I'm a colossal idiot!
Figured out my own issue
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
if ($genome === $fenome) {
$cubgeno = $genome;
} else {
$cubgeno = rand(1,2);
if ($cubgeno == 1) {
$cubgeno = $genome;
} else {
$cubgeno = $fenome;
}
}
echo $cubgender." - ".$cubgeno."<br/>";
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
$sequence = "";
for ($i = 0;$i < count($fepieces); $i++) {
rand(1,2) == 1 ? $sequence .= $gepieces[$i] : $sequence .= $fepieces[$i];
}
echo $sequence;

PHP And MySQL Insert error: ucp is currently unable to handle this request

today i finished working on my markup and started writing code, i wrote this simple code for registration but when user clicks on submit button they see error:
This page isn’t working
ucp.ls-rp.ge is currently unable to handle this request.
HTTP ERROR 500
How can this be solved?
Any kind of help would be highly appreciated and thank you in advance.
<?
$host = 'localhost';
$user = 'lsrpge14_forum';
$pass = 'lsrpgeforum123.';
$db = 'lsrpge14_forum';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
/* CREATE CHARACTER FUNCTIONS */
$sex = 0;
function IsRPName($name, $max_underscores = 1)
{
$underscores = 0;
if ($name[0] < 'A' || $name[0] > 'Z') return false;
for($i = 1; $i < strlen($name); $i++)
{
if($name[$i] != '_' && ($name[$i] < 'A' || $name[$i] > 'Z') && ($name[$i] < 'a' || $name[$i] > 'z')) return false;
if( ($name[$i] >= 'A' && $name[$i] <= 'Z') && ($name[$i - 1] != '_') ) return false;
if($name[$i] == '_')
{
$underscores++;
if($underscores > $max_underscores || $i == strlen($name)) return false;
if($name[$i + 1] < 'A' || $name[$i + 1] > 'Z') return false;
}
}
if ($underscores == 0) return false;
return true;
}
function SendCharacterAplication($ownerid, $name, $sex, $explainmg, $explainpg, $charbio)
{
$sql = "INSERT INTO Aplicants (ownerid, charname, sex explainmg, explainpg, charbio) " . "VALUES ('$ownerid', '$name', '$sex', '$explainmg', '$explainpg', '$charbio')";
if ($mysqli->query($sql) ){
$_SESSION['createcharerror'] = "<h2 style='color:green'>თქვენი პერსონაჟი წარმატებით შეიქმნა! დაელოდეთ ადმინისტრაციის პასუხს!</h2>";
header("location: profile.php");
}
header("location: index.php");
}
/* CREATE CHARACTER CHECK VALUES */
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['createcharerror'] = "<h2 style='color:red'>პროფილის სანახავად გაიარეთ ავტორიზაცია!</h2>";
header("location: index.php");
}
if ( $_SESSION['countchars'] == 2)
{
$_SESSION['createcharerror'] = "<h2 style:'color:red'>თქვენ უკვე გაქვთ გაკეთებული 2 პერსონაჟი!</h2>";
header("location: index.php");
}
if(is_null($_POST['charname']))
{
$_SESSION['createcharerror'] = "<h2 style:'color:red'>ჩაწერეთ პერსონაჟის სახელი!</h2>";
header("location: index.php");
}
if(!IsRPName($_POST['charname']))
{
$_SESSION['createcharerror'] = "<h2 style='color:red;'>პერსონაჟის ნიკი არ არის სწორ ფორმატში Firstname_Lastname!</h2>";
header("location: index.php");
}
if(is_null($_POST['gender']))
{
$_SESSION['createcharerror'] = "<h2 style='color:red;'>მონიშნეთ პერსონაჟის სქესი!</h2>";
header("location: index.php");
}
if($_POST['gender'] == 'male')
{
$sex = 1;
}
else
{
$sex = 2;
}
if(str_word_count($_POST['charbio']) < 70)
{
$_SESSION['createcharerror'] = "<h2 style='color:red'>პერსონაჟის ბიოგრაფია არ უნდა იყოს 70 სიტყვა-ზე ნაკლები!</h2>";
header("location: index.php");
}
else{
SendCharacterAplication($_SESSION['masterid'], $_POST['charname'], $sex, $_POST['explainmg'], $_POST['explainpg'], $_POST['charbio']);
}
You may be getting a 500 error because the server is configured to repress SQL errors. If you have a look at your error logs or output the SQL being run and run it manually you may be able to see the exact error.
You must sanitize any values passed into SQL from user input, any use of an apostrophe in any of those fields will result in an SQL error (or worse SQL injection).
http://php.net/manual/en/mysqli.real-escape-string.php
It's also possible you've got the table name wrong compared to what's in the database; Aplicants should have 2 p's

Why is the return value of this PHP function zero for a long array?

When the row['error'] is bigger than 35, the value isn't present and the result of the function is 0. Where is the problem?
<?php
if ($row['error'] == "")
{
$error = "0";
}
else
{
$error = $row['error'];
}
if ($row['error'] != "")
{
if (strlen($error) > 35)
{
$error = substr($row['error'],0,32) + "...";
}
else
{
$error = $row['error'];
}
}
?>
Change
$error = substr($row['error'],0,32) + "...";
to:
$error = substr($row['error'],0,32) . "...";
The concatenate operator in PHP isn't a plus (+) sign; it's a period (.) sign
All this code is not necessary. The second condition is redundant, and it doubles the else condition from the above. Make it all with just these few lines of code:
<?php
$error = $row['error'];
if (strlen($error) > 35) {
$error = substr($row['error'],0,32) . "...";
}
?>
Because you check:
if(strlen($error) > 35) {
}

help with array

What am i doing wrong here? The username string is less than 2 chars but it still dont set error[]?
Register:
$errors = array();
$username = "l";
validate_username($username);
if (empty($errors)) {
echo "nothing wrong here, inserting...";
}
if (!empty($errors)) {
foreach ($errors as $cur_error)
$errors[] = '<li class="warn"><span>'.$cur_error.'</span></li>';
}
function validate_username($username) {
$errors = array();
if (strlen($username) < 2)
$errors[] = "Username too short";
else if (strlen($username) > 25)
$errors[] = "Username too long";
return $errors;
}
It's because you are not assigning the return value of validate_username() to any variable.
Try
$errors = validate_username($username);
Change validate_username($username); to $errors = validate_username($username);
Your function is affecting a local variable named errors, not the global errors that you may have been expecting.
Further, your code can be cleaned up a little bit as follows
$username = "l";
$errors = validate_username($username);
// No errors
if ( empty($errors) ) {
echo "nothing wrong here, inserting...";
}
// Errors are present
else {
foreach ( $errors as $cur_error ) {
$errors[] = '<li class="warn"><span>'.$cur_error.'</span></li>';
}
}
function validate_username($username) {
$errors = array();
$len = strlen($username);
if ( $len < 2 ) {
$errors[] = "Username too short";
} elseif ( $len > 25 ) {
$errors[] = "Username too long";
}
return $errors;
}
you're not returning it the right way, you need:
$errors = validate_username($username)
you forgot to assign $errors
$errors = validate_username($username);
**//TRY THIS INSTEAD**
$errors = array();
$username = "l";
**$errors = validate_username($username);**
if (empty($errors)) {
echo "nothing wrong here, inserting...";
}
if (!empty($errors)) {
foreach ($errors as $cur_error)
$errors[] = '<li class="warn"><span>'.$cur_error.'</span></li>';
}
function validate_username($username) {
$errors = array();
if (strlen($username) < 2)
$errors[] = "Username too short";
else if (strlen($username) > 25)
$errors[] = "Username too long";
return $errors;
}

Categories