0

我想在我的 SQL 查询的 WHERE 位中列出 for 语句的所有结果 ($photosToPromote[$i])。

目前 for 语句输出结果的文本列表,如果在前一页上只选择了一张照片,则 SQL 会选择 $photosToPromote 中的一张。如果选择了多张照片,则 FOR 语句将它们全部列出,但 SQL 不会找到它们并显示图像。

在将其放入 SQL 之前,我是否需要使用 mysql_real_escape_string?我该怎么做呢?

$photosToPromote = $_POST['promotePhoto'];
if(empty($photosToPromote)) 
{
echo("<p class=\"error\">You didn't select any photos so go back and start again!");
} 
else
{
$N = count($photosToPromote);

echo("You selected $N photos(s): ");
for($i=0; $i < $N; $i++)
{
    echo($photosToPromote[$i] . " ");
}
}

$queryUserPhotos = mysql_query("SELECT photoID FROM photos WHERE photoid='$photosToPromote[$i]' AND (auth = '5' OR auth = '2' OR auth = '4') ORDER BY auth DESC") or die("Something went wrong...please try this again later!");

while($resultUserPhotos = mysql_fetch_array($queryUserPhotos)){

<img src=\"/$imgpath/$resultUserPhotos[photoID].jpg\" alt=\"Your Photo\"/>

}
4

1 回答 1

0

你可以试试这个:

$c = '';
foreach ($photosToPromote as $p) {
    $c .= " photoid=$p OR ";
}
$c .= '1=2';//false condition to close $c by a condition never true
$q = "SELECT photoID FROM photos WHERE ( $c ) AND (auth = '5' OR auth = '2' OR auth = '4') ORDER BY auth DESC";
$queryUserPhotos = mysql_query($q) or die("Something went wrong...please try this again later!");
于 2013-03-24T15:17:44.843 回答