How do I extract a single column from another database? - php

Why this condition passes even if I change the $_GET variable?
I've this code
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && $_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc') { /*rest goes here*/ } else {redirect}
Link returns like this
http://localhost/system/results.php?script_id=2&results=reorder&sort_column=supplier_address&sort_order=desc
But when I change this sort_column=supplier_address to say for example sorcodsalumn=supplier_address it doesn't redirect, instead goes ahead, any idea why? But if I simply remove few letters and dont replace with something else it does redirect...
How come if am using this isset($_GET['sort_column'] and am modifying sort_column to something else still passes this condition

Basic PHP operator precedence... && evaluates before ||, so your entire statement boils down to:
(x && y && z && ....) || ($_GET['sort_order'] == 'desc')
You need to simplify that if(), add some () to enforce your own evaluation order, and then things should start working a bit better.

your AND's and OR's need to be bracketed properly.
else if (isset($_GET['results']) &&
$_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) &&
$_GET['sort_column'] != '' &&
isset($_GET['sort_order']) &&
$_GET['sort_order'] != '' &&
($_GET['sort_order'] == 'asc' || $_GET['sort_order'] == 'desc'))
{
/*rest goes here*/
} else {
redirect
}
More specifically your last || needs its own brackets, as shown above.

You need to put a bracket around your || (OR) statement like this:
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && ($_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc')) { /*rest goes here*/ } else {redirect}
Otherwise your statement will return true anytime sort_order is set to 'desc'.

Related

I don't understand why this redirect doesn't work

if (lo_first == 'true' && $lo_first != '1') {
header("Location: http://example.com/myOtherPage.php");
exit(); }
I don't understand why this redirect doesn't work
Take a look at your if:
if (lo_first == 'true' && $lo_first != '1') {
Here you use both lo_first and $lo_first, which indicates that $lo_first is a variable from which the $ is forgotten at its first use. Fix:
if (lo_first == 'true' && $lo_first != '1') {

How can i add exception to php if statement

I have this if statement but how can I add more conditions to it?:
if($row['Type']==$rtype){
}
I would like to have two conditions so when $row['Type']=A and $rtype=B and vise versa all others should be match exact but in case when Type A and B comes together I want to allow the statement display result.
I tried by adding:
($row['Type']=='A' && $rtype=='B') || ($row['Type']=='B' && $rtype=='A')
as:
if(...&& (($row['Type']==$rtype) || ($row['Type']=='A' && $rtype=='B') || ($row['Type']=='A' && $rtype=='B'))) {
}
What is the best way to approach this?
From your comment above, it sounds like you may need more clearly define when to "show" and when to "not show". Typically, if there are only two scenarios — either show or don't show — then you wouldn't need two if conditions.
However, with what you describe above, this should work:
if (($rtype == 'A' && $row['Type'] == 'B') || ($rtype == 'B' && $row['Type'] == 'A')) {
return true;
}
if (($rtype == 'D' && $row['Type'] == 'D') || ($rtype == 'E' && $row['Type'] == 'E') || ($rtype == 'F' && $row['Type'] == 'F') {
return false;
}
Note that each group is contained within its own ( and ). That does the AND you're looking for. Outside of each group, the || will do the OR.
If you know that you don't want to "show" if the first if doesn't evaluate to true, then you can simply do:
if (($rtype == 'A' && $row['Type'] == 'B') || ($rtype == 'B' && $row['Type'] == 'A')) {
return true;
}
return false;
I'm not sure what "show" and "don't show" mean so I'm just assuming a true/false will do for you.
Also check out the Operator Precedence link that abhishek posted above. That might give you some more general knowledge about how to group your logical expressions.

PHP - An if statement with concatenated '&&' and '||' can give problems?

I have a problem with a form check that use an if statement with multiple 'and' and 'or' operators. This check return me an anomalous occasionally false value.
public function insert_checkForm($form) {
$form = array_filter($form);
if (
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
isset($form['travel_go_from']) != isset($form['travel_go_to']) ||
isset($form['work_go_from']) != isset($form['work_go_to']) ||
!isset($form['travel_go_from']) &&
!isset($form['travel_go_to']) &&
!isset($form['work_go_from']) &&
!isset($form['work_go_to'])
) {
return false;
} else {
return $form;
}
}
Last question, the above code changes compared to this (in spite of the priorities of and operators)?
[...]
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
(isset($form['travel_go_from']) != isset($form['travel_go_to'])) ||
(isset($form['work_go_from']) != isset($form['work_go_to'])) ||
(!isset($form['travel_go_from']) && !isset($form['travel_go_to']) && !isset($form['work_go_from']) && !isset($form['work_go_to']))
[...]
Thanks =)
The most common problem with isset() is that it returns false when the item is NOT SET but also returns false if the item IS SET && IS NULL.
isset($arr['nonexisting']); //this returns: false
$arr['existing'] = null;
isset($arr['existing']); //this returns: false

Shortest way to code this php multiple if condition

I am trying to do a query like:
If $_GET['page'] == 'items' AND $_GET['action'] == 'new' OR 'edit'
Here's what I have:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && $_GET['action'] == 'new' || isset($_GET['action']) && $_GET['action'] == 'edit') {
// This is what Im looking for
}
}
Is this correct, and is this the easiest way to make this query?
You could have done it like this as well:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
}
}
Your way is perfectly fine, although I would almost be tempted to do it the following way. The only reason I suggest this is that your code requires that both action and page are set. If action is not set then there isn't much point checking if the page is == 'items'.
if(isset($_GET['page']) && isset($_GET['action'])) {
if($_GET['page'] == 'items' && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
//do code here
}
}
You may also try in_array like:
if (isset($_GET['page']) && $_GET['page'] == 'items')
{
if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'new', 'edit' ) )
{
// This is what Im looking for
}
}
That is one of possible solutions
if ( #$_GET['page'] == 'items' && in_array(#$_GET['action'], array('new','edit')))
Everything is ok, but also you can use function:
function paramIs($param, $values) {
$result = false;
foreach ((array)$values as $value) {
$result = $result || isset($_GET[$param]) && $_GET[$param] == $value;
}
return $result;
}
Usage:
if (paramIs('page', 'items') && paramIs('action', array('new', 'edit')))
{
// your code here
}
It will reduce the number of repetitions in your code and encapsulate logic in one place

PHP if not statements

This may be the way my server is set up, but I'm banging my head against the wall. I'm trying to say that if $action has no value or has a value that is not "add" or "delete" then have an error, else keep running the script. However, I get an error no matter what $action is.
$action = $_GET['a'];
if((!isset($action)) || ($action != "add" || $action != "delete")){
//header("location:index.php");
echo "error <br>";
}
$action is being set properly and if run something like if($action =="add") it works. This is on my local host, so it could be a settings issue.
Your logic is slightly off. The second || should be &&:
if ((!isset($action)) || ($action != "add" && $action != "delete"))
You can see why your original line fails by trying out a sample value. Let's say $action is "delete". Here's how the condition reduces down step by step:
// $action == "delete"
if ((!isset($action)) || ($action != "add" || $action != "delete"))
if ((!true) || ($action != "add" || $action != "delete"))
if (false || ($action != "add" || $action != "delete"))
if ($action != "add" || $action != "delete")
if (true || $action != "delete")
if (true || false)
if (true)
Oops! The condition just succeeded and printed "error", but it was supposed to fail. In fact, if you think about it, no matter what the value of $action is, one of the two != tests will return true. Switch the || to && and then the second to last line becomes if (true && false), which properly reduces to if (false).
There is a way to use || and have the test work, by the way. You have to negate everything else using De Morgan's law, i.e.:
if ((!isset($action)) || !($action == "add" || $action == "delete"))
You can read that in English as "if action is not (either add or remove), then".
No matter what $action is, it will always either not be "add" OR not be "delete", which is why the if condition always passes. What you want is to use && instead of ||:
(!isset($action)) || ($action !="add" && $action !="delete"))
You're saying "if it's not set or it's different from add or it's different from delete". You realize that a != x && a != y, with x != y is necessarily false since a cannot be simultaneously two different values.
You could also try:
if ((!isset($action)) || !($action == "add" || $action == "delete")) {
// Do your stuff
}
For future reference, you can quickly create a truth table to check if it evaluates the way you want... it's kind of like Sudoku.
(!isset($action)) && ($action != "add" && $action != "delete"))
Example:
column 1 is issetaction, column 2 and 3 evaluates !="add","delete" respectively
if($a=add) T && (F && T) => T && F => FALSE
if($a=delete) T && (T && F) => T && F => FALSE
if($a=nothing) T && (T && T) => T && T => TRUE
I think this is the best and easiest way to do it:
if (!(isset($action) && ($action == "add" || $action == "delete")))
Not an answer, but just for the sake of code formatting
if((isset($_GET['a'])) $action=$_GET['a']; else $action ="";
if(!($action === "add" OR $action === "delete")){
header("location: /index.php");
exit;
}
Note the exit; statement after header(). That's the important thing. header() does not terminate script execution.

Categories