So I am trying to use a result from an SQL search $search_course
and then having that as the WHERE LIKE
part of the next statement $Search_Module
.
once the $Search_Module
query has finished I would like the results to be in a list which I know should be coded correctly.
I have tried the following code but the search returns no values;
Any help with this would be much appreciated, thanks in advance.
$search_course = "
SELECT title, id, wyl, overview, module, course, careers
FROM course
LEFT JOIN cm
ON course.id=cm.course
WHERE id LIKE '%".$_POST['submitcourseselection']."%'";
$result = $mysqli->query($search_course) or die($mysqli->error);
$display_course = $result->fetch_assoc();
//Searches the module table in the DB for modules within the course
$Search_Module = "
SELECT id, title, level, credits
FROM module
WHERE id LIKE '".$search_course['module']."'";
$M_Results = $mysqli->query($Search_Module) or die($mysqli->error);
$ModuleList = '';
while ($MResults = $M_Results->fetch_assoc()) {
$ID = $MResults['id'];
$Title = $MResults['title'];
$Level = $MResults['level'];
$Credits = $MResults['credits'];
$ModuleList.='<div><h2>'.$Title.'</h2></div>';
}
you don't need LIKE in your second query and you can do all by one query.
$search_course = "
SELECT id, title, level, credits
FROM module
WHERE id IN (SELECT module
FROM course
LEFT JOIN cm
ON course.id=cm.course
WHERE id LIKE '%".$_POST['submitcourseselection']."%'")";
in your where clause WHERE id LIKE '".$search_course['module']."'";
, you are referencing an array location of a string $search_course
, which is your SQL statement.
I think you meant to reference your result array $display_course
, so try this:
$search_course = "
SELECT title, id, wyl, overview, module, course, careers
FROM course
LEFT JOIN cm
ON course.id=cm.course
WHERE id LIKE '%".$_POST['submitcourseselection']."%'";
$result = $mysqli->query($search_course) or die($mysqli->error);
$display_course = $result->fetch_assoc();
//Searches the module table in the DB for modules within the course
$Search_Module = "
SELECT id, title, level, credits
FROM module
WHERE id LIKE '".$display_course['module']."'";
$M_Results = $mysqli->query($Search_Module) or die($mysqli->error);
$ModuleList = '';
while ($MResults = $M_Results->fetch_assoc()) {
$ID = $MResults['id'];
$Title = $MResults['title'];
$Level = $MResults['level'];
$Credits = $MResults['credits'];
$ModuleList.='<div><h2>'.$Title.'</h2></div>';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.