0

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

2
  • See implode(). If you don't want the array joined by commas, you may want to remove that part. Commented Jul 27, 2019 at 9:16
  • There was no coding attempt to insert, so a generalised duplicate is suitable for closure. You should not be using the GET method to send data bound for the database. Commented Jul 27, 2019 at 9:55

1 Answer 1

0

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

You can 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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

You might consider using Prepared Statements. Also see why MySQLi prepared statements?.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.