在 PHP 中循环插入查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10226000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:41:51  来源:igfitidea点击:

Loop over an insert Query in PHP

phpmysqlhtmlsql

提问by Apocaliptica61

I am having trouble with insert the same value N time In SQL via PHP script. I have this vslue?

我在通过 PHP 脚本在 SQL 中插入相同的值 N 次时遇到问题。我有这个vslue?

Table Info  column (ID,Name, LastName,)
Valus ('',Alain,Alian);

Whata i want to do is insert this value in the same table 10 Times Using a While loop for E.G Or something like that any idea?.

我想要做的是在同一个表中插入这个值 10 次使用 EG 的 While 循环或类似的任何想法?

<?php
    $i=1;
    While ($i<= 5)      
    {
       $sql="INSERT INTO arivage
          (ID_Ship,Date_ariv,Date_achat,prov_id,Sph,cyl,Prod_type,Pord_color)
          VALUES
          ('','$date','$date1','$prov_id','$sph','$cyl','$Prod_type','$Pord_color')";
       $i++;
    }
?>

回答by ab_dev86

Have you some errors?

你有什么错误吗?

what you need is feasible

你需要的是可行的

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
  die('Could not connect: ' . mysql_error());
}

for($i = 0; $i<10; $i++) {
    $sql="INSERT INTO arivage (ID_Ship,Date_ariv,Date_achat,prov_id,Sph,cyl,Prod_type,Pord_color) VALUES ('','$date','$date1','$prov_id','$sph','$cyl','$Prod_type','$Pord_color')";
    $result = mysql_query($sql);
    if (!$result) {
     die('Invalid query: ' . mysql_error());
    }
}

回答by Muhammad Alvin

I assume you have a connection to the database (by using mysql_connect() + mysql_select_db()), and also execute your query after composing it (by mysql_query($sql)).

我假设你有一个到数据库的连接(通过使用 mysql_connect() + mysql_select_db()),并且在编写它之后执行你的查询(通过 mysql_query($sql))。

I need to know what is the datatype of column ID_Ship? Is this PRIMARY KEYand auto_increment? If yes, then you need to change your query from:

我需要知道ID_Ship列的数据类型是什么?这是PRIMARY KEYauto_increment吗?如果是,那么您需要将查询从:

INSERT INTO arivage (ID_Ship,Date_ariv,Date_achat,prov_id,Sph,cyl,Prod_type,Pord_color) 
VALUES ('','$date','$date1','$prov_id','$sph','$cyl','$Prod_type','$Pord_color')

to:

到:

INSERT INTO arivage (Date_ariv,Date_achat,prov_id,Sph,cyl,Prod_type,Pord_color) 
VALUES ('$date','$date1','$prov_id','$sph','$cyl','$Prod_type','$Pord_color');

Note: ID_ship is removed, and '' is also removed.

注意:ID_ship 被删除,'' 也被删除。

This removal is to prevent inserting the same primary key for 2nd, 3rd, etc records. Remember that primary key must be unique. Not specifying it will let MySQL inserts the generated value (because it is auto_increment).

此删除是为了防止为第二、第三等记录插入相同的主键。请记住,主键必须是唯一的。不指定它将让 MySQL 插入生成的值(因为它是 auto_increment)。

回答by NaNO3

You should Add

你应该添加

mysql_query($sql);

to your code so that the query is executed between the cycles.

到您的代码,以便在循环之间执行查询。

$i=1;
While ($i<= 5)      
{
 $sql="INSERT INTO arivage
 (ID_Ship,Date_ariv,Date_achat,prov_id,Sph,cyl,Prod_type,Pord_color)
 VALUES('','$date','$date1','$prov_id','$sph','$cyl','$Prod_type','$Pord_color')";

 mysql_query($sql);

    $i++;
}

回答by Bibin Velayudhan

Try This.. This might work

试试这个..这可能有用

$sql= "INSERT INTO TABLE_NAME(field1,field2) VALUES( ";
    for($i=0;$i<10;$i++)
    {
       $sql.="('value1', 'value2'), "
    }
    $sql.=" )";