创建一个数组,但我不知道如何将该数组传递给SELECT语句

时间:2018-08-20 18:50:25

标签: php sql

我对编码非常陌生,因此请保持谨慎。我正在从www.allenmurrow.com对我的项目进行建模。

我正在为我的课程制作一个问题数据库。学生选择他们想做的问题数量以及他们想做的问题的类型。

我正在尝试使用复选框来选择主题。这是表格

<form name="initial" id="gfform" method="post" action="quiz.php" enctype="application/x-www-form-urlencoded">
        <div class="theform">

                <div><br/>
                    <div class="questions">
                        <span>How many questions?<br></span>

                        <select name="count">
                            <br>
                            <option value="5" selected="selected">5</option>
                            <option value="10">10</option>
                            <option value="15">15</option>
                            <option value="20">20</option>
                            <option value="25">25</option>
                            <option value="30">30</option>
                            <option value="50">50</option>
                            <br>
                        </select>
                    </div>
                </div>
                <div>
                    <span class='checkall'><a href='javascript: checkall();'>Check All</a> / <a href='javascript: uncheckall();'>Un-check All</a></span>
                </div>
                <div class="topiclist">

                        <div><input type="checkbox" name="topic[]" value="LOA"/><span>LOA</span></div>
                        <div><input type="checkbox" name="topic[]" value="LGA"/><span>LGA</span></div>
                        <div><input type="checkbox" name="topic[]" value="Elec"/><span>Electrical</span></div>
                        <div><input type="checkbox" name="topic[]" value="RC"/><span>Reactor Control</span></div>
                        <div><input type="checkbox" name="topic[]" value="Rx"/><span>Reactor</span></div>
                        <div><input type="checkbox" name="topic[]" value="Stm"/><span>Steam</span></div>
                        <div><input type="checkbox" name="topic[]" value="ECCS"/><span>ECCS</span></div>
                        <div><input type="checkbox" name="topic[]" value="Cnmt"/><span>Containment</span></div>
                        <div><input type="checkbox" name="topic[]" value="Water"/><span>Water Systems</span></div>
                        <div><input type="checkbox" name="topic[]" value="HVAC"/><span>HVAC & Radwaste</span></div>
                        <div><input type="checkbox" name="topic[]" value="TS"/><span>Tech Specs</span></div>
                        <br>
                </div>
                <div class="searchfilter">
                    Limit results to those containing the following text:  <input type="text" name="txtSearch"/>
                </div>
           </div>

             标准测验-开始一个随机测验,其中包括与您的过滤条件匹配的问题。

                    <input type="submit" name="Submit" value="Start Quiz" onClick="_gaq.push(['_trackEvent', 'GF', 'Custom', 'BWR']);"/>

        </div>



这是我连接到数据库并运行选择查询的方式。

<?php

define('DB_NAME', 'quizquestions');
define('DB_USER', 'root'); # myd2n7jhklvu
define('DB_PASSWORD', 'password'); #S@ndlot18
define('DB_HOST', 'localhost');


$link = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD);


if (!$link) {
        die ('could not connect:' . mysqli_error());
}

$db_selected = mysqli_select_db($link, DB_NAME)

    if (!$db_selected) {
    die('Can\'t use') . DB_NAME;
}

$limitValue = $_POST['count'];
$qType = $_POST['topic'];
$sql = "SELECT question_id, questionType, question, answerA, answerB, answerC, answerD
                        FROM questions
                        WHERE questionType IN ($qType)
                        ORDER BY RAND() LIMIT $limitValue";
$result  = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {

while ($row = mysqli_fetch_array($result)) {
    echo "QID: " . $row["question_id"]. "<br>". "Question Type: " . $row["questionType"]. "<br>" . "Question: " . $row["question"]. "<br><br><br>";
    echo "A) " .$row["answerA"]. "<br>" . "B) " .$row["answerB"]. "<br>" . "C) " .$row["answerC"].  "<br>" . "D) " .$row["answerD"]. "<br><br><br>";
}
}

mysqli_close($link);

当我运行它时,出现错误PHP通知:数组到字符串的转换;这很有意义,因为复选框创建了一个数组,但是我不知道如何将该数组传递给SELECT语句。

我已经尝试过内爆和IN函数,但是我对此感到沮丧。我知道这可能是我所缺少的简单事物。

感谢任何人都可以提供的帮助。

1 个答案:

答案 0 :(得分:0)

感谢GolezTrol,我能够根据他的输入使它正常工作。以下是我用来使其正常工作的内容。

$type = $_POST['topic'];
$qType = join("','", $type);
$sql = "SELECT question_id, questionType, question, answerA, answerB, answerC, answerD 
       FROM questions
       WHERE questionType IN ('$qType') 
       ORDER BY RAND() LIMIT $limitValue";
$result  = mysqli_query($link, $sql);