如何在foreach循环中声明mysql中的变量

时间:2017-09-30 11:14:03

标签: php mysqli

我是一名学生,也是php的新手。我希望有人可以帮助我使我的代码工作。错误是:"未定义索引:test_id in ..."和#34;为foreach()提供的参数无效..."

这是我的php代码:

<?php
  require 'core/init.php';

  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "palo";
  $conn= mysqli_connect($servername, $username, $password, $dbname);
     if(!$conn){
       die("Connection failed: ". mysqli_connect_error());
     }

  if(isset($_POST['submit'])) {
     $score = 0;

     foreach($_POST['test_id'] as $qID => $qVal) {
        $qID =  (int) $qID;
        $qVal = (int) $qVal;

        $learnerResponse= "SELECT COUNT(*) AS rightAnswer FROM tquestions 
        WHERE test_id = $qID AND correctanswer = $qVal";
        $result=mysqli_query($conn, $learnerResponse);
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 

        if($row['rightAnswer']) {
           $score++;
        }
     }
  }
?>

以下是提交的表格:     

  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "palo";
  $conn= mysqli_connect($servername, $username, $password, $dbname);
  if(!$conn){
    die("Connection failed: ". mysqli_connect_error());
  }
  $sql = "SELECT test_id, question, optiona, optionb, optionc, optiond FROM 
  tquestions ORDER BY RAND()";      
  $result = mysqli_query ($conn, $sql); // Run the query
  if (!$result) {
    printf("Error: %s\n", mysqli_error($conn));
    exit();
  }
?>

<!DOCTYPE html>
<body>
<div class="col-md-auto col-md-offset-1 col-centered">
        <?php if (!empty($_SESSION['msg'])) { ?>
            <div class="alert alert-info"><?php echo $_SESSION['msg']; ?>

我没有包含其他部分,因为它只有接口设计代码 这是继续:

     <form action="results.php" method="POST">
        <div class="tabcontent">
            <table class="table table-hover">
            <tbody>
                <?php $num=1; ?>
                <?php while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                    $question = $row['question'];
                    $test_id = $row['test_id'];
                    $optiona = $row['optiona'];
                    $optionb = $row['optionb'];
                    $optionc = $row['optionc'];
                    $optiond = $row['optiond'];
                ?>
                        <div class="form-group">   
                            <h3 name="q<?php echo $num;?>" style="text-indent: 40px;"><?php echo $num,'. ', $question; ?> </h3>
                        </div>
                        <div class="form-group">
                            <label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
                                <input style="font-size: 18px;" type="radio" name="ans<?php echo $test_id;?>" value="<?php echo $optiona;?>"><?php echo $optiona;?> 
                            </label>
                            <br>
                            <label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
                                 <input style="font-size: 18px;" type="radio" name="ans<?php echo $test_id;?>" value="<?php echo $optionb;?>"><?php echo $optionb;?> 
                            </label>
                            <br>
                            <label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
                                 <input style="font-size: 18px;" type="radio" name="ans<?php echo $test_id;?>" value="<?php echo $optionc;?>"><?php echo $optionc;?> 
                            </label>
                            <br>
                            <label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
                                 <input style="font-size: 18px;" type="radio" name="ans<?php echo $test_id;?>" value="<?php echo $optiond;?>"><?php echo $optiond;?> 
                            </label>
                            <br>
                        </div>
                    <?php $num++; ?>
                <?php
                }
                ?>
            </tbody>
            </table>
        </div>
        <br>
        <div class="form-group"><center>
            <input class="btn btn-success" type="submit" name="submit" value="Submit"></center>
        </div>
        </form>
        </div>
    </div>
    </div>
</body>

提前谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

 <?php

  require 'core/init.php';

  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "palo";
  $conn= mysqli_connect($servername, $username, $password, $dbname);
   if(!$conn){
     die("Connection failed: ". mysqli_connect_error());
   }

  if(isset($_POST['test_id'])) {
     $score = 0;
     $test_id = $_POST['test_id'];
     if( !empty($test_id)):
         foreach($test_id as $qID => $qVal) {
            $qID =  (int) $qID;
            $qVal = (int) $qVal;

            $learnerResponse= "SELECT COUNT(*) AS rightAnswer FROM tquestions 
            WHERE test_id = $qID AND correctanswer = $qVal";
            $result=mysqli_query($conn, $learnerResponse);
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 

            if($row['rightAnswer']) {
               $score++;
            }
         }
      endif;
  }
?>

答案 1 :(得分:0)

您的输入未被命名为test_id[],因此$_POST['test_id']无法找到它们。你需要改变:

name="ans<?php echo $test_id;?>"

为:

name="test_id[<?php echo $test_id;?>]"