确定。此代码返回为foreach()
提供的无效参数的语法错误我经历了很多这段代码,并且在我的生命中找不到错误。
我还在学习和尝试,所以请保持温柔。
<?php
include ('c1.php');
if ($_COOKIE["auth"] == "1") {
$display_block = "<p>You are an authorized user.</p>";
} else {
header("Location: userlogin.html");
exit;
}
doDB();
$display_block = "<h1>Results</h1>";
if (isset($_POST['search']) && !empty($_POST['search'])) {
foreach ($_POST['search'] as $key => $value) {
if ($value == 1)
$search[] = "$key";
$searchstring = implode(' AND ', $search);
$post_map = array(
'postcode' => 'candididate_contact_details.postcode'
);
if (isset($_POST['postcode']) && !empty($_POST['postcode'])) {
foreach ($_POST['postcode'] as $key => $value) {
if (array_key_exists($key, $post_map))
$search[] = $post_map[$key] . '=' . mysql_real_escape_string($value);
echo $searchstring;
$query = "SELECT candidate_id.master_id, candidate_contact_details.first_name, candidate_contact_details.last_name, candidate_contact_details.home_phone, candidate_contact_details.work_phone, candidate_contact_details.mobile_phone, candidate_contact_details.email FROM candidate_id, candidate_contact_details, qualifications, security_experience, previous_career WHERE qualifications.active = 'finished' and candidate_id.master_id = candidate_contact_details.master_id and candidate_id.master_id = qualifications.master_id and candidate_id.master_id = security_experience.master_id and candidate_id.master_id = previous_career.master_id and $searchstring";
$query_res = mysqli_query($mysqli, $query)
or die(mysqli_error($mysqli));
// $search = mysqli_query($mysqli, $query)or die(mysqli_error($mysqli));
{
$display_block .= "
<table width=\"98%\" cellspacing=\"2\" border=\"1\">
<tr>
<th>Registration Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Home Number</th>
<th>Work Number</th>
<th>Mobile Number</th>
<th>E-Mail</th>
</tr>";
while ($result = mysqli_fetch_array($query_res)) {
$regnum = $result['master_id'];
$first_name = $result['first_name'];
$last_name = $result['last_name'];
$home_phone = $result['home_phone'];
$work_phone = $result['work_phone'];
$mobile_phone = $result['mobile_phone'];
$email = $result['email'];
$display_block .= "
<tr>
<td align=\"center\">$regnum <br></td>
<td align=\"center\">$first_name <br></td>
<td align=\"center\">$last_name <br></td>
<td align=\"center\">$home_phone <br></td>
<td align=\"center\">$work_phone <br></td>
<td align=\"center\">$mobile_phone <br></td>
<td align=\"center\">$email <br></td>
</tr>";
}
$display_block .= "</table>";
}
}
}
}
}
?>
<html>
<head>
<title> Display results</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
答案 0 :(得分:1)
您的表单不会向这些帖子字段发送数组值。最好查看is_array
。
如果您要传递字符串,请使用explode
。
答案 1 :(得分:1)
此错误的原因可能是您使用POST提交的值。
您写道:
if(isset($_POST['search']) && !empty($_POST['search'])){
foreach($_POST['search'] as $key=>$value){
etc...
所以你假设该值是一个数组。
要在post var'search'中实际发送一个数组,你必须像这样定义输入字段:
<input type="text" name="search[]" value="" />
(括号告诉PHP它是一个数组)。
如果您已经知道这一点,那么您应该只检查提交的$ _POST ['search']是否实际上是一个带有is_array()的数组。
答案 2 :(得分:0)
这看起来对我很怀疑:
foreach ($_POST['postcode'] as $key=>$value) {
if (array_key_exists($key, $post_map))
答案 3 :(得分:0)
确定。此代码返回为foreach()
提供的无效参数的语法错误
这通常是因为你将一个不是数组的东西传递给foreach
,它需要一个数组。两种解决方案:
在is_array
来电中接听来电。
if(is_array($_POST['search']) {
foreach($_POST['search'] as $key=>$value){
首先将变量强制转换为数组。
foreach((array)$_POST['search'] as $key=>$value){
你需要让$_POST['search']
实际上包含一个数组,以便任何此类事件发生。请参阅@ enyo的答案,了解如何做到这一点。