我需要帮助来解决select查询where IN子句?
1( 现在的问题是选择查询不工作where pid IN ($pid)
2( 第二个问题是CCID input text
是空的?
当i 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>
第2页。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 }?>
-
使用
echo $pid=implode (",",$new_product).'<br>';
-
由于上述问题,
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
循环之外;
$new_product = array();
for($i=0; $i<$max; $i++){
$new_product[]=$_SESSION['pid1'][$i];
}
$pid=implode (', ', $new_product);