简体   繁体   中英

multiple row insertion in mysql table through php

I just installed xampp on my system and created connection and table in mysql using php and i even inserted one row in my table but when i tried to insert multiple rows then the flow directly gone to else part to my customize error message.

My small code is as follows:

<?php

include('connection_practice.php');
$insert_query="INSERT INTO student (NAME,ID,ADDRESS,SUBJECT,REMARKS)
               VALUES('AJAY',11,'SILK BOARD','IRO','AVERAGE'),
               VALUES('RITESH',12,'KARTHIK NAGAR','IRO','GOOD'),
               VALUES('SUNNY',13,'HEBBAL','HR MODULE','GOOD'),
               VALUES('HEMAVATHI',14,'HEBBAL','TME','GOOD'),
               VALUES('GURU',15,'HEBBAL','IRO','GOOD'),
               VALUES('SARITHA',16,'HEBBAL','IRO','GOOD'),
               VALUES('NIHAAR',17,'MULTIPLEX','COMPUTERS','AVERAGE')";

if(mysql_query($insert_query,$conn))
{
    echo "8 records added ";
} 
else
{
    echo "record not added ";
}

?>

I think it should be:

$insert_query = "INSERT INTO student (NAME, ID, ADDRESS, SUBJECT, REMARKS)
VALUES ('AJAY', 11, 'SILK BOARD', 'IRO', 'AVERAGE'),
('RITESH', 12, 'KARTHIK NAGAR', 'IRO', 'GOOD'), 
('SUNNY', 13, 'HEBBAL', 'HR MODULE', 'GOOD'), 
('HEMAVATHI', 14, 'HEBBAL', 'TME', 'GOOD'),
('GURU', 15, 'HEBBAL', 'IRO', 'GOOD'), 
('SARITHA', 16, 'HEBBAL', 'IRO', 'GOOD'),
('NIHAAR', 17, 'MULTIPLEX', 'COMPUTERS', 'AVERAGE')";

Have a look at the MySQL Documentation !

Long story short - your code should look as follows:

<?php

include('connection_practice.php');
$insert_query="INSERT INTO student (NAME,ID,ADDRESS,SUBJECT,REMARKS)
VALUES('AJAY',11,'SILK BOARD','IRO','AVERAGE'),
('RITESH',12,'KARTHIK NAGAR','IRO','GOOD'),
('SUNNY',13,'HEBBAL','HR MODULE','GOOD'),
('HEMAVATHI',14,'HEBBAL','TME','GOOD'),
('GURU',15,'HEBBAL','IRO','GOOD'),
('SARITHA',16,'HEBBAL','IRO','GOOD'),
('NIHAAR',17,'MULTIPLEX','COMPUTERS','AVERAGE')";

if(mysql_query($insert_query,$conn))
{
echo "8 records added ";
}

else
{
echo "record not added ";
}

?>

The right sintax is with only one "VALUE" string. something like:

INSERT INTO student (NAME,ID,ADDRESS,SUBJECT,REMARKS) VALUES ('AJAY',11,'SILK BOARD','IRO','AVERAGE'), ('RITESH',12,'KARTHIK NAGAR','IRO','GOOD'), ('SUNNY',13,'HEBBAL','HR MODULE','GOOD'), ('HEMAVATHI',14,'HEBBAL','TME','GOOD'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM