无效参数和非法偏移

时间:2015-12-28 13:19:42

标签: php html mysql pdo

我的网页应用程序中有一个页面,我在<input type="search"中输入了一些文本,我从下拉列表中选择,一种类型来完成我的搜索操作,然后当我点击搜索提交按钮时,网络应用程序转到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();。但我得到了同样的警告。

之后,当我在搜索文本框中输入名称时,提交搜索时出现此错误:

  

警告:非法字符串偏移&#39; item_code&#39;

     

警告:非法字符串偏移&#39; item_name&#39;

     

警告:非法字符串偏移&#39; buy_price&#39;

     

警告:非法字符串偏移&#39; item_sold&#39;

我尝试了这段代码in this link but still the same error

然后我看到this link here for the illegal offset,但它与我的代码无关(不值得测试,不同的代码)。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

请注意,你正在迭代[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 } ?>