通过php在mysql表中插入多行

时间:2014-06-02 14:35:31

标签: php mysql

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

我的小代码如下:

<?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 ";
}

?>

3 个答案:

答案 0 :(得分:0)

我认为应该是:

$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')";

答案 1 :(得分:0)

查看MySQL Documentation

长话短说 - 您的代码应如下所示:

<?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 ";
}

?>

答案 2 :(得分:0)

正确的语法只有一个&#34; VALUE&#34;串。类似的东西:

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'),