-6

Hi a have a small problem. I can't insert into db array... this is my code

$qurum1 = implode('|',$_POST['qurum1']);
$qurum = explode('|', $qurum1);
foreach ($qurum as $value) {
$query2 = "INSERT INTO test (data_id, col2) VALUES ('$id', '$value')"; 
}

This code inserts only last value

2 Answers2

0

Try this

<form method="post">
    <input id="value1" value="value1"  name="qurum1[]" type="checkbox">
    <input id="value2" value="value2"  name="qurum1[]" type="checkbox">
    <input value="21"  name="id" type="hidden">
    <input type="submit">
</form>

<?php
    if(isset($_POST['qurum1'])){
        $invite = $_POST['qurum1'];
        $id = $_POST['id'];
        $dbObj=mysqli_connect("$dbhost","$dbusername","$dbpasswd","$dbname") or die('Could not connect to DB : ' .mysqli_connect_error());
        foreach ($qurum as $value) {
            $query2 = "INSERT INTO test (data_id, col2) VALUES ('$id', '$value')"; 
            $ins_sql = mysqli_query($dbObj,$query2);
        }
    }
?>

Now i have added the 'id' as hidden element.

Ashok
  • 128
  • 1
  • 8
-3

You need to add the MySQL query inside the foreach() function, as well:

foreach ($qurum as $value) {
    $query2 = "INSERT INTO test (data_id, col2) VALUES ($id, $value)";
    mysqlExecute($query2);
}

Note that mysqlExecute() does not exist, you need to use your own function to execute the query.

Also note that executing SQL statements in a loop is not recommended because it slows down performance when executing multiple queries. If your web page does not have too much load or you're just practicing it's fine either way.

boroboris
  • 1,548
  • 1
  • 19
  • 32
Ciprian
  • 872
  • 1
  • 10
  • 30