如何选择其他值为数组的值?

时间:2015-04-23 01:42:58

标签: php mysql

我正在尝试选择另一个POST值是数组的值,我不知道我的查询有什么问题给我这个错误。我想知道哪些课程刚刚添加到表格中。我在表格中有五个输入。

Notice: Trying to get property of non-object in C:\Apache\htdocs\xxx\addcourse.php on line 262

这是我的代码

  <?php 
    if(isset($_POST['Submit'])) {

      $code= isset($_POST['code']) ? $_POST['code'] : '';
      $coursecode = isset($_POST['coursecode']) ? $_POST['coursecode'] : '';
      $both=$code[$x] .' '. $coursecode[$x];

      $sqlcourses = "SELECT * FROM courses where course_code='$both' ORDER BY course_id DESC LIMIT 5 ";
      $resultcourses = $mysqli->query($sqlcourses);


        if ($resultcourses->num_rows > 0) {


            while($row = $resultcourses->fetch_assoc()) {

                ?>
                </p>
                <p>&nbsp;</p>
                <p>&nbsp;  </p>
                <table width="415" border="0">
                <tr>
                <?php
                $courses=$row["course_code"];
                echo $courses;
                ?>
                    </div>
                </tr>
                  </table>
              <?php 
            }
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

首先,构建一个要检索的课程代码数组;为简单起见,我暂时不进行边界检查:

$codes = [];
foreach ($_POST['code'] as $k => $code) {
    $codes[] = $code . ' ' . $_POST['coursecode'][$k];
}

然后,您准备将要使用的声明:

$stmt = $mysqli->prepare("SELECT * 
  FROM courses 
  WHERE course_code = ?
  ORDER BY course_id DESC 
  LIMIT 5");

接下来是主循环:

foreach ($codes as $code) {
    $stmt->bind_param('s', $code);
    assert($stmt->execute());

    $res = $stmt->get_result();
    while ($row = $res->fetch_assoc()) {
      // ...
    }
}