php - 循环两倍 - 将 double 写入 MySQL 表

标签 php mysql

简而言之,我的代码似乎循环了两倍(应该写两行的时候写了四行)。这应该是一个简单的解决方案,但我运气不好。

这是我的 php 循环。 . .一定是一件非常简单但看不见的事情,没有人能够找到为什么这个婴儿不工作:

                //query statement before the for loop
                $stmt="INSERT INTO o70vm_invoices_invoices 
                (`id`, `created_by`, `user_id`, `added`, `to_name`, `to_address`, `invoice_num`, `real_invoice_num`, `from_name`, `from_address`, `from_num`, `invoice_date`, `publish`, `notes`, `template_id`, `taxes`, `start_publish`, `end_publish`, `currency_before`, `currency_after`, `status`, `to_email`, `to_company`, `from_phone`, `from_url`, `from_email`, `discount`, `invoice_duedate`, `admin_notes`, `to_city`, `to_state`, `to_country`, `to_vatid`, `to_zipcode`, `rec_year`, `rec_month`, `rec_day`, `rec_nextdate`, `is_recurrent`) VALUES ";

                // loop through number of invoices user selected to create
                for($x = 0; $x < $invoiceCount; $x++) 
                        {

                            // add the user identified days to each invoice
                            $date->modify("+7 days");
                            $invoiceDateNew = $date->format ('Y-m-d 00:00:00');
                            $invoiceDueDateNew = $date->format ('Y-m-d H:m:s');
                            $startPubNew = $date->format ('Y-m-d 00:00:00');

                            // getting the values per row
                            $ValuesAddToQuery[] ="(NULL, '792', '$userID', '$todayDate', '$parentName', 'unknown address', '0000', '0000', '', '', '', '".$invoiceDateNew."', '1', '', '2', '', '".$startPubNew."', '0000-00-00 00:00:00', '$', '', '', '$email', '$childName', '', '', '', '0.00', '".$invoiceDueDateNew."', '', '', '', '', '', '', '0', '0', '0', '0000-00-00', '0')";

                            }

                            $stmt .= implode(',',$ValuesAddToQuery);

                            mysql_query($stmt) or exit(mysql_error());

我将发票数量存储为:

    $invoiceCount  

我已经回显了 $invoiceCount 的值,并且该值始终与用户输入的值相同。 IE,用户选择创建 2 个发票,在变量中显示 2 个发票,但在 MySQL 表中创建 4 个发票。

更陌生:当我检查受以下影响的行时:

 mysql_affected_rows()

它返回用户选择的发票数量/行(不是我能看到的实际行添加到 MySQL 表中)。例如,当添加了四行时,它会说“2”行受到影响。

更加狂野。 . .当我回显 MySQL 查询时:

   echo $stmt;

我的查询还显示,当用户选择要添加的两行但代码实际写入了 4 行时,只添加了两行。

冒险,我什至尝试对数组进行切片以查看是否可以更改发送的代码:

                                //implode the values into the statement
                            $stmt .= implode(',',$ValuesAddToQuery);

                            //limit the length of the array
                            array_slice($ValuesAddToQuery,0,2);

                            mysql_query($stmt) or exit(mysql_error());

而且,您猜对了,它绝对不会改变任何东西。我将 array_slice 放在 implode 语句之上。同样,当我只想要 2 行时,输入的 4 行没有变化。

我看得越多,就越看不出这段代码为什么会翻倍。

任何帮助,非常感谢。

有关我的一些输入字段和我正在做的事情的详细说明,请按照以下步骤操作:

首先,我让用户选择要复制多少行并根据需要更新发票日期。我正在使用这些输入字段获取重复发票的 FREQUENCY(7 天、14 天或 30 天)值和 DURATION(要创建/复制的发票数量):

                <select name="freqOfInvoices">
                    <option value="7">Weekly</option>
                    <option value="14">Bi-Weekly</option>
                    <option value="30">Monthly</option>
                </select>

    <input type="number" title="numberOfInvoices" name="numberOfInvoices" size="2" id="numberOfInvoices" value="numberOfInvoices" />

对于我希望添加 x 天数的三个日期,我有类似的输入字段:

        // assigning variables  
        $freqOfInvoices = htmlentities($_POST['freqOfInvoices'], ENT_QUOTES);
        $numberOfInvoices = htmlentities($_POST['numberOfInvoices'], ENT_QUOTES);
        $invoiceDate = htmlentities($_POST['invoice_date'], ENT_QUOTES);
        $invoiceDueDate = htmlentities($_POST['invoice_duedate'], ENT_QUOTES);
        $startPub = htmlentities($_POST['start_publish'], ENT_QUOTES);


        //assigning number of invoices
        $countInvoices=$numberOfInvoices;

最佳答案

看来您可能只需要 1 个循环来构造值。

//query statement before the foreach loop
$stmt="INSERT INTO o70vm_invoices_invoices (`id`, `.....`, etc) VALUES ";

$ValuesAddToQuery = [];

for($x = 0; $x < $arrayLength; $x++) {
    // add the user identified days to the date
    $date->modify("+7 days");
    $invoiceDateNew = $date->format ('Y-m-d 00:00:00');

    $ValuesAddToQuery[]="(NULL, '....', ".$invoiceDateNew.")";
}

$stmt .= implode(',',$ValuesAddToQuery);

mysql_query($stmt) or exit(mysql_error());

关于php - 循环两倍 - 将 double 写入 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294255/

相关文章:

c# - 将所有 datagridview 值插入 mysql 数据库

php - 无法在 Laravel 中捕获 Cartalyst\Sentinel\Checkpoints\NotActivatedException

php - $this->headTitle() 不起作用

php - 从图像中剪切任何形状(Imagik/Gd)

php - DELETE * FROM TABLE WHERE this=that 不起作用

mysql - 如何在sql中从周数和日期获取日期?

php - 存储未知长度的数组

php - 错误 : [ng:areq] Argument 'SidebarController' is not a function, 未定义

mysql - 如何选择价格高于 'Movie3' 的标题和价格

在没有明显错误的情况下,用户创建期间的 MySql 语法错误