dongrouyuan5685 2015-12-28 13:19
浏览 42

无效参数和非法偏移

I have a page in my web application, where I type some text inside an <input type="search" and I choose from a drop list, a type to complete my search action, then when I click on search submit button, the web app go to MySQL and search for rows then show them to me in HTML table.

I have this SQL code that it is placed on top of my HTML page:

<?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();
    }
}
?>

And in the webpage HTML form I have this code:

<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>

Now, when I run and test my page, the first error was:

Warning: Invalid argument supplied for foreach()

So I changed result6 = 0; into result6 = array();. but I got the same warning.

Then after that, when I type a name in search text box, I got this error when submitting my search:

Warning: Illegal string offset 'item_code'

Warning: Illegal string offset 'item_name'

Warning: Illegal string offset 'buy_price'

Warning: Illegal string offset 'item_sold'

I tried this code in this link but still the same error

Then I saw this link here for the illegal offset but it is not relevant with my code (Don't worth to test, different code).

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dqwnxdhb88531 2015-12-28 20:45
    关注

    Please be adviced, you are iterating over the result of [http://php.net/manual/es/pdostatement.fetch.php] fetch, and no on the entire resultset, that is why you get 'illegal string offset'

    Remove '$result6 = $stmt6...' and then please iterate over the statement...

    <?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 } ?>
    
    评论

报告相同问题?