【问题标题】:How to post an array to MySQL如何将数组发布到 MySQL
【发布时间】:2011-03-26 21:04:56
【问题描述】:

我们如何将以下数组发布到 mysql(columns: id, date, amount)...

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

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

顺便说一句,我们正在使用以下字段获取此数组...

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

感谢您的支持。

【问题讨论】:

  • 你有一个二维数组,所以我假设你必须有一个双 foreach 循环。
  • 您的知识水平如何?您是否已经在使用特定的 MySQL 扩展(mysql_*mysqli_*PDO 也许)?或者这是一个完整的 PHP/MySQL 菜鸟问题?
  • @fireeyedboy 是的,我可以使用 mysqli,但我想在将其发布到 db 之前在 php 中循环。
  • @JCOC611:这没有必要。循环一个元素并同时检查另一个元素中是否存在相同的索引可以正常工作。
  • @fireeyedboy 你能贴出代码吗?

标签: php mysql


【解决方案1】:

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

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

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

【讨论】:

  • 如果数组中的计数为 5,那么它会向 mysql 发布 5 行吗?
  • 是的,它遍历所有数组索引。
  • @seoppc:你得到了另外三个关于 mysql/mysqli 的答案。也检查一下。如果您想对提供的示例代码表示赞赏(在其他答案中),请使用赞成箭头。
  • @mario 你能看看 fireeyedboy 发布的 mysqli 代码,如果它好用的话。
  • @mario,我非常喜欢你的代码,它又快又小,如果你能用 mysqli 更新 pdo,那会很有帮助。谢谢
【解决方案2】:

试试这个:

<?php
$arr = $_POST['payment'];
$insert = array();
foreach($arr['date'] as $key => $date){
   $date = mysql_real_escape_string($date);
   $amount = (int)$arr['amount'][$key];
   $insert[] = "('$date', $amount)";
}

mysql_query("INSERT INTO table(date, amount) VALUES " 
    . implode(",", $insert));

【讨论】:

  • 你为什么使用内爆?因为我们只想插入一个包含已发布数据的新行
  • 因为在我的示例中,我们在每次执行 mysql_query 时插入多个(全部)值。
  • 在 count(array) = 5 的情况下会生成多少行
  • 它将创建与 $_POST['payment']['date'] 中的元素一样多的行。
  • 谢谢!你能帮我解决这个问题吗... stackoverflow.com/questions/5436726/adding-prefix-to-mysql-data 因为我无法在那里得到正确的答案。
【解决方案3】:

这是一个 mysqli 版本(未经测试,我从不使用 mysqli,但我相信应该可以)

// the sql statement for the insert
$sql = 'INSERT INTO yourTable ( `id`, `date`, `amount` ) VALUES( null, ?, ? )';

// prepare the query for binding to the placeholders later on
$stmt = $mysqli->prepare( $sql );

// bind some variables to the placeholders
$stmt->bind_param( 'si', $date, $amount );

// loop through all date values
foreach( $yourArray[ 'date' ] as $key => $value ) )
{
    // check if there is an equivalent index in $yourArray[ 'amount' ]
    // change to isset if you want to guard againt null values
    if( !array_key_exists( $key, $yourArray[ 'amount' ] ) )
    {
        // throw an exception of perhaps use some other form of error
        throw new Exception( 'missing parameter' );
    }

    // give the previously bound variables values for this query execution
    $date = $yourArray[ 'date' ][ $key ];
    $amount = $yourArray[ 'amount' ][ $key ];

    // execute it!
    $stmt->execute();
}

【讨论】:

  • 如果数组计数等于 5,它会在表中创建 5 行吗,我只想更清楚地了解代码。谢谢
  • @seoppc:此代码将自动插入与示例数组一样多的行。 foreach 循环遍历所有 $yourArray[ 'date' ] 元素(并提取相应的 $yourArray[ 'amount' ] 元素)并在每次迭代时执行查询。
【解决方案4】:
function mysql_insert_array($into, $columns, $array){

        $column = explode(",", $columns);

        foreach($column as $c){
            $c = $post[$c];
            $v .= "'$c',";
        }
        $v = trim($v, ",");

        $query = ("INSERT INTO $into($columns) VALUES($v)");
        mysql_query($query);

        return mysql_insert_id();
}

$post["username"] = "Little Bobby Tables";
$post["password"] = "x' OR 1 = 1";

mysql_insert_array("users", "username,password", $post)

编辑:哦,二维数组,不能用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2020-12-15
    • 2023-03-11
    • 1970-01-01
    • 2017-06-30
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多