致命错误:字符串不支持 [] 运算符 [英] Fatal error: [] operator not supported for strings

查看:28
本文介绍了致命错误:字符串不支持 [] 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中获取信息,将其保存在数组中并以具有循环结构的形式回显,当我尝试将修改后的信息保存到数据库时遇到了问题.

I'm getting information from database, saving it in array and echoing it in a form with loop structure and I'm having problems when I try to save the modified information to database.

我收到此错误:

致命错误:[] 运算符不支持...中的字符串

Fatal error: [] operator not supported for strings in....

代码:

$namesql1 = "SELECT name,date,text,date2 FROM table WHERE something= '$something'";
$nameresult1 = mysql_query($namesql1);
$countrows = mysql_num_rows($nameresult1);
while ($row = mysql_fetch_array($nameresult1, MYSQL_ASSOC)) {
    $name[] = $row['name'];
    $date[] = $row['date'];
    $text[] = $row['text'];
    $date2[] = $row['date2 '];
}

/** SOME CODE HERE **/

    
$wrotesql = "UPDATE service_report SET  name ='$name' , $date = '$date',$text = '$text[$nro]', ser_date = '$date2[$nro]' WHERE something = '$something')";

$wroteresult = mysql_query($wrotesql);

有人能告诉我我做错了什么吗?

Could somebody please give me a hint what I'm doing wrong?

推荐答案

当您尝试对字符串使用短数组推送语法时,您会收到此错误.

You get this error when attempting to use the short array push syntax on a string.

例如这个

$foo = 'foo';
$foo[] = 'bar'; // ERROR!

我敢猜测您的 $name$date$text$date2 中的一个或多个 变量已初始化为字符串.

I'd hazard a guess that one or more of your $name, $date, $text or $date2 variables has been initialised as a string.

再次查看您的问题,看起来您实际上并不想将它们用作数组,因为您将它们进一步视为字符串.

Looking again at your question, it looks like you don't actually want to use them as arrays as you're treating them as strings further down.

如果是这样,请将您的分配更改为

If so, change your assignments to

$name = $row['name'];
$date = $row['date'];
$text = $row['text'];
$date2 = $row['date2'];

<小时>

使用 empty-index 数组推送语法的 PHP 7 和代码似乎存在一些问题.


It seems there are some issues with PHP 7 and code using the empty-index array push syntax.

明确地说,这些在 PHP 7+ 中工作正常

To make it clear, these work fine in PHP 7+

$previouslyUndeclaredVariableName[] = 'value'; // creates an array and adds one entry

$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry

什么不起作用试图使用empty-index推送任何声明为字符串、数字、对象等的变量,即

What does not work is attempting to use empty-index push on any variable declared as a string, number, object, etc, ie

$declaredAsString = '';
$declaredAsString[] = 'value';

$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';

$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';

都会导致致命错误.

这篇关于致命错误:字符串不支持 [] 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