简体   繁体   English

通过php在mysql表中插入多行

[英]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. 我刚刚在系统上安装了xampp,并使用php在mysql中创建了连接和表,甚至在表中插入了一行,但是当我尝试插入多行时,流程直接转到了我的自定义错误消息的其他部分。

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 ! 看看MySQL文档

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. 正确的sintax仅包含一个“ VALUE”字符串。 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'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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