将PHP数组到MySQL表 [英] Insert php array into mysql table
问题描述
更新:
我忘了提,回声$ matstring输出'65 0.70,科尔斯','34天','14' - 这似乎是正确的语法?
我是一个PHP / MySQL的新手,我认为这是相当基本的,但看了所有关于这一主题的其他计算器问题,并用摆弄了好几个小时不同的版本我的code的我不能明白我做错了。将非常AP preciate任何帮助/建议。
目的:通过从我的PHP数组($矩阵)数据到MySQL表
$矩阵[1] =
([0] => 65.70 [1] => Coles的[2] =&GT 34日内付运[3] => 14)$矩阵[2] =
([0] => 62.70 [1] => Coles的[2] =&GT 13日内付运[3] => 14)$矩阵[3] =
([0] => 12.70 [1] =>西夫韦[2] =&GT 43日内付运[3] => 14)
code:
$ matstring =破灭(,,$矩阵[1]);
$ matstring ='。$ matstring。';
的mysql_query(INSERT INTO澳大利亚(价格,公司,天,体重)VALUES('$ matstring')');
修正code:
$ matstring =破灭(,,$矩阵[1]);的mysql_query(INSERT INTO澳大利亚(价格,公司,天,体重)VALUES('$ matstring'));
(即删除原来的code中的第二行,并把周围的mysql_query的论点双引号)
鸭preciate user1847757的帮助 - 因为他/她指出, $ matstring
本身是正确的,但 VALUES内的单引号('')
正在加入到我原来的code二号线加入 $ matstring
单引号,导致 VALUES(''65.70','科尔斯','34天','14'')
感谢所有您的帮助和放大器;建议
Update: I forgot to mention that echo $matstring outputs '65.70', 'Coles','34 days','14' - which would appear to be the right syntax?
I'm a php/mysql newbie, and I think this is fairly basic, but having read all of the other stackoverflow questions on this topic and fiddling with different versions of my code for several hours I can't understand what I'm doing wrong. Would very much appreciate any help/suggestions. Aim: pass data from my php array ($matrix) into a mysql table
$matrix[1]=
( [0] => 65.70 [1] => Coles [2] => 34 days [3] => 14 )
$matrix[2]=
( [0] => 62.70 [1] => Coles [2] => 13 days [3] => 14 )
$matrix[3]=
( [0] => 12.70 [1] => Safeway [2] => 43 days [3] => 14 )
Code:
$matstring=implode("','",$matrix[1]);
$matstring="'".$matstring."'";
mysql_query('INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')');
Corrected code:
$matstring=implode("','",$matrix[1]);
mysql_query("INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')");
(i.e. delete the second line from original code and put double quotes around the argument of mysql_query)
Appreciate user1847757's help - as s/he pointed out, $matstring
itself was correct, but the single quotes inside of VALUES(' ')
were being joined to the single quotes added to $matstring
in the 2nd line of my original code, resulting in VALUES(''65.70','Coles','34 days','14'')
Thanks all for your help & suggestions
这篇关于将PHP数组到MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!