从数组PHP插入到表中

从数组PHP插入到表中,php,mysql,arrays,Php,Mysql,Arrays,我有四个数组,由一些值组成。我想在同一行中插入具有相同索引的数组。 例如: 我希望桌子是这样的 +----+----------+-------------+------+--------+ | id | product | sub_product | plan | months | +----+----------+-------------+------+--------+ | 1 | Facebook | Likes | 10k | 3 | +----+----

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

我希望桌子是这样的

+----+----------+-------------+------+--------+
| id | product  | sub_product | plan | months |
+----+----------+-------------+------+--------+
| 1  | Facebook | Likes       | 10k  | 3      |
+----+----------+-------------+------+--------+
| 2  | Twitter  | Boost       | 20k  | 6      |
+----+----------+-------------+------+--------+
实现这一点的最佳方法是什么,使每个数组的索引位于同一行?我想在HTML表格中插入

试试下面的代码

    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>";

}
$product=array(“Facebook”、“Twitter”);
$sub_product=数组(“Likes”、“Boost”);
$plan=阵列(“10k”、“20k”);
$months=数组(3,6);
对于($i=0;$i以这种方式进行简单操作:

$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++;
}
$count=计数($product);
$index=0;

而($index假设所有数组都具有相同的长度,
$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);
}
for($i=0;$iquery($query);
}

如果您想单次执行mysql\u查询,那么

$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_查询函数更快。

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

    <?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 />";
}


?>


您在所有四个数组中都有相同的索引吗?您可以添加一个forech循环,然后逐个插入记录。您想在数据库中插入记录,还是想在html中显示为表?如果不需要,您需要为该foreach中存在的所有元素添加验证。@Nehil Mistry提到所有数组的索引都是相同的。谢谢如果元素丢失,那么您的代码会做什么,错误未定义索引?@Vegeta,这是一个很好的方法,首先形成一个查询,然后执行它。很好。流程是优化的。只需澄清一个疑问,而不是ltrim,它应该是rtrim,因为在查询的右侧添加了逗号。为什么要将$query字符串拆分为two语句?@saurabhkamble谢谢。你能添加代码将$data数组插入数据库@priyank57吗
    <?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 />";
}


?>