This question already has answers here:
“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
                                
                                    (28个答案)
                                
                        
                                2年前关闭。
            
                    
我有一个问题。
我有一个字段,您可以在其中输入以下内容:

word|secondword|third|fourth
and|more|and|more


所以我认为做这样的爆炸应该没问题:

$linebyline = explode("\n", $_POST['message']);
for ($i=0;$i<100;$i++){
  $exploded = explode("$delimiter", $linebyline[$i]);


for循环中,我添加了一条语句,如下所示:

$query = ("insert into homesweet(phone,sensitive,vagisil) values('$result','$exploded[$experience]','$exploded[$name]');


但是,如果我将其复制粘贴到MysQL中,它将起作用。如果我通过PHP使用它,则不会。抛出只是无法执行查询。另外,它说:


  PHP注意:未定义的偏移量


和:


  PHP注意:未定义的索引


无法使用的查询:$query = ("insert into homesweethome(trashcan,exp,seller,name,zip,city,state,country,dobmonth,dobyear,ssn,address,price,phone) values('$result','$exploded[$exp]','$username','$exploded[$name]','$exploded[$zip]','$exploded[$city]','$exploded[$state]','$exploded[$country]','19','1990','$exploded[$ssn]','$exploded[$address]','$exploded[$price]','$exploded[$phone]'");

最佳答案

您可以使用定界符替换接收到的字符串中的所有换行符,并且只能使用explode()一次。

$receivedString = "word|secondword|third|fourth
and|more|and|more";

$linebyline = preg_replace('/\s+/', '|', $receivedString);


$exploded = explode("|", $linebyline);

$query = "INSERT INTO table_name(column0, column1, column2, column3, column4)
VALUES('$exploded[0]', '$exploded[1]', '$exploded[2]', '$exploded[3]', '$exploded[4]')";

echo $query;

关于php - MySQL不接受PHP查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44769088/

10-09 22:45