MySQL / PHP <select>不显示提交的值

时间:2017-06-28 18:10:05

标签: php mysql

我正在完成一项学校任务,我遇到了一些问题。我有一个表单的PHP代码,选中后,将选定的结果发送到MySQL数据库,然后循环并显示结果。唯一的问题是,它不显示所选的&lt;选项&gt;,而是显示所有四个选项。 这是我的代码: &LT; PHP include_once(connection.php); if(($ _SERVER [&#34; REQUEST_METHOD&#34;] ==&#34; POST&#34;)&amp;&amp;(isset($ _ POST [&#39; card_catalog_form&#39;]))){     $ card_name = mysqli_real_escape_string($ conn,$ _POST [&#39; card_name&#39;]);     $ card_label = mysqli_real_escape_string($ conn,$ _POST [&#39; card_label&#39;]);     $ insert_card_genre_query = sprintf(&#34; INSERT到card_catalog(card_name,card_label)VALUES(&#39;%s&#39;,&#39;%s&#39;)&#34;,         $ CARD_NAME,         $ card_label);     $ insert_card_genre = mysqli_query($ conn,$ insert_card_genre_query)或die(mysqli_error($ conn));     $ last_record = mysqli_insert_id($ conn); } $ card_genre_query =&#34;选择card_genre.genre_id,card_label来自`card_genre`命令,由card_label asc&#34 ;; $ card_genre = mysqli_query($ conn,$ card_genre_query)或死(mysqli_error($ conn)); $ get_card_genre_query =&#34; SELECT card_catalog.id,card_catalog.card_name,card_catalog.card_label,card_genre.genre_id from card_catalog right join card_genre on card_catalog.card_label = card_genre.card_label&#34 ;; $ get_card_genre = mysqli_query($ conn,$ get_card_genre_query)或死(mysqli_error($ conn)); ?&GT; &lt;!DOCTYPE html&gt; &LT; HTML&GT; &LT; HEAD&GT;     &lt; meta charset =&#34; UTF-8&#34;&gt;     &lt; title&gt;玛娜俱乐部的市场&lt; / title&gt;     &lt; link rel =&#34; stylesheet&#34;类型=&#34;文本/ CSS&#34; HREF =&#34;样式表/ tmp.css&#34;&GT; &LT; /头&GT; &LT;身体GT;     &lt;?php include(templatestuff / top_of_tmp.php); ?&GT;     &lt;主&GT;         &LT; H1&GT;产品:其中/ H1&GT;         &lt;节&GT;             &lt; ul id =&#34; products_list&#34;&gt;                 &lt; li&gt;&lt; b&gt;产品1:&#34; Jack-In-The-Mox&#34;&lt; / b&gt;&lt; / li&gt;                 &lt; li&gt;&lt; b&gt;产品说明:&#34;为Jack-in-the-Mox滚动六面模具。在1,牺牲Jack-in-the-Mox并失去5点生命。否则,Jack-in-the-Mox具有以下效果之一。将此能力视为法术力来源......&#34;&lt; / b&gt;&lt; / li&gt;                 &lt; img src =&#34; productimages / jackinthemox.jpeg&#34; alt =&#34; Jack In The Mox&#34; /&gt;             &LT; / UL&GT;         &LT; /节&gt;         &LT; DIV&GT;             &lt; h2&gt;您在寻找什么卡?&lt; / h2&gt;             &lt; form action =&#34;&lt;?php echo htmlspecialchars($ _ SERVER [&#34; PHP_SELF&#34;]);?&gt;&#34;方法=&#34;后&#34; ENCTYPE =&#34;多部分/格式数据&#34;&GT;               &LT;字段集&GT;                 &lt; p&gt;&lt; b&gt;卡名称是什么? &lt; input type =&#34; text&#34;名称=&#34; CARD_NAME&#34;&GT;&LT; / B&GT;&LT; / p为H.                 &LT; p为H.                   &lt; p&gt;&lt; b&gt;卡类型:&lt; / b&gt;&lt; / p&gt;                   &lt; select name =&#34; card_genre&#34;&gt;                       &lt;?php while($ row_card_genre = mysqli_fetch_assoc($ card_genre)){?&gt;                       &lt; option value =&#34;&lt;?php echo $ row_card_genre [&#39; genre_id&#39;];?&gt;&#34;&gt;&lt;?php echo $ row_card_genre [&#39; card_label&# 39];?&GT;&LT; /选项&GT;                       &lt;?php}?&gt;                   &LT; /选择&GT;                 &LT; / p为H.                 &lt; p&gt;&lt; input type =&#34;提交&#34;&gt;&lt; / p&gt;                 &lt; input type =&#34; hidden&#34;名称=&#34; card_catalog_form&#34;&GT;               &LT; /字段集&GT;             &LT; /形式&GT;             &LT; PHP                 if($ last_record){                     echo&#34;&lt; p&gt;&lt; b&gt;您刚刚创建了表单查询#&#34; 。 $ last_record。&#34;&lt; / b&gt;&lt; p&gt;&#34 ;;                 }             ?&GT;             &lt; p&gt;您正在提交表单                 &LT; PHP                     date_default_timezone_set(&#39;美国/纽约&#39);                     回音日期(&#39; g:i a \ o \ n l,F j,Y&#39;);                 ?&GT;             &LT; / p为H.         &LT; / DIV&GT;         &LT; PHP         $ query =&#34; SELECT card_catalog.card_name,card_catalog.card_label,card_genre.genre_id FROM card_catalog,card_genre&#34 ;;         $ result = mysqli_query($ conn,$ query)或die(mysqli_error($ conn));         while($ row = mysqli_fetch_array($ result)){             echo $ row [&#39; card_name&#39;]。 &#34; - &#34;。 $行[&#39; genre_id&#39;];             echo&#34;&lt; br /&gt;&#34 ;;         }         ?&GT;     &LT; /主&GT;     &lt;?php include(&#39; templatestuff / bottom_of_tmp.php&#39;); ?&GT; &LT; /体&GT; &LT; / HTML&GT;` (如果你想看到包含问题的网站,你可以去这里) 任何帮助或建设性批评都将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

这可能会指向正确的方向:

if (isset($_POST['card_genre'])) {
    $query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id FROM card_catalog, card_genre WHERE card_genre.genre_id = ?";
    $stmt   = mysqli_prepare($conn, $query);
    $stmt->bind_param('s', $_POST['card_genre']);
    $stmt->execute();
    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {
        echo $row['card_name']." - ".$row['genre_id'];
        echo "<br />";
    }
} else {
    $query = "SELECT card_catalog.card_name, card_catalog.card_label, card_genre.genre_id FROM card_catalog, card_genre";
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));

    while ($row = mysqli_fetch_array($result)) {
        echo $row['card_name']." - ".$row['genre_id'];
        echo "<br />";
    }
}