MySQL不接受PHP查询

时间:2017-06-26 21:40:03

标签: php mysql mysqli

我有一个问题。
我有一个字段,你可以输入这样的东西:

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]'");

1 个答案:

答案 0 :(得分:1)

您可以使用分隔符替换接收到的字符串中的所有新行字符,并仅使用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;