Sql rating issue - php

Here's my code. My problem is that I want the insertion to happen just when the rating_airlines value is between 1 and 5 but it keeps adding all the values.
rating_airlines is varchar on my database.
<?php
require_once('connect.php');
mysql_select_db($database_localhost,$con);
$nom_airlines=$_GET['nom_airlines'];
$rating_airlines=$_GET['rating_airlines'];
$a=intval($rating_airlines);
if($a=1 || $a=2 || $a=3 || $a=4 || $a=5 ) {
mysql_query("INSERT INTO Airlines(nom_airlines,rating_airlines)
VALUES ('$nom_airlines','$a') ");
echo "OK";}
else {
die('RequĂȘte invalide : ' . mysql_error());
}
?>

You're not comparing the values you are setting. Single equals sets double compares.
if($a=1 || $a=2 || $a=3 || $a=4 || $a=5 )
Should be
if($a==1 || $a==2 || $a==3 || $a==4 || $a==5 )
But could even be
if($a>=1 && $a <=5 )
You also should switch from the mysql to mysqli or PDO. mysqli or PDO - what are the pros and cons?
Also note that by passing $nom_airlines directly to your query you are open to injections at a minimum use the mysql_real_escape_string.
$nom_airlines=mysql_real_escape_string($_GET['nom_airlines']);

You are using assigning operator . You need to use equality here.
if($a==1 || $a==2 || $a==3 || $a==4 || $a==5 ) {
Take a look into these 2:
Assignment Operators in PHP
Equality Operators in PHP
Moreover,
$a = $b Assign Sets $a to be equal to $b.
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)

Related

Negation (!) does not change the outcome of preg_match in PHP [duplicate]

In PHP, is
if(!$foo)
equivalent with
if($foo != true)
or with
if($foo !== true)
or is it even something completly different of both?
Note that,
== OR != compares the values of variables for equality, type casting as necessary. === OR !== checks if the two variables are of the same type AND have the same value.
This answer will give you better explanation of this concept:
https://stackoverflow.com/a/80649/3067928
if(!$foo)
is the equivalent to
if($foo != true)
so
$foo = null;
if(!$foo){
echo "asd";
}
will ouptut "asd"
Its not the same
!= is No equal (Returns true if is not equal)
!== is Not identical (Returns true if is not equal , or they are not of the same type)
$a != $b
TRUE if $a is not equal to $b after type juggling.
$a !== $b
TRUE if $a is not equal to $b, or they are not of the same type.
See type juggling in PHP for more info on type juggling.
Sources : php.net

PHP: Evaluate string as logical expression and return true or false

I have a txt file with hundreds of logical expressions.
I want to read each one (no problem so far) and to be able to evaluate it recursively, but I can't figure a way how. The expression has && and == and comparissons between strings and numbers. I don't want to use eval, as it's not recommended apparently and it didn't work in my case.
Example. Let's say I read these 2 strings:
s = "a == alpha && b == beta || b == omega", or
s = "g >= 2 && f != gamma"
I want to break them down to
($a == "alpha" && $b == "beta" || b == "omega")
($g >= 2 && f!= "gamma")
to use them in an if, so that it returns TRUE or FALSE. My problem is not with replacing the variables, it's with making them evaluate as a logical expression
Can anybody give me a hand?
Thanks in advance,
Cristina
Try this :
if( (($a == 'alpha' && $b == 'beta') || ($b == 'omega')) || ($g >= 2 && $f != 'gamma'))
{
// returns true
}
else
{
// returns false
}

How do I use or operator in PHP?

if($a=="" and $b=="" or $c=="") {
echo "something";
}
I want to ask that if $a AND $b is empty then it will print: something. But if $b is not empty, then it will check the statement like this e.g. if $a is empty AND $c is empty then print: something (Means $a is compulsory to check with the variable which is empty $b OR $c)
See the PHP Operator Precedence table. and has higher precedence than or, so your condition is treated as if you'd written
if (($a == "" and $b == "") or ($c == ""))
Since you want the $a check to be independent, you need to use parentheses to force different grouping:
if ($a == "" and ($b == "" or $c == ""))
Note that in expressions, it's conventional to use && and || rather than and and or -- the latter are usually used in assignments as a control structure, because they have lower precedence than assignments. E.g.
$result = mysqli_query($conn, $sql) or die (mysqli_error($conn));
See 'AND' vs '&&' as operator
I suppose you need something like this:
if (
($a == '')
&&
(
($b == '')
||
($c == '')
)
)
so, it will print something only when $a is empty and either $b or $c empty
Just force the comparison order/precedence by using parentheses:
if( $a == "" and ( $b == "" or $c == "" ) )
But you would be better off using && and ||:
if( $a == "" && ( $b == "" || $c == "" ) )

difference between <>, =!, ==! in php

Hi I am doing a conditional
if ($row['ConsignadaCaja'] === 'si' && $row['Estado'] ==! 'I') {
$pago = 0;
}
It does not work as I want
So I try using
if ($row['ConsignadaCaja'] === 'si' && $row['Estado'] =! 'I') {
$pago = 0;
}
But it does not work either
Finally I try this:
if ($row['ConsignadaCaja'] === 'si' && $row['Estado'] <> 'I') {
$pago = 0;
}
It is works but do not why?
==! is not the operator you are thinking of - You are mixing two operators here.
== checks for equality, and ! is a logical not operation. So, you are actually performing one of these, thanks to operator precedence putting ! higher than the comparison or assignment operators:
if( $row['Estado'] = (!'I')) // Assigns the inverted value of 'I' to $row['Estado']
if( $row['Estado'] == (!'I')) // Compares the inverted value of 'I' to $row['Estado']
Instead, you should be using != or !==, depending on if you want type-coercion to occur.
Note that if you see that <> is working as expected, this is identical to the != operator.

PHP/MySQL -- What does || mean?

I am trying to set up a simple php/mysql database on my website and the example has a line that says:
if (!$searchtype || !?searchterm) {
What are the ||??? I tried to copy and paste them but they cause syntax errors. Noob question I know, but would love an answer nonetheless!
its ||(OR) logical operator
$a || $b Or TRUE if either/both $a or $b is TRUE.
and it should be
if (!$searchtype || !searchterm) {
as in op this Comment you can write this by
I think the code actually is :
if (!$searchtype || !$searchterm) {
|| means OR operator, meaning conditions is satisfied if either of the condition is true.
Its an OR logical operator for handling conditions:
Ex :
$a || $b
Or TRUE if either $a or $b is TRUE.
See : http://php.net/manual/en/language.operators.logical.php
for more details

Categories