2

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.

Community
  • 1
  • 1
  • 1
    before a foreach you'd better to test the existance of your variable with a if isset($myvariable) – Sylvain Martin Dec 28 '15 at 13:29
  • nope, still the same –  Dec 28 '15 at 13:39
  • have you tried to debug with a echo "
    ";var_dump($_POST);echo "
    "; to show the value of your post data ?
    – Sylvain Martin Dec 28 '15 at 14:05
  • still the same error, even var_dump($show_result) is not showing –  Dec 28 '15 at 14:17
  • What does it look like if you do a `var_dump($result6)` ? It might be possible that your PHP search script is not running properly or returning the result you believe it is. Also, I would absolutely recommend that you use a conditional to display your results as @SylvainMARTIN suggested. However, as you are defining the variable globally in your PHP, i would use `if( ! is_empty($result6) ) { run your foreach(); }` to only run that if the array has data inside of it. – Lionel Ritchie the Manatee Dec 28 '15 at 15:52
  • From the troubleshooting you've done, it seems pretty clear that your `$result6` variable is empty and not set. So either your queries aren't running or aren't returning any data. Remember, the way you have them written, they will only return EXACT matches. – Lionel Ritchie the Manatee Dec 28 '15 at 15:56
  • I did use your conditions, but still getting the same result, I hope to get more help –  Dec 29 '15 at 06:20

1 Answers1

0

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