将php数组元素插入mysql数据库[重复]

This question already has an answer here:

i Want to insert each array element into new row in mysql db

<?php
   if(isset($_SESSION['user'])){
      echo "<body>
            <form method='GET'>
            <input type='text' id='search-box' name='item'>
            <button type='submit' name='add'>add</button>
            <button type='submit' name='place'>place</button>
            </form>
            </body>";
    if(isset($_GET['add'])){
       $item = $_GET["item"];
       array_unshift($_SESSION['menu1'], $item);
    }
   if(isset($_GET['place'])){
       echo implode(",", $_SESSION['menu1']);
    }
   }
   else{
      echo "you are not logged in";
   }
 ?>

The above code outputs an array in which each element is separated by comma, but i want to insert each element into a new row

</div>

If you are performing the database operation from core php, you need the build the query like this,

$query = "INSERT INTO _YOUR_TABLE_NAME_ values ";
foreach ($item_array as $item){
    $query .= "(DEFAULT , '".$item->name."' , '".$item->description."'),";
}
$query = rtrim( $query , ',');
$result = mysqli_query( $query , $connection);

If you have a large set of values in the array to be inserted in the database, I suggest you to segment the query.

If you want to use Prepared Statement,

$stmt = $mysqli->prepare("INSERT INTO something ( id , title , description) VALUES ( DEFAULT , ?, ?)");

foreach( $item_array as $item ){
    $stmt->bind_param( $item->title , $item->description);
    $stmt->execute();
}

This is also valid.