0

I dont know whether it's a duplicate one or not?

Here is what i'm trying:

 <?php
    session_start();
    include('db.php');
     $valid_data=$_POST['data'];
     if(isset($_POST['data']))
    {
     $list = isset($_SESSION['strtpnt1']) ? $_SESSION['strtpnt1'] : array();
     $_SESSION['strtpnt1'][] =$valid_data;
     $a=implode(',',$_SESSION['strtpnt1']);
    }
  ?>

when i use print_r for $_SESSION['strtpnt1'] i could see like this:

     array (size=5)
     0 => string 'trivandrum' (length=10)
     1 => string 'kochi' (length=5)
     2 => string 'nagercoil' (length=9)
     3 => string 'thrissur' (length=8)

I found nothing wrong with that

when i echoed the imploded variable i find the value like this:

      trivandrum,kochi,nagercoil,thrissur

When i tested the imploded data in wampserver phpmyadmin's sql like this i could get an error message:

  select start from tbl_place where start NOT IN(trivandrum,kochi,nagercoil,thrissur)   

the error message is:

        Unknown column 'trivandrum' in 'where clause'

But the query works when string value is passed ie 'trivandrum','kochi' etc.

i dont know what is wrong with my query ...here is the query part

  $sql21 = "select start from tbl_place where start NOT IN('".$a."')";

3 Answers3

0

There is error in your parameter $a value, all values inside IN() should be quoted and separated by commas.

Change your code like following:

$a=implode("','",$_SESSION['strtpnt1']); // ',' => "','"

$sql21 = "select start from tbl_place where start NOT IN('".$a."');";// no change

// NOW the SQL query will became:  select start from tbl_place where start NOT IN('trivandrum','kochi','nagercoil','thrissur');

Now it should work.

Adarsh Rajput
  • 1,246
  • 14
  • 24
0

For now you can try this but prepared statement is best for it. You can use PDO or MYSQLi . it doesn't work for you because NOT IN needs the list to individually be quoted and separated by commas.

  $a=$_SESSION['strtpnt1'];
  $new_a= "'" . implode("','", $a) . "'";
  $sql21 = "select start from tbl_place where 
 start NOT IN($new_a)";

Similar Ideas :Can I bind an array to an IN() condition?

Proper format for PDO and MySQL IN/NOT IN queries

Community
  • 1
  • 1
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

$sql21 = "select start from tbl_place where start NOT IN('".$a."')";

will querying like this :

$sql21 = "select start from tbl_place where start NOT IN('trivandrum,kochi,nagercoil,thrissur')";

which will treat as a whole string

you have to do in passing variable $a=implode("','",$_SESSION['strtpnt1']);

Pranav Bhatt
  • 715
  • 4
  • 8