0

我有一个多选列表(链接),它将值发布到 $links。然后,我想对表“链接”运行查询,返回与 $links 中的值匹配的记录。我正在使用以下代码,但没有得到任何结果:

<select name="links[]" size="9" multiple="multiple" id="links">
            <?php 
$query = mysql_query("SELECT * from link ORDER BY link_title ASC");
for($i=0;$i<mysql_num_rows($query);$i++) {
$row=mysql_fetch_assoc($query);
?>
            <option value="<?php echo $row['link_pk']; ?>"><?php echo $row['link_title']; ?></option>
            <?php 
}
?>
          </select> 

和提交代码:

$author_pk = $_GET['author_pk'];
$title = $_POST['title'];
$topic_introduction = $_POST['topic_introduction'];
$selected_topic = $_POST['selected_topic'];
$links = $_POST['links'];
$majors = $_POST['majors'];
$majors_string = implode(",", $majors);
$sub_discipline = $_POST['sub_discipline'];




 if(isset($_POST['submit'])){

$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";
 $result_links = mysql_query($query_links, $connection) or die(mysql_error());
 while ($row_links = mysql_fetch_assoc($query_links)){
     $topic_links = array();
     $topic_links[$row_links['url']] = $row_links;

     } if($result_links){

$topic = $topic_introduction . '<p>' . $topic_links;

        $query = "INSERT INTO topic (topic_pk,title,topic,majors,sub_discipline_fk,author_fk,created)
          VALUES ('','$title','$topic','$majors_string','$sub_discipline','$author_pk',NOW())";
        $result = mysql_query($query, $connection) or die(mysql_error());
        if($result){
            $message = "- The topic '" . $title . "' has been created";
        }
     }
}
4

1 回答 1

-1

这一行是错误的:

while ($row_links = mysql_fetch_assoc($query_links)){

...因为 $query_links 实际上是一个字符串(您的 SQL 查询)。您应该改用 $result_links。

此外,$query_links 字符串的定义不正确。您必须在开头使用的字符串文字的末尾使用相同的分隔符。代替:

$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";

尝试:

$query_links = 'SELECT * FROM link WHERE link_pk IN (' . implode(',', $links) . ')';
于 2013-02-01T02:15:06.003 回答