drt5813 2016-09-22 05:29
浏览 36

如何使用foreach输出将多条记录插入mysql?

I have a small app where the user adds 3-4 ticket in a single Form via the 'Add Another Ticket' button. These text boxes are generated via Jquery .append() and each ticket has 5 input boxes in it. Code Below

<form  action="ticket-addcode.php" method="post" enctype="multipart/form-data" class="my-form">
<span id="tixmegaform">
<input type="hidden" name="Eventid" value="<?php  echo $eventid; ?>" />
<div class="AddRow">
<label>Package Name</label>
    <input class="requierd" type="text" name="ticketgroup" placeholder="Enter the Package Name. Most Preferably Event name" id="EN" value="<?php echo $ticketgroup; ?>">
</div>
<h5>Ticket 1</h5>
<div class="AddRow">
<label>Ticket Title</label>
    <input class="requierd" type="text" name="tname[]" placeholder="Enter the Package Name. Most Preferably Event name" id="EN">
</div>
<div class="AddRow">
<label>Ticket Desc</label>
    <input class="requierd" type="text" name="tdesc[]" placeholder="Enter the Details" id="EN">
</div>
<div class="AddRow">
<label>Ticket Cost</label>
    <input class="requierd" type="text" name="tprice[]" placeholder="Enter the ticket Cost in Numbers. No Currency" id="EN">
</div>
<div class="AddRow">
<label>Ticket Book URL</label>
    <input class="requierd" type="text" name="turl[]" placeholder="Enter the URL without http" id="EN">
</div>

<div class="AddRow">
<label>Time</label>
    <input type="text" class="left requierd" name="eventTime[]" id="timeformatExample1" placeholder="Start">
</div>

<div class="AddRow">
<label>Date</label>
    <input class="requierd" type="text" name="tdate[]" placeholder="Enter the Package Name. Most Preferably Event name" id="from">
</div>


</span>
<input type="submit" name="submit" class="add_field_button_submit">
</form>
</div>

</div>

</div>
<a href="#"><div class="add_field_button">Add Another Ticket</div></a>
</div>

So, when I hit the Submit button, a nested foreach runs through an array generated by the submit button. I'm able to fetch the values out of the array but somehow the output is not useful to me. Below is the foreach & the output

foreach ($_POST as $pos => $newarr) {
foreach($newarr as $res => $final){
echo $pos.'-----'.$final.'<br>';


}
}

Output

**tname-----VIP tix
tdesc-----Early Bird Desc
tdesc-----VIP Desc Tix
tprice-----5000
tprice-----10000
turl-----google.com
turl-----yahoo.com
eventTime-----00:30:00
eventTime-----00:00:45
tdate-----2-2-2016
tdate-----3-3-2016**

I tried to use an Insert Statement, but it just won't work. It seems that my foreach is resolving the sub array (tname array) and the outer array. If my foreach could just fetch values of different key and not the entire subarray, I would be able to insert the record into db.

Can you guide me on how to achieve this and where to put the INSERT Statement?

</div>
  • 写回答

2条回答 默认 最新

  • du0531 2016-09-22 07:00
    关注

    I don't think looping over $_POST as you have done will do you any good. Notice how the order of your information coming out makes it difficult?

    Instead pick any of your array fields to determine first the number of tickets you have. Then use the number of tickets for iterating over each ticket. This way you can get the index of each group (ticket) of related information together. With the index, you can get all the information related for the group.

    Once you have the necessary information, you can either store each information by doing one insert at a time or by doing one big insert. For simplicity, we shall use the former approach (using PDO).

    Below is a rough and untested sketch of how it might look:

    try {
        $dbh = new PDO($dsn, $user, $password);
        // prepare your SQL statement
        $sth = $dbh->prepare("INSERT INTO table (title, desc, price, url) VALUES(?, ?, ?, ?)");
        // loop over each ticket information
        for ($i = 0, $numTickets = count($_POST['tname']); $i < $numTickets; $i++) {
            $title = $_POST['tname'][$i];
            $desc = $_POST['tdesc'][$i];
            $price = $_POST['tprice'][$i];
            $url = $_POST['turl'][$i];
            // insert information into database
            $sth->execute(array($title, $desc, $price, $url));
        }
    } catch (PDOException $e) {
        // if something goes wrong, add some logic
    }
    

    For more information on PDO, read the documentation.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line