我需要帮助解决选择查询哪里IN子句?

问题描述:

我需要帮助解决选择查询where IN子句?我需要帮助解决选择查询哪里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 }?> 
+0

使用IN语句在其中声明SE​​LECT语句。它在数据库中运行时是否工作? mysql_error的错误是什么意思? – andrewsi 2013-04-29 14:32:29

+0

@andrewsi在'input type text name ccid'中没有显示'ccid'? – 2013-04-29 14:34:54

+0

这是因为您的查询失败。回应您正在生成的查询。在数据库中运行它。 – andrewsi 2013-04-29 14:37:02

  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环路;

+0

它可能承认澄清,没有胶水参数的'implode'默认为空字符串。 – 2013-04-29 14:41:56

+1

+1。我试图让他为自己工作:) – andrewsi 2013-04-29 14:42:12

+0

这就是我,踏上教学时刻。 – 2013-04-29 14:43:03

$new_product = array(); 
for($i=0; $i<$max; $i++){ 
    $new_product[]=$_SESSION['pid1'][$i]; 
} 
$pid=implode (', ', $new_product);