Reputation: 53
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[]'");
Upvotes: 0
Views: 1567
Reputation: 1120
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);
?>
Upvotes: 0
Reputation: 72729
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, i.e.:
$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.
Upvotes: 1
Reputation: 2522
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]."'");
}
Upvotes: 0
Reputation: 121
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.
Upvotes: 0
Reputation: 10548
$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')";
}
Upvotes: 1
Reputation: 81
<?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]."'");
}
Upvotes: 0