Query Based on User Input—PHP - php

A script I'm using takes in a string of field name/value pairs, splits them, and creates a query from them. The string is formatted like this:
var1==value1,var2==value2...
The values will be submitted by users on the frontend of the website. So, if a user selects a value for var1 and var4 but not 2 and 3 I would need the string to look like this:
var1==value1,var4==value4
Getting the user-submitted data isn't a problem. What is the best way to add in the field name and == only if the associated value is not blank?

if (isset($varname))
isset — Determine if a variable is set and is not NULL
if(empty($varname)))
empty — Determine whether a variable is empty
if(is_null($varname)))
is_null — Finds whether a variable is NULL
In other words, it only returns true when the variable is null. is_null() is the opposite of isset(), except you can use isset() on unknown variables.

You could do something like
$pairs = "var1==stuff,var3==morestuff"
if(strpos($pairs, "var2==") !== false){
$pairs .= "var2==defaultvalue";
}
And you could do that for every var# you want. This would be able to check if the
About the strpos : How do I check if a string contains a specific word in PHP?

Just whip over the submitted form fields looking for anything with a value:
foreach($_POST as $key => $value){
if($value != ''){
// do stuff
}
}
also add a check to skip the submit = submit bit :)

Related

does isset php function checks string length

in my website's registration form, the following php code controls user first name field :
if (!isset($value[4])){
$return = $register_msg['_register_some_msg'];
}
what does this code do ? does it have anything with checking the string length ?
currently it forces the first name of minimum 3 characters, but as i searched seems that strlen() Function checks the length of string which doesn't exists there.
Yes, this code is checking string length.
It appears you have two arrays.
$value is a numerical array, and you check if the 5th element is set. (index starts on 0).
$register_msg is an associative array, instead of numbers as the index, it uses keys. Here you assign the value of key _register_some_msg to your return variable.
The other option is that $value is a string, and it's using bad practice to check each letter by referencing them as an array key:
<?php
$x = 'abcd';
echo $x[3];
//output: d
Which is just bad. use strlen() instead http://php.net/manual/en/function.strlen.php
The isset() method here just verify if $value[4] (your object, or string, or other) exists or not.

if statement evaluates to true on NULL variable when using is_null()

