简体   繁体   中英

Php insert into sql from array

I have 2 array and I want to add them into my sql table with one button. I dont know using arrays with 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 :

**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 . The mysql API has been deprecated for a while now and will be removed in the next release of PHP (7).

To answer your actual question imo the first step should be to make the data sane, ie:

$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. 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. If ever the name changes you are stuck with the name in the entry column.

This uses a non deprecated API. Prevents SQL injection vulnerabilities and is just more sane and maintainable.

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:

$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.

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);
?>

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