How to select values ​​where another value is an array?

I am trying to select values ​​where other POST values ​​are an array, I do not know what is wrong with my request giving me this error. I am trying to find out which courses have just been added to the table. I have five input in the form.

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 } } } ?> 
+6
source share
1 answer

First, you create an array of course codes that you want to receive; For simplicity, I leave border checks:

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

Then you prepare the expression that you will use:

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

The following main loop:

 foreach ($codes as $code) { $stmt->bind_param('s', $code); assert($stmt->execute()); $res = $stmt->get_result(); while ($row = $res->fetch_assoc()) { // ... } } 
0
source

Source: https://habr.com/ru/post/985737/


All Articles