查询返回数组中的记录

时间:2013-02-01 01:56:57

标签: php mysql

我有一个多选列表(链接),它将值发布到$ links。然后我想在表'link'上运行一个查询,返回与$ 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";
        }
     }
}

1 个答案:

答案 0 :(得分:-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) . ')';