AhmAch
AhmAch

Reputation: 107

Sql rating issue

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());
    }
?>

Upvotes: 0

Views: 34

Answers (2)

chris85
chris85

Reputation: 23880

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

Upvotes: 1

RNK
RNK

Reputation: 5792

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)

Upvotes: 0

Related Questions