So I've got a form with a modal, that modal has 3 rows with 2 text fields each, if the user (me in this prod case) fills out only 2 rows, and leave the other row empty, that 3rd value should be NULL.
In my script I've got:
if (!is_null($_POST['packageDependencies']['bundle'][2])) {
$packageDependency3 = $_POST['packageDependencies']['bundle'][2] . "|" . $_POST['packageDependencies']['version'][2] . "|" . $_POST['packageDependencies']['repository'][2];
$depends = "<key>dependencies</key>
<array>
<string>$packageDependency1</string>
<string>$packageDependency2</string>
<string>$packageDependency3</string>
</array>
";
}
So I'm checking if (!is_null($3rdRow)) { //Do this }, but the variable $_POST['packageDependencies']['bundle'][2] is in fact NULL, as I use var_dump($_POST['packageDependencies']['bundle'][2]); and I get NULL printed to the page, but the if statement is still processing as if it isn't NULL.
$depends gets fwrite() to an XML file, and when I open it, I only see || and but that shouldn't be there as the variable is NULL as I entered no values into those input fields.
Given my advice, a more complete solution would be:
if (!empty(trim($_POST['packageDependencies']['bundle'][2]))) {
NULL is a specific state of a variable that involves the way PHP associates the name of a variable with a variable location. You can think of it like a flag, that indicates a variable name exists, but there is no storage location associated with it. There are a number of situations that empty with trim will catch that will bypass a check against null.
Even though !empty() did the trick, I've decided to use == to be less ambiguous. The answers found here are quite intuitive.
EDIT: As per #gview, adding (!empty(trim($var))) is the best bet as if a user accidentally presses the space key after a tab, it will avoid any errors.

PHP while loop ignores its condition

At the moment i try to create a function in php which simply reads an int value of a field of an array and compares it to a max value.
If this max value is reached, it shall set the value of the field to zero, jump to the next field in the array and increase the int value stored inside.
If this value also reached the max value do the same as above.
If the above condition isn´t true it shall just increase the stored value in the array.
My Code looks like this:
if($sign_counter[0] === (count($pw_signs) - 1)){
$counter = 0;
while($sign_counter[$counter] === (count($pw_signs) - 1)){
$sign_counter[$counter] = "0";
$counter++;
}
$sign_counter[$counter]++;
}
else{
$sign_counter[0]++;
}
I allready tested this part of the function several times with different values on my website and browser. I also checked if the values were stored correctly in the array and inside needed variables.
Thats how my array looks like:
$sign_counter = array("38", "2");
For example:
$sign_counter array to store int values
(count($pw_signs) - 1) always equals 38 (because there are 39 fields in the counted array)
$counter used to determine the field position inside the array
Now if i store the value "38" in the first field and "2" in the second field of the array the code should detect, that the max value is reached in the first field, set the value of the field to 0, then jump to the next field and increase its value by 1.
Instead of what i want to achieve the code just increases the value of the first field of the array.
It looks like the while loop just ignores it's own condition but the values itself don't seem to be the problem.
I don't really get why the while loop behaves like this.
Do i completly miss something here?
Would appreciate any help.
Greetings Sleepy
The problem is that you're storing the values as strings, not numbers, and you're using the === operator, which doesn't perform type coercion between different types. count($pw_signs) - 1 will always be a number, not a string, all the === tests will fail.
Get rid of the quotes around all the numbers and it should work as desired. And if the source of the values is external, convert them to numbers with intval() before storing into the array.

Comparing variable pathinfo

How do I get the path info (php) when the path is of the form: www.example.com/post/* (* being a variable number that matches the post id) and compare it to www.example.com/post to make it TRUE.
In other words, if pathinfo is www.example.com/post/1068 (or 1069, or 1070, or whatever the id number) then the statements that follow are executed. If not, then they are skipped.
The snippet is destined to a template file. If this requires a pre-process function, please say so. Thank you.
I am new at this so please do not send me to some other post that reads like Chinese! :)
Thank you.
You could explode and filter for empty values and then use the last occurrence, something rough like this:
$url = "www.example.com/post/1068";
$id = array_pop(array_filter(explode('/', $url)));
$validIds = array("1068", "1069", "1070");
if (in_array($id, $validIds)) {
//do something;
}
explode will separate your string in an array using "/" as a separator (in this case)
array_filter will filter empty values in order to make sure that the last value will be your id
array_pop will retreive the last value of the given array
in_array will compare a value with an array and check if the value is within the given array and return true/false depending whether it is or not inside the array.

Regex to validate checkbox input

I'm having some issues with codeigniter's validation filter when using checkboxes. I typically used the numeric filter for checkboxes, presuming it would filter for 0 or 1 but now I see there are several instances in which this fails.
Does anyone know a regex that I can put in the preg_match to validate a checkbox?
I would like this to allow for booleans and a few others 1, 0, null, true, false, empty etc...
A checkbox only returns one value. It's value (as indicated in its value= attribute) or 'true'. If the checkbox is not selected, it is not passed in the POST request. Therefore, for validation, you only need to check for 2 things:
Was it even selected in the first place?
Is its value consistent with what you expect it to be?
So:
if (isset($_POST['checkbox']) && ($_POST['checkbox'] == 'true') { //or whatever value you want
Should do the trick nicely. Unless I've misunderstood your question in which case please comment.
Checkboxes either have a value or they don't. If they have a value they're checked, if they don't then they're not checked.
A regex would be serious overkill here, you only need to check if the checkbox exists in the submitted data.
$checkBoxChecked = isset ($_POST['checkbox_name_goes_here']);

Categories