【问题标题】:Save Dynamic Array To MySQL Database将动态数组保存到 MySQL 数据库
【发布时间】:2014-01-12 06:32:34
【问题描述】:

我有一个动态的form,允许用户输入信息,form 可以多次提交,页面将显示所有输入的数据,感谢$_SESSION。该信息被发送到另一个页面,在提交后它将被保存到 MySQL 数据库中。

我无法保存所有信息。如果我有 3 组数据,它只会将最后一组写入数据库。 如何将整个数组保存到数据库中?

这是显示所有动态信息的页面:

    <?php
        $invoice_no = $_SESSION['invoice'];
        if(isset($_SESSION['order'])) : 

            foreach($_SESSION['order'] as $sav) {
            ?>
            <form action="addrow.php" method="post">
            <label>Length</label><input type="text" name="length" value="<?php echo $sav['length']; ?>" size="2">
            <label>Width</label><input type="text" name="width" value="<?php echo $sav['width']; ?>" size="2">
            <label>Color</label><input type="text" name="color" value="<?php echo $sav['color']; ?>" size="4">
            <label>Quantity</label><input type="text" name="quantity" value="<?php echo $sav['quantity']; ?>" size="2">
            <label>Invoice Is Hidden</label><input type="hidden" name="invoice" value="<?php echo $invoice_no; ?>">
            <input type="hidden" name="total" value="<?php echo $sav['total']; ?>" />
            <input type="hidden" name="PaymentStatus" value="PAID">
            <br>
            <?php } endif; ?>
            <br><br>
            <input type="submit" value="Submit" name="upload">
            </form>

此页面将其保存到数据库中。我不确定如何将数组保存到数据库中,所以我使用相同的代码来显示会话数据并修改它但失败了:

    <?php
require("addrow_info.php");

if(isset($_POST['upload'])) :

$decal = array(
    'length' => $_POST['length'],
    'width' => $_POST['width'],
    'color' => $_POST['color'],
    'quantity' => $_POST['quantity'],
    'total' => $_POST['total'],
    'invoice' => $_POST['invoice'],
    'paymentStatus' => $_POST['PaymentStatus'],
    'submit' => $_POST['upload']
 );

 $_POST['order'][] = $decal;

endif;


if(isset($_POST['order'])) : 
foreach($_POST['order'] as $newOrder) {


// Opens a connection to a MySQL server
$connection=mysql_connect ("localhost", $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

 // Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Insert new row with user data
$query = "INSERT INTO orders (PaymentStatus, invoice_no, length, width, color,   quantity, total ) VALUES  ('".$newOrder['paymentStatus']."','".$newOrder['invoice']."','".$newOrder['length']."', '".$newOrder['width']."', '".$newOrder['color']."', '".$newOrder['quantity']."', '".$newOrder['total']."')";

$result = mysql_query($query);

if (!$result) {
die('Invalid query: ' . mysql_error());

echo "$query";

mysql_close();
}

} endif;

header ("location:/thankyou.php");

?>

我正在阅读有关使用 serialize() 函数的信息,但我不确定这是否最适合我想要完成的任务。我希望每组数据保存在各自列下的一行中。

应该是这样的:

Length    Width    Color    Invoice    Quantity    Total    PaymentStatus
5         5        Green    abc123     1           2.00     PAID <--Each row is a group
6         6        blue     def234     2           3.00     PAID

array 保存到 MySQL 数据库的最佳解决方案是什么?

【问题讨论】:

  • 在数据库中保存数组的最佳解决方案是规范化您的数据,以便您可以运行正确的查询。
  • 顺便说一句,mysql_query 已被弃用。看看 mysqli 或 PDO。

标签: php mysql arrays database session


【解决方案1】:
 <form action="addrow.php" method="post"><?php
    $invoice_no = $_SESSION['invoice'];
    if(isset($_SESSION['order'])) : 

        foreach($_SESSION['order'] as $sav) {
        ?>

        <label>Length</label><input type="text" name="length[]" value="<?php echo $sav['length']; ?>" size="2">
        <label>Width</label><input type="text" name="width[]" value="<?php echo $sav['width']; ?>" size="2">
        <label>Color</label><input type="text" name="color[]" value="<?php echo $sav['color']; ?>" size="4">
        <label>Quantity</label><input type="text" name="quantity[]" value="<?php echo $sav['quantity']; ?>" size="2">
        <label>Invoice Is Hidden</label><input type="hidden" name="invoice[]" value="<?php echo $invoice_no; ?>">
        <input type="hidden" name="total[]" value="<?php echo $sav['total']; ?>" />
        <input type="hidden" name="PaymentStatus" value="PAID">

        <br>
        <?php } endif; ?>
        <br><br><input type="submit" value="Submit" name="upload">
        </form>

       try it and print_r( $_POST ), I think you can make it.

if(isset($_POST['upload'])) {
    print_r($_POST);
}

数组 ( [长度] => 数组 ( [0] => 2 [1] => 3 [2] => 4 ) [宽度] => 数组 ( [0] => 2 [1] => 3 [2] => 3)

for($i=0;$i<count($_POST['length']);$i++){
   $order = array(
       'length'=>$_POST['length'][$i], 
       'width'=>$_POST['width'][$i]),
        //...............
   );
   $sql = "INSERT INTO orders (PaymentStatus, invoice_no, length, width, color,   quantity, total ) VALUES  ('{$order['paymentStatus']}','{$order['invoice']}','{$order['length']}', '{$order['width']}', '{$order['color']}', '{$order['quantity']}', '{$order['total']}')";
   mysql_query($sql);
}

【讨论】:

  • 我该怎么办?
  • 我不需要此页面的帮助。此页面工作正常并发布所有会话数据。我需要帮助才能将此页面中的内容发布到 MySQL 数据库。
  • 您的页面(html)无法成功,请使用此页面发布。我想让你看看这两个页面有什么不同,所以我告诉你使用 print_r($_POST);我认为你可以做最后一个。我不想做所有的工作。我只是给你一个方向。
  • 我在页面上使用了print_r,会话中有3组值,它只返回一组值,具体来说是数组的最后一组。
  • 这是它给我的:Array ( [length] =&gt; 4 [width] =&gt; 3 [color] =&gt; white [quantity] =&gt; 2 [invoice] =&gt; 2654700459Svb2UpGXN0PvcH6 [total] =&gt; 4.80 [PaymentStatus] =&gt; PAID [upload] =&gt; Submit ) 它应该有 3 个 [length] 值和 [width] 等。
猜你喜欢
  • 2013-06-03
  • 2012-09-29
  • 2020-04-17
  • 2016-05-05
  • 2013-05-08
  • 2015-09-22
  • 1970-01-01
  • 2011-10-14
  • 2017-03-22
相关资源
最近更新 更多