我的web应用程序中有一个页面,我在<input type="search"
中键入一些文本,然后从下拉列表中选择一个类型来完成搜索操作,然后当我单击search submit按钮时,web应用程序转到MySQL并搜索行,然后在HTML表中向我显示它们。
我有一个SQL代码,它放在我的HTML页面的顶部:
<?php
require_once('../include/global.php');
$result6 = 0;
$message = '';
if(isset($_POST['submit_search']))
{
$selected = $_POST['select_search_type'];
$search = $_POST['search_item'];
try
{
if($selected=="item_name")
{
$query = ("SELECT * FROM inventory WHERE item_name= :search");
$stmt6 = $conn->prepare($query);
$stmt6->bindParam(":search", $search);
$count6 = $stmt6->execute();
$result6 = $stmt6->fetch(PDO::FETCH_ASSOC);
}
if($selected=="item_cat")
{
$query = ("SELECT * FROM inventory WHERE item_cat= :search");
$stmt6 = $conn->prepare($query);
$stmt6->bindParam(":search", $search);
$count6 = $stmt6->execute();
$result6 = $stmt6->fetch(PDO::FETCH_ASSOC);
}
if($selected=="item_code")
{
$query = ("SELECT * FROM inventory WHERE item_code= :search");
$stmt6 = $conn->prepare($query);
$stmt6->bindParam(":search", $search);
$count6 = $stmt6->execute();
$result6 = $stmt6->fetch(PDO::FETCH_ASSOC);
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
在网页HTML表单中,我有以下代码:
<form action="" method="post">
<input type="search" name="search_item"/>
<select id="selectW" name="select_search_type">
<option value="select_search">select</option>
<option value="item_name">product</option>
<option value="item_cat">category</option>
<option value="item_code">code</option>
</select>
<input type="submit" name="submit_search" value="search"/>
</form>
</div>
<div id="section2">
<form action="" method="post">
<table class="imagetable" border="1" cellspacing="2" cellpadding="2">
<tr>
<th align="center">product</th>
<th align="center">code</th>
<th align="center">price</th>
<th align="center">number</th>
</tr>
<?php foreach ($result6 as $show_search) { //var_dump($show_search);?>
<tr>
<td align="center"><?php echo $show_search['item_name'] ?></td>
<td align="center"><?php echo $show_search['item_code'] ?></td>
<td align="center"><?php echo $show_search['buy_price'] ?></td>
<td align="center"><?php echo $show_search['item_sold'] ?></td>
</tr>
<?php } ?>
</table>
</form>
现在,当我运行并测试我的页面时,第一个错误是:
警告:为foreach()提供的参数无效
所以我把
result6 = 0;
改成了result6 = array();
。但我也收到了同样的警告。然后,当我在搜索文本框中键入名称时,提交搜索时出现以下错误:
警告:非法的字符串偏移量“项目代码”
警告:非法的字符串偏移量“item_name”
警告:非法字符串偏移“购买价格”
警告:字符串偏移量“item_salled”非法
我试过这个代码in this link but still the same error
然后我看到了this link here for the illegal offset但它与我的代码无关(不值得测试,不同的代码)。
如有任何帮助,我们将不胜感激。
最佳答案
请注意,您正在对[http://php.net/manual/es/pdostatement.fetch.php]fetch的结果进行迭代,而对整个结果集没有,这就是为什么您会得到“非法字符串偏移量”
删除'$result6=$stmt6…',然后请迭代语句。。。
<?php foreach ($stmt6 as $show_search) { //var_dump($show_search);?>
<tr>
<td align="center"><?php echo $show_search['item_name'] ?></td>
<td align="center"><?php echo $show_search['item_code'] ?></td>
<td align="center"><?php echo $show_search['buy_price'] ?></td>
<td align="center"><?php echo $show_search['item_sold'] ?></td>
</tr>
<?php } ?>
关于php - 无效的参数和非法的偏移量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34494310/