如何将数组发布到MySQL [英] How to post an array to MySQL

查看:107
本文介绍了如何将数组发布到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何将以下数组发布到mysql(列:id,日期,金额)...

How can we post following array to mysql(columns: id, date, amount)...

Array
(
   [date] => Array
   (
      [0] => 03/27/2011
      [1] => 04/27/2011
    )
   [amount] => Array
   (
      [0] => 5000
      [1] => 5000
    )
)

我们如何从这个数组中获取数据(日期和数量)以使用foreach发布到MySQL中?

How can we get data(dates and amounts) from this array to post intomysql using foreach?

我们正在使用以下字段来获取此数组...

we are getting this array using following fields btw...

<input type="text" name="payment[date][]" /> <input type="text" name="payment[amount][]" />
<input type="text" name="payment[date][]" /> <input type="text" name="payment[amount][]" />

感谢您的支持.

推荐答案

在第一个子数组上循环,并保留$i索引以访问第二个子数组:

Loop over the first subarray, and keep the $i index to access the second alike:

$stmt = $pdo->prepare("INSERT INTO things (date,amount) VALUES (?,?)");

foreach ($array["date"] as $i=>$_ignore)
{
    $stmt->execute( array($array["date"][$i], $array["amount"][$i]) );
}

这篇关于如何将数组发布到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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