简体   繁体   English

PHP从数组插入SQL

[英]Php insert into sql from array

I have 2 array and I want to add them into my sql table with one button. 我有2个数组,我想用一个按钮将它们添加到我的sql表中。 I dont know using arrays with sql. 我不知道在SQL中使用数组。 I read a lot of page but couldnt find an easy way. 我读了很多页面,但找不到简单的方法。

$name=array("Jack", "John", "Fiona");

$country=array("London", "Greece", "Japan"); 

$entry =' went to school';

Sql table : SQL表:

**id** **|** **name**  **|**    **entry**            **|**      **country**

**1** Jack **|** Jack went to school   **|** London

**2** John  **|**  John went to school    **|** Greece

**3** Fiona  **|**  Fiona went to school  **|** Japan

I tried something like this but it didn't work. 我尝试了类似的方法,但是没有用。 Regards 问候

$sql = mysql_query("INSERT INTO bilgi (name,entry,country) VALUES ('$name[]','$name[]&$entry','$country[]'");
$name=array("Jack", "John", "Fiona");

$country=array("London", "Greece", "Japan"); 

$entry =' went to school';

$SizeOfName=sizeof($name);

for($i=0;$i<$SizeOfName;$i++)
{
    $Name=$name[$i];
    $Country=$country[$i];
    $Entry=$Name$entry;

    $sql = mysql_query("INSERT INTO bilgi (name,entry,country) VALUES ('$Name','$Entry','$Country')";
}

You should really upgrade to either PDO or mysqli . 您应该真正升级到PDOmysqli The mysql API has been deprecated for a while now and will be removed in the next release of PHP (7). mysql API已经过时了一段时间,并将在下一版PHP(7)中删除。

To answer your actual question imo the first step should be to make the data sane, ie: 要回答imo的实际问题,第一步应该是使数据合理,即:

$data = [
    [
        'name'    => 'Jack',
        'country' => 'London',
    ],
    [
        'name'    => 'John',
        'country' => 'Greece',
    ],
    [
        'name'    => 'Fiona',
        'country' => 'Japan',
    ],
];

This makes it much easier to handle and maintain the data. 这样可以更轻松地处理和维护数据。

Next you simply need to loop through the single array: 接下来,您只需要遍历单个数组:

$entry = ' went to school';

foreach ($data as $item) {
    $sql = mysql_query("INSERT INTO bilgi (name,entry,country) VALUES ('$item[name]','$item[name]$entry','$item[country]'");
}

However this still uses the old API and doesn't prevent sql vulnerabilities. 但是,这仍然使用旧的API,并且不会阻止sql漏洞。 What you should do (after making a database connection proper ) is: 您应该做的(在建立适当的数据库连接之后)是:

$entry = 'went to school';

$stmt = $pdo->prepare('INSERT INTO bilgi (name,entry,country) VALUES (:name, :entry, :country');

foreach ($data as $item) {
    $stmt->execute([
        'name' => $item['name'],
        'entry' => $entry,
        'country' => $item['country'],
    ]);
}

I have also removed name from the entry field, because that is a bad way of duplication in your database. 我还从输入字段中删除了name ,因为这是在数据库中复制的一种不好的方法。 If ever the name changes you are stuck with the name in the entry column. 如果名称更改,您将在entry栏中保留名称。

This uses a non deprecated API. 这使用了不建议使用的API。 Prevents SQL injection vulnerabilities and is just more sane and maintainable. 防止SQL注入漏洞,并且更加理智和可维护。

You can use this 你可以用这个

for ($i = 0; $i < count($name); $i++) {
   $entryVal = $name[$i].$entry;
   $sql = mysql_query("INSERT INTO bilgi (name,entry,country) VALUES  ('".$name[$i]."','".$entryVal."','".$country[$i]."'");
}
<?php

$name=array("Jack", "John", "Fiona");

$country=array("London", "Greece", "Japan"); 

$entry =' went to school';

for($i=0 ; $i<count($name);$i++){

    $sql = mysql_query("INSERT INTO bilgi (name,entry,country) VALUES ('".$name[$i]."','".$name[$i].$entry."','".$country[$i]."'");
}

You can insert in one SQL as: 您可以将一个SQL插入为:

$name = array("Jack", "John", "Fiona");
$country = array("London", "Greece", "Japan"); 
$entry =' went to school';

$values;
for ($i = 0; $i < count($name); $i++) {
    $values[] = "('$name[$i]', '$name[$i]$entry', '$country[$i]')";
}

$sql = 'INSERT INTO bilgi (name, entry, country) VALUES ';
for ($i = 0; $i < count($values); $i++) {
    $sql .= $values[$i];
    if($i < (count($values) - 1)) $sql .= ", ";
}

$result = mysql_query($sql);

Please take care on your arrays before insert into database. 在插入数据库之前,请注意阵列。 $name and $country must be equal in number of elements. $ name和$ country的元素数必须相等。

This is dynamic solution for your question : 这是针对您的问题的动态解决方案:

<?php
$name=array("Jack", "John", "Fiona","Mack","Raja","Bohemia");
$country=array("London", "Greece", "Japan"); 
$entry ='went to school';

$bigcount = max(count($name),count($country));

for($i=0;$i<$bigcount;$i++)
{
    $finalValues .= "('$name[$i]','$country[$i]','$entry'),";
}
$final = substr($finalValues,0,-1);
$query = "INSERT INTO bilgi (name,entry,country) VALUES $final";
$sql = mysql_query($query);
?>

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

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