gpt4 book ai didi

php - 无效参数和非法偏移量

转载 作者:行者123 更新时间:2023-11-28 03:01:37 25 4
gpt4 key购买 nike

我的 Web 应用程序中有一个页面,我在其中输入了一些文本 <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>

现在,当我运行并测试我的页面时,第一个错误是:

Warning: Invalid argument supplied for foreach()

所以我改了result6 = 0;进入result6 = array(); .但我收到了同样的警告。

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

Warning: Illegal string offset 'item_code'

Warning: Illegal string offset 'item_name'

Warning: Illegal string offset 'buy_price'

Warning: Illegal string offset 'item_sold'

我试过这段代码 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com