3

I'm trying to insert multiple rows at once in a MySQL database using PHP. I want to insert a, b, c in 3 different rows but it is inserted in 1 single row in this format a,b,c.

My code is here below, it doesn't give me any error because the code works perfectly, but I can't get my desire result.

<?php
require_once('conn.php');
$name = $_POST['name'];
 if($name)
 {
    foreach($name as $std_name)
    {
    $student[] = $std_name;
    // implode($c); i.e a,b,c etc
    }
 }
$sql = "INSERT INTO `tbl_student` 
 (`student_name`) 
 VALUES 
 ('".implode($student, ',')."');";
 $res = mysql_query($sql);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript"> 

    var nFloor = "";    

    function removeField(nField){

        nField.parentNode.parentNode.removeChild(nField.parentNode);
    }

    function insertField(){

        var newFieldContainer = document.createElement('div');
        var newFieldLabel = document.createElement('label');
        newFieldLabel.innerHTML = "Student Name:&nbsp;&nbsp;&nbsp;";        
        var newField = document.createElement('input');
        newField.type = "text";
        newField.name = "vipInfo[]";
        newFieldContainer.appendChild(newFieldLabel);
        newFieldLabel.appendChild(newField);
        var deleteBtn = document.createElement('input');
        deleteBtn.type = "button";
        deleteBtn.value = "Remove";
        deleteBtn.style.marginLeft = "5px";
        deleteBtn.onclick = function(){removeField(this)};
        newFieldContainer.appendChild(deleteBtn);
        document.forms[0].insertBefore(newFieldContainer,nFloor);
    }

    function init(){

        var insertBtn = document.getElementById('newFieldBtn')
        insertBtn.onclick = function()
            {
             insertField();
            }
        nFloor = insertBtn;     
    }

    navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);    

</script>
</head>
    <body>
        <form action="" method="post">

            <div class="field"><label>Student Name:&nbsp;&nbsp;&nbsp;<input type="text" name="name[]"></label></div>

            <input type="button" id="newFieldBtn" value="New Field"> 
            <input type="submit" name="submit" value="Submit">

        </form>
    </body>
</html>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • echo $sql, what does it return? –  Jan 30 '12 at 05:59
  • Check this -> http://stackoverflow.com/questions/8667384/inserting-multiple-rows-in-a-table-using-php – Rikesh Jan 30 '12 at 06:02
  • @Dagon echo $sql give me this result INSERT INTO `tbl_student` (`student_name`) VALUES ('a,b,c'); but i want it to insert in 3 different rows i.e 3 different record –  Jan 30 '12 at 06:06

2 Answers2

0

put this query

$sql = "INSERT INTO `tbl_student` 
 (`student_name`) 
 VALUES 
 ('".implode($student, ',')."');";
 $res = mysql_query($sql);

inside foreach block don't use use implode or rather use below query as

$sql = "INSERT INTO `tbl_student` 
 (`student_name`) 
 VALUES 
 ('". $std_name."');";

so it will add each one in different row

maxjackie
  • 22,386
  • 6
  • 29
  • 37
  • it insert only last value into table as INSERT INTO `tbl_student` (`student_name`) VALUES ('b'); –  Jan 30 '12 at 06:21
0

Use this:

$val="('".implode("'), ('",$student)."')";
        $sql = "INSERT INTO `tbl_student`
 (`student_name`) VALUES ".$val.";";
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226