Php 如何解决此错误:(警告:非法字符串偏移量)和(注意:未初始化的字符串偏移量:0)
假设我有search.php和edit.php。 在search.php中,他们可以删除和更新一些记录。 如果用户单击“更新”按钮,系统会将用户重定向到另一个名为edit.php的页面。 我成功地调用了itemid。 但是这种情况发生了Php 如何解决此错误:(警告:非法字符串偏移量)和(注意:未初始化的字符串偏移量:0),php,mysql,web,Php,Mysql,Web,假设我有search.php和edit.php。 在search.php中,他们可以删除和更新一些记录。 如果用户单击“更新”按钮,系统会将用户重定向到另一个名为edit.php的页面。 我成功地调用了itemid。 但是这种情况发生了 <?php $itemid = $_GET["itemid"]; $description = ""; $classification =
<?php
$itemid = $_GET["itemid"];
$description = "";
$classification = "";
$unit = "";
$quantity = "";
$price = "";
$reorder = "";
$status ="";
$record = MYDB::query(
"select
*
from
item
where
itemid = ? ",
array($itemid),
"SELECT"
);
if(count($record) > 0)
{
$record = $record[0];
$description = $description['description'];
$classification = $classification['classification'];
$unit = $unit['unit'];
$quantity = $quantity['quantity'];
$price = $price['price'];
$reorder = $reorder['reorder'];
$status = $status['status'];
}
else
{
echo '<div class="fail">';
echo '<b>FAIL!</b> Item Not Found!</div>';
die();
}
if(isset($_POST["btnsubmit"]))
{
if(isset($_POST["description"])){
$description=ucwords(trim($_POST["description"]));
}
else{
echo "Please Enter description";
}
if(isset($_POST["classification"])){
$classification=ucwords(trim($_POST["classification"]));
}
else{
echo "Please Enter classification";
}
if(isset($_POST["unit"])){
$unit=ucwords(trim($_POST["unit"]));
}
else{
echo "Please Enter unit";
}
if(isset($_POST["quantity"])){
$quantity=ucwords(trim($_POST["quantity"]));
}
else{
echo "Please Enter quantity";
}
if(isset($_POST["price"])){
$price=ucwords(trim($_POST["price"]));
}
else{
echo "Please Enter Price";
}
if(isset($_POST["reorder"])){
$reorder=ucwords(trim($_POST["reorder"]));
}
else{
echo "Please Enter reorder";
}
if(isset($_POST["status"])){
$status=ucwords(trim($_POST["status"]));
}
else{
echo "Please Enter status";
}
}
?>
这看起来像是罪魁祸首
$record = $record[0];
不能将变量设置为无数组,而将其本身设置为具有未初始化值的数组。这简直是胡说八道……:)
你认为你想用那条线做什么
假设$record[0]存在并且它是某种id,您可以
$record_id = $record[0];
但是您正在为所有其他项目使用关联数组,那么0的索引在图片中的位置是什么
您需要执行$record的var_转储来检查您是否得到了预期的结果。这看起来像是罪魁祸首
$record = $record[0];
不能将变量设置为无数组,而将其本身设置为具有未初始化值的数组。这简直是胡说八道……:)
你认为你想用那条线做什么
假设$record[0]存在并且它是某种id,您可以
$record_id = $record[0];
但是您正在为所有其他项目使用关联数组,那么0的索引在图片中的位置是什么
您需要执行$record的var_dump来检查您是否得到了预期的结果。该错误基本上意味着您正在通过一个不存在的键调用数组。我会告诉你那是哪个键,但你没有提供具体的变量
假设我有这个数组:
$array = ('some_real_key' => 'a very important value');
现在,如果我称之为$array['some\u fake\u key']
,假设我们的数组没有some\u fake\u key
,那么它将产生您看到的错误。您的0偏移量也是如此
您正在代码中调用它:
$record = $record[0];
这意味着没有
0
offset,这可能意味着一系列的事情。。。同样,没有提供足够的数据。但它将遵循与上面相同的示例
要解决这些问题,您可以使用array\u key\u exists()
:
这将只输出第一个数组键;并且不会输出错误
更新
考虑到这一点,我认为您的错误可能由以下变量产生:
$record = $record[0];
$description = $description['description'];
$classification = $classification['classification'];
$unit = $unit['unit'];
$quantity = $quantity['quantity'];
$price = $price['price'];
$reorder = $reorder['reorder'];
$status = $status['status'];
您并不是真正从数组中设置它们;您可能想调用$record['key']
,而不是$description=$description['description']代码>
例如,这看起来像:
$description = $record['description'];
// ... so on ...
该错误基本上意味着您正在通过不存在的键调用数组。我会告诉你那是哪个键,但你没有提供具体的变量
假设我有这个数组:
$array = ('some_real_key' => 'a very important value');
现在,如果我称之为$array['some\u fake\u key']
,假设我们的数组没有some\u fake\u key
,那么它将产生您看到的错误。您的0偏移量也是如此
您正在代码中调用它:
$record = $record[0];
这意味着没有
0
offset,这可能意味着一系列的事情。。。同样,没有提供足够的数据。但它将遵循与上面相同的示例
要解决这些问题,您可以使用array\u key\u exists()
:
这将只输出第一个数组键;并且不会输出错误
更新
考虑到这一点,我认为您的错误可能由以下变量产生:
$record = $record[0];
$description = $description['description'];
$classification = $classification['classification'];
$unit = $unit['unit'];
$quantity = $quantity['quantity'];
$price = $price['price'];
$reorder = $reorder['reorder'];
$status = $status['status'];
您并不是真正从数组中设置它们;您可能想调用$record['key']
,而不是$description=$description['description']代码>
例如,这看起来像:
$description = $record['description'];
// ... so on ...
我正在使用该行捕获项目id。。。它可以成功地获取id。那么,当您可以将其称为item_id或其他任何字段名时,为什么要使用0作为索引?它的字段名与您使用的所有其他字段名(关联)相同,而不是索引号?我正在使用该行来捕获项目id。。。它可以成功地获取id。那么,当你可以称它为item_id或任何它的字段名时,为什么要使用0作为索引?它的字段名与你使用的所有其他字段名(关联)一样,而不是索引号?@Samuel Whoops我错过了所有的$unit=$unit['unit']还有我的朋友们…@RobertKevinSieras你是什么意思?@Samuel哎呀我错过了所有的$unit=$unit['unit']还有我的朋友们…@RobertKevinSieras你是什么意思?