从Array PHP插入到表中


Insert into table from Array PHP

我有四个由一些值组成的数组。我想在同一行中插入具有相同索引的数组。例如:

$product = [Facebook, Twitter]; 
$sub_product = [Likes, Boost]; 
$plan = [10k, 20k];
$months = [3,6]; 

我希望桌子像一样

+----+----------+-------------+------+--------+
| id | product  | sub_product | plan | months |
+----+----------+-------------+------+--------+
| 1  | Facebook | Likes       | 10k  | 3      |
+----+----------+-------------+------+--------+
| 2  | Twitter  | Boost       | 20k  | 6      |
+----+----------+-------------+------+--------+

实现这一点的最佳方法是什么,使每个数组的索引都位于同一行?我想在HTML表中插入

如果你想单次执行mysql_query,那么

$product = [Facebook, Twitter]; 
$sub_product = [Likes, Boost]; 
$plan = [10k, 20k];
$months = [3,6]; 
$query = 'insert into TABLE (product,sub_product,plan,months) values';
foreach( $product as $index => $col ){
    $query .= "( '".$product[$index]."', '".$sub_product[$index]."', '".$plan[$index]."', ".$months[$index]." ),";
}
$query = rtrim( $query, ',');
mysqli_query(mysql_query);

这将比在循环中执行多个mysql_query函数更快。

试试这个代码,

    foreach($product as $key=>$p){
    $data = array(
    'product'=>$p,
    'sub_product'=>$sub_product[$key],
    'plan'=>$plan[$key],
    'months'=>$months[$key]
    );
    //Code to insert this array to Database
    // Create connection
         $conn = mysqli_connect($servername, $username, $password, $dbname);
         $sql = "INSERT INTO `table`('product','sub_product',...) VALUES ($data['product'],$data['sub_product'],...)";
         mysqli_close($conn);
    //OR If you are using Codeigniter,
         $this->db->insert('table',$data);
    }

这个代码应该可以工作,

$product = array("Facebook", "Twitter"); 
$sub_product = array("Likes", "Boost"); 
$plan = array("10k", "20k");
$months = array(3,6); 
for($i=0;$i<count($product);$i++){
    $product_name=$product[$i];
    $sub_product_name=$sub_product[$i];
    $plan_name=$plan[$i];
    $month_no=$months[$i];
    echo "insert into table_name (product_name, sub_product_name, plan_name, month_no) values ('$product_name', '$sub_product_name', '$plan_name', '$month_no')";
    echo "<br>";
}

谢谢Amit

假设所有数组都有相同的长度,并且$mysqli是到数据库的连接,

for ($i=0; $i < count($product); $i++) {
    $query = "INSERT INTO your_table (id, product, sub_product, plan, months)";
    $query .= " VALUES ('$i', '{$product[$i]}', '{$sub_product[$i]}', '{$plan[$i]}', '{$months[$i]}')";
    $mysqli->query($query);
}

假设上述所有数组在给定时间点的大小相同,并且它们的索引一致,则可以使用以下代码:

    <?php
    $product = array('Facebook', 'Twitter'); 
    $sub_product = array('Likes', 'Boost'); 
    $plan = array('10k', '20k');
    $months = array(3,6); 
/* Connected to a mysql Database */
$i = 0; //initialize a counter
while($i < sizeof($product)) //Run the loop (Twice in this case)
{
//Store the current iteration value in variables
$current_product = $product[$i];
$current_sub_product = $sub_product[$i];
$current_plan = $plan[$i];
$current_months = $months[$i];

    //Prepare the SQL statement
$sql = <<<EOD
        INSERT INTO table_product(product,subproduct,plan,months) 
        VALUES ('$current_product','$current_sub_product','$current_plan',$current_months)  
EOD;
$i++;
echo $sql . "<br />";
}

?>

用这种方法简单地做:

$count = count($product);
$index=0;
while($index<$count){
//do your stuff here i.e. insert query
$sql = "INSERT INTO your_table SET product='".$product[$index]."', sub_product='".$sub_product[$index]."', plan='".$plan[$index]."', months='".$months[$index]."' ";
mysql_query($sql);
$index++;
}