Search code examples
phpmysqlphpmyadminmultiple-insert

Insert multiple data at once


Though there are some same questions here, still I want to post my own question as I think I made a solution, but it's not working. Here is the following code:

$i=1;
while ( $i<= 10) 
{
    $i++;
    $sql    =   "INSERT INTO job (Category, Title, Type) VALUES ('$_POST[cat]','$_POST[title]','$_POST[type]')";
}

Shouldn't it insert 10 data one by one? But it just inserts 1 data. Why is this happening?


Solution

  • See that you're constructing exactly the same sql string every iteration.
    Your code should be:

     $i=1;
     while ( $i<= 10)
     {
        $i++;
        $sql = "INSERT INTO job (Category, Title, Type)
                     VALUES ('{$_POST[cat]}','{$_POST[title]}','{$_POST[type]}')";
        mysql_query($sql);
     }



    Pay attention to the []