doulieyu0881
2017-11-27 20:04
浏览 46
已采纳

如何为数组的每个元素插入数据库?

I have a booking system and need to insert the details into mysql database. All of my variables are strings and one of them is an array - so I need to insert the same information for each array element ($ticketnumber). I have tried doing it with a foreach loop, but it doesn't seem to work. How do I do this with only one INSERT statement as people may choose different number of tickets to buy?

Here is part of the code:

<?php

$day = $_SESSION['date'];
$time = $_SESSION['time'];
$name = $_REQUEST['name'];
$ticketnumber = $_REQUEST['tickets']; //this is the array variable

foreach ($ticketnumber as $ticket){
    $sql = "INSERT INTO table VALUES ('$name', '$day', '$time', '$ticket');";
    $handle = $conn->prepare($sql);
    $handle->execute();

}
?>

The values of my array in this case are(it varies depending on how many tickets you check):

array(2) { [0]=> string(3) "010" [1]=> string(3) "011" }

This matched the value of $ticket as well, why is that happening?

My session and connection to the database are established before this. I also tried replacing the array variable with a string and the insert statement works.

Any help is appreciated!

Thank you.

图片转代码服务由CSDN问答提供 功能建议

我有一个预订系统,需要将详细信息插入mysql数据库。 我的所有变量都是字符串,其中一个是数组 - 所以我需要为每个数组元素插入相同的信息($ ticketnumber)。 我尝试过使用foreach循环,但它似乎不起作用。 我如何只使用一个INSERT语句,因为人们可能会选择不同数量的票来购买?

以下是代码的一部分:

  &lt;?php 
 
 $ day = $ _SESSION ['date']; 
 $ time = $ _SESSION ['time']; 
 $ name = $ _REQUEST ['name']; 
  $ ticketnumber = $ _REQUEST ['ticket'];  //这是数组变量
 
foreach($ ticketnumber as $ ticket){
 $ sql =“INSERT INTO table VALUES('$ name','$ day','$ time','$ ticket')  ;“; 
 $ handle = $ conn-&gt; prepare($ sql); 
 $ handle-&gt; execute(); 
 
} 
?&gt; 
    
 
 

在这种情况下,我的数组的值是(它取决于您检查的票数):

  array(2){[0]  =&GT;  string(3)“010”[1] =&gt;  string(3)“011”} 
   
 
 

这也与 $ ticket 的值相匹配,为什么会发生这种情况?

在此之前建立了我的会话和与数据库的连接。 我也尝试用字符串替换数组变量,并且insert语句有效。

感谢任何帮助!

谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujing6053 2017-11-27 20:12
    已采纳

    If you concatenate values into the SQL string as you are doing, there's not much benefit to using a prepared statement. Instead, prepare a statement with placeholders and execute it repeatedly for the various different values.

    Create an array of parameters

    $params['day'] = $_SESSION['date'];
    $params['time'] = $_SESSION['time'];
    $params['name'] = $_REQUEST['name'];
    

    Create the prepared statement with placeholders where your values will be bound

    $sql = "INSERT INTO table VALUES (:name, :day, :time, :ticket);";
    $handle = $conn->prepare($sql);
    

    Execute the statement in a loop for each value in $ticketnumber.

    $ticketnumber = $_REQUEST['tickets'];
    
    foreach ($ticketnumber as $ticket) {
        $params['ticket'] = $ticket; // assign the current ticket to the array of values
        $handle->execute($params);   // the values are bound to the prepared statement
    }
    

    You don't really need to explicitly close or null the connection (especially not inside the loop as others have pointed out).

    打赏 评论

相关推荐 更多相似问题