简体   繁体   English

如何从 select 表单中获取值到 SQL 查询

[英]How to get a value from select form into SQL query

I have a project where a user logs in and can then answer multiple quizzes.我有一个用户登录的项目,然后可以回答多个测验。 These quizzes have a select form where they can select how many questions do they want.这些测验有一个 select 表格,他们可以在其中 select 他们想要多少问题。 My problem is when they select how many questions they want i don't know how to transform that into a PHP variable and then use it in SQL QUERY.我的问题是当他们 select 他们想要多少问题时,我不知道如何将其转换为 PHP 变量,然后在 SQL QUERY 中使用它。 my SQL QUERY looks something like that:我的 SQL QUERY 看起来像这样:

SELECT questions FROM quiz_questions ORDER BY RAND() LIMIT=?

Please explain when you answer the question thanks:)请在回答问题时解释一下谢谢:)

I tried to set a variable of select form to $number.我试图将 select 形式的变量设置为 $number。 But when I tried to test it with echo I didn't get anything.I use iframe because I need page not to reload since i have some javascript functions later.但是当我尝试用 echo 测试它时,我什么也没得到。我使用 iframe 因为我需要不重新加载页面,因为我稍后有一些 javascript 功能。

  <form target="frame" action="UvodniNivo.php" method="post">
    <div class="center">
      <label>izberite št. vprašanj:&nbsp;</label>
        <select name="number" id="value">
          <option value="5">5</option>
          <option value="10">10</option>
          <option value="15">15</option>
        </select>
    </div>
    <button onclick="hide()" name="start-btn" type="submit">Začni</button>
  </form>
  <iframe name="frame"></iframe>

                       <!--Start of the quiz-->


  <div id="SHOW">
  <?php

    if(isset($_POST['start-btn'])){
    $select="SELECT * FROM quiz_question ORDER BY RAND() LIMIT=?";
    $result= mysqli_query($conn, $select);
    $count= mysqli_num_rows($result);
    if($count < 1)
    {
      echo "ERROR";
    }
    else
    {
      while($row= mysqli_fetch_array($result))
      {
        echo $row[2]."<br />";
      }
    }
    ?>
  </div>

EDIT: $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);编辑: $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

DB_HOST,DB_USER,DB_PASS,DB_NAME all have the correct value since login/register works. DB_HOST、DB_USER、DB_PASS、DB_NAME 都具有正确的值,因为登录/注册工作。

first of all, you are using prepared statement, so you have to initialize a stmt, prepare the statement, bind the statement with the parameter(s) and execute..首先,您使用的是准备好的语句,因此您必须初始化一个 stmt,准备语句,将语句与参数绑定并执行..

<?php
if(isset($_POST['start-btn'])){

$select="SELECT * FROM quiz_question ORDER BY RAND() LIMIT=?";
$numOfQuestions = $_POST['number']; //make sure you sanitize this
$q = mysqli_stmt_init($conn);
mysqli_stmt_prepare($q, $select);
mysqli_stmt_bind_param($q, 'i', $numOfQuestions );
mysqli_stmt_execute($q); 

$result = mysqli_stmt_get_result($q);
if (mysqli_num_rows($result) != 0) {
     // do stuff here
}

?>

$number = $_POST['number']; $number = $_POST['number'];

limit = $number;限制 = $数字;

SELECT * FROM quiz_question ORDER BY RAND() LIMIT=? SELECT * FROM quiz_question ORDER BY RAND() LIMIT=?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM