我想在一个提交按钮中存储多行。这是购物车页面
在这里,我只添加了简单的代码。我想在数据库中存储多行

的HTML

<form class="form-horizontal" name="project_form"  id="project_form" action="{url r="cart#prepare"}" method="post">
            {foreach from=$user item="user"}
                <div class="col-4 heads"> NAME </div>
                <div class="col-8"> <input type="text" name="name" id="name" placeholder="name" value="{$user.user_name}" > </div>

                <div class="col-4 heads"> Email </div>
                <div class="col-8"> <input type="text" name="email" id="email" placeholder="email" value="{$user.email}" > </div>

                <div class="col-4 heads"> Mobile </div>
                <div class="col-8"> <input type="text" name="cell" id="cell" placeholder="mobile no" value="{$user.mobile}" > </div>
            {/foreach}
                <div class="col-4 heads"> ADDRESS LINE 1 </div>
                <div class="col-8"> <input type="text" name="address1" id="address1"  placeholder="address1" > </div>

                <div class="col-4 heads"> ADDRESS LINE 2 </div>
                <div class="col-8"> <input type="text" name="address2" id="address2" > </div>

                <div class="col-4 heads"> CITY </div>
                <div class="col-8"> <input type="text" name="city" id="city" placeholder="city" > </div>

                <div class="col-4 heads"> STATE </div>
                <div class="col-8"> <input type="text" name="state" id="state"  placeholder="state" > </div>

            <input type="text" id="iinc" name="iinc" value="3">




在这里,我添加了购物车页面的屏幕截图。
php - 如何在Thinkphp中的数据库上存储多行-LMLPHP

不要与html混淆。疑问仅在这里。是的,仅在php函数中。

public function prepare()
{
    if(!$GLOBALS['user_info'])
    {
        app_redirect(url("user#login"));
    }
    else
    {
        $name = $_REQUEST['name'];
        $iinc = $_REQUEST['iinc'];

        for($i=1; $i <= $iinc; $i++)
        {
            $order_id[$i] = mt_rand(100000, 999999);
            $Amount[$i]=$_REQUEST['amount'];

            $list = $GLOBALS['db']->query("insert into payment(name,order_id,amount) Values('".$name."','".order_id[$i]."','".$Amount[$i]."');
        }
    }
}

最佳答案

请像这样更改您的代码

$order_id=array();
 $Amount=array();
        for($i=0; $i < $iinc; $i++)
        {
            $order_id[$i] = mt_rand(100000, 999999);
            $Amount[$i]=$_REQUEST['amount'];

            $list = $GLOBALS['db']->query("insert into payment(name,order_id,amount) Values('".$name."','".order_id[$i]."','".$Amount[$i]."');
        }

关于php - 如何在Thinkphp中的数据库上存储多行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45035477/

10-10 01:52