如何选择另一个值为数组的值?

时间:2021-09-25 15:33:21

I'm trying to select values where another POST values are an array, I do not know what is wrong with my query giving me this error. I'm trying to know what courses are just added to table. I have five inputs in the form.

我在尝试选择另一个POST值为数组的值,我不知道我的查询有什么问题导致了这个错误。我想知道哪些课程是刚刚加到表格里的。我有5个输入。

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

Here is my code

这是我的代码

  <?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 个解决方案

#1


0  

First, you build an array of the course codes that you want to retrieve; I'm leaving off the boundary checks for simplicity:

首先,构建要检索的课程代码的数组;为了简单起见,我省略了边界检查:

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

Then, you prepare the statement you will use:

然后,你准备好你将要使用的语句:

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

Followed by the main loop:

然后是主循环:

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

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

#1


0  

First, you build an array of the course codes that you want to retrieve; I'm leaving off the boundary checks for simplicity:

首先,构建要检索的课程代码的数组;为了简单起见,我省略了边界检查:

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

Then, you prepare the statement you will use:

然后,你准备好你将要使用的语句:

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

Followed by the main loop:

然后是主循环:

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

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