user2249079
user2249079

Reputation:

select query is not working through Where IN Clause?

need help to solve select query problem but problem is that i can't fetch idsrows from database through WHERE IN (pid) clause. Select query fetching all ids from database from IN (pid)?

1)if session pid is 29 then it should be select only 27 id why it is showing all ids if session pid is 29?

2) How do i select multiple id through two different PID?

Page1 Where Session Pid Creating

$_SESSION['pid']=array();   
$_SESSION['pid'][]= implode (",",$_POST['pid']);

Page2 Where I Want To Use Select Query

$pid = join(',',$_SESSION['pid']); 
$result=mysql_query("SELECT id AS wid FROM mywishlist where pid IN (pid)")
while($row=mysql_fetch_array($result)){
    <input type="text" name="wid[]" value="<?php echo $row['wid']//[$j]; ?>" />
<?php  }?>

Page3 Where I Want To Use Wid

$max=count($_REQUEST['wid']);
for($a=0; $a<$max; $a++){
    $query = mysql_query("UPDATE mywishlist SET     
        cusername='".$_SESSION['username']."',uid='".$_SESSION['id']."', 
        email='".$email."'  where id='".$_REQUEST['wid'][$a]."'") 
    or die ("Cart Email Query");
}

Databse Image

enter image description here

Page2 Live Image

enter image description here

Upvotes: 0

Views: 146

Answers (2)

Hackerman
Hackerman

Reputation: 12295

This:

$pid = join(',',$_SESSION['pid']); 
$result=mysql_query("SELECT id AS wid FROM mywishlist where pid IN (pid)")

Should be:

$pid = join(',',$_SESSION['pid']); 
$result=mysql_query("SELECT id AS wid FROM mywishlist where pid IN ($pid)")

The reason why it shows all the result it's because you have pid in (pid)....taking pid as the field pid on your table, so for every row on the first row you said:

pid value = 25
SELECT id AS wid FROM mywishlist where pid IN (25)
pid value = 26
SELECT id AS wid FROM mywishlist where pid IN (26)

And for all the rows the same...you are comparing with the value pid in you table.

---------------------------UPDATE-----------------------------

Because this:

SELECT id AS wid FROM mywishlist where pid IN ($pid) 

If you make an echo to $pid, it shoul looks like, as example:

24,25 

Then you select should looks like:

SELECT id AS wid FROM mywishlist where pid IN (24,25)   

Ooops, you code should be more safely if you use mysql_real_scape_string

"SELECT id AS wid FROM mywishlist where pid IN (".mysql_real_scape_string($pid).")"; 

I added this fiddle if you dont know how to works with an array of inputs:

http://phpfiddle.org/main/code/2fd-p20

--------------------------UPDATE2--------------------------------

With this code on my fiddle you have an array of inputs:

<?php
if(isset($_POST['friend']))
{ $arr = array();
 foreach ($_POST['friend'] as $value) {
   if(!empty($value))
$arr[] = $value;
 }

 //here you have you array of values in your session var

 $_SESSION['pid'] = implode(",",$arr);
 }
?>

Then only use it this way:

$pid = $_SESSION['pid'];
$result=mysql_query("SELECT id AS wid FROM mywishlist where pid IN (".$pid.")")

PS: Dont use mysql extension...go for mysqli or PDO.

Upvotes: 1

RafH
RafH

Reputation: 4544

Problem is that you use pid and not $pid in the where clause of your query.

$result=mysql_query("SELECT id AS wid FROM mywishlist where pid IN ($pid)")

Upvotes: 3

Related Questions