0

我需要帮助来解决选择查询 where IN 子句?

1)现在的问题是选择查询不起作用where pid IN ($pid)
2)第二个问题是CCID input text空的?

当我echo $pid=implode ($new_product).'<br>';显示PID哪些创建为 $_SESSION['pid']..!

使用时出错 ($pid)

CCID Problem
You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near ')' at line 1

创建会话 Pid 的页面

session_start();

$_SESSION['pid1'][]=$_POST['pid'];


function get_id($id){
$result1=mysql_query("select * from products where id='$id'")
or die("Id Problem"."<br/><br/>".mysql_error());
$results1= array();
$k=0; // add the new line
while($row1=mysql_fetch_assoc($result1)){
$results1[$k] =$row1['id'];
$k++;
}
return $results1;
}

$pid1=get_id($id);

<form method="post">

<input type="hidden" name="pid[]" value="<?php echo $pid1?>" />
</form>

Page2.php

session_start();
if(is_array($_SESSION['pid1'])){
$max=count($_SESSION['pid1']);  
for($i=0; $i<$max; $i++){
$new_product=$_SESSION['pid1'][$i];
echo $pid=implode ($new_product).'<br>';
}}

$result=mysql_query("SELECT id AS ccid FROM cart where pid IN ($pid) ") 
or die("CCID Problem"."<br/><br/>".mysql_error());

while($row=mysql_fetch_array($result)){
?>

<input type="text" name="ccid[]" value="<?php echo $row['ccid'];?>" />

<?php }?>
4

2 回答 2

3
  1. 利用 echo $pid=implode (",",$new_product).'<br>';

  2. CCID input text由于上述问题,它是空的。

假设您有array(1, 2 3)会话中的 pid。正在发生的事情是:

你用:

$pid = implode($new_product);

这将通过连接没有任何分隔符的数组值来创建字符串。所以你得到:

$pid = 123

您需要的是它们之间的逗号。所以你必须使用:

$pid=implode (",",$new_product);

这里的第一个参数是逗号,它用作将数组值保存在字符串中的粘合剂,因此结果变为:

$pid=1,2,3;

您需要在带有IN子句的查询中使用这个字符串。

还将使pid 移出循环的$pid=implode (",",$new_product); for

于 2013-04-29T14:39:51.510 回答
0
$new_product = array();
for($i=0; $i<$max; $i++){
   $new_product[]=$_SESSION['pid1'][$i];
}
$pid=implode (', ', $new_product);
于 2013-04-29T14:45:00.657 回答