将php数组插入mysql表 [英] Insert php array into mysql table

查看:148
本文介绍了将php数组插入mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:
我忘了提及echo $ matstring 输出 '65 .70','Coles','34 days' '14' - 这看起来是正确的语法?

Update: I forgot to mention that echo $matstring outputs '65.70', 'Coles','34 days','14' - which would appear to be the right syntax?

我是一个php / mysql新手,我认为这是相当基本的,但已经阅读所有其他stackoverflow问题这个主题和fiddling与不同的版本我的代码几个小时我不明白我做错了什么。非常感谢任何帮助/建议。

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.

目标:将数据从我的php数组( $ matrix )传入a mysql table

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 )

$matstring=implode("','",$matrix[1]);
$matstring="'".$matstring."'";
mysql_query('INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')');


推荐答案

已更正的代码: / p>

Corrected code:

$matstring=implode("','",$matrix[1]);

mysql_query("INSERT INTO Australia (Price, Company, Days, Weight) VALUES ('$matstring')");

(即删除原始代码的第二行,并在mysql_query )

(i.e. delete the second line from original code and put double quotes around the argument of mysql_query)

感谢user1847757的帮助 - 正如他指出, $ matstring 本身是正确的,但 VALUES('')中的单引号正被加入到添加到 $ matstring 中的单引号我的原始代码的第二行,导致 VALUES(''65.70','Coles','34 days','14'')

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屋!

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