由于某种原因,我的for循环项目没有被执行。
// Message to the client
$message = "<html><body>";
$message .= "<p>";
foreach($data as $item) {
$message .= "<b>
➛<a style='color:#FF6400; text-decoration: none' href='{$item['link']}'>{$item['title']}</a></b><br>";
$message .= "Format: {$item['format']} <br>";
$message .= "Cost: ${$item['costs']} <br>";
$message .= "Estimated Duration: {$item['duration']} hours<br>";
$message .= "<br>";
}
以下是定义$data
的地方:
// Converting the array into individual strings
$formats = implode("','", $formatsArray);
$topics = implode("','", $topicsArray);
// Check to see if user is careless about the format
// Always use prepare statements to store data
if($_POST['costs'] == "any") {
if(in_array("any",$formatsArray)) {
$resources = $con->prepare("SELECT * FROM resources WHERE
(format IN ('Video', 'Podcast', 'Blog/Article', 'forum', 'Online Course'))
AND (topic IN (?))");
$resources->bind_param('s', $topics);
}
// If user has a specific format in mind, then proceed with the following
else {
// Prepare the statement
$resources = $con->prepare("SELECT * FROM resources WHERE
(format IN (?))
AND (topic IN (?))");
// Bind the statement
$resources->bind_param('ss',$formats, $topics);
}
}
else if($_POST['costs'] == "free") {
if(in_array("any",$formatsArray)) {
// Prepare statement
$resources = $con->prepare("SELECT * FROM resources WHERE
(format IN ('Video', 'Podcast', 'Blog/Article', 'forum', 'Online Course'))
AND (topic IN (?))
AND cost like 0");
// Bind paramater
$resources->bind_param('s', $topics);
}
// If user has a specific format in mind, then proceed with the following
else {
// Prepare the statement
$resources = $con->prepare("SELECT * FROM resources WHERE
(format IN (?))
AND (topic IN (?))
AND (cost like 0)");
// Bind the statement
$resources->bind_param('ss', $formats, $topics);
}
}
else if($_POST['costs'] == "100") {
if(in_array("any",$formatsArray)) {
// Prepare statement
$resources = $con->prepare("SELECT * FROM resources WHERE
(format IN ('Video', 'Podcast', 'Blog/Article', 'forum', 'Online Course'))
AND (topic IN (?))
AND (cost <= 100)");
// Bind paramater
$resources->bind_param('s', $topics);
}
// If user has a specific format in mind, then proceed with the following
else {
// Prepare statement
$resources = $con->prepare("SELECT * FROM resources WHERE
(format IN (?))
AND (topic IN (?))
AND (cost <= 100)");
// Bind paramater
$resources->bind_param('ss', $formats, $topics);
}
}
// Execute the prepare statement
$resources->execute();
while($row=$result->fetch_assoc())
{
$data[] = array(
'format' => $row['format'],
'title' => $row['title'],
'costs' => $row['cost'],
'stage' => $row['stage'],
'topic' => $row['topic'],
'duration' => $row['duration'],
'link' => $row['link']
);
}
我确定查询运行成功,因为没有返回任何错误,我确实存储了项目。我已将查询转换为使用预准备语句。