【问题标题】:how to assign associative array to variables php如何将关联数组分配给变量php
【发布时间】:2021-09-11 13:25:07
【问题描述】:

老师好,

一个初学者在这里几乎要秃头,我如何将变量分配给从数据库中获取的关联数组。 我有一个关联数组,它的检索方式如下。我想要的是将变量分配给这个数组,然后迭代地插入另一个表 tbl_product。

Array
(
    [0] => Array
        (
            [pid] => 1
            [pname] => Delta Café
            [pcategory] => Mobiliário de Cozinha
            [purchaseprice] => 120
            [pstock] => 120
            [pdescription] => 120
            [pimage] => 61379859e4d6d.jpeg
        )

    [1] => Array
        (
            [pid] => 2
            [pname] => Baygon
            [pcategory] => Material de Higiene e Limpeza
            [purchaseprice] => 500.58
            [pstock] => 8
            [pdescription] => 
            [pimage] => 613b649f23a5f.jfif
        )

我的代码,

        
if(isset($_POST['btnaddproduct'])){
    $select=$pdo->prepare("select * from tbl_pending");
    $select->execute();
    
  $row=$select->fetchAll(PDO::FETCH_ASSOC);
   
    foreach($row as $product_details=>$key){
    
        $productname=$product_details['pname'];
        $category=$product_details['pname'];
        $stock=$product_details['stock'];
        $description=$product_details['pdescription'];
        $productimage=$product_details['pimage'];
        
   
    $insert=$pdo->prepare("insert into tbl_product(pname,pcategory,purchaseprice,pstock,pdescription,pimage) values(:pname,:pcategory,:purchaseprice,:pstock,:pdescription,:pimage)");     
     $insert->bindParam(':pname',$productname); 
     $insert->bindParam(':pcategory',$category);
     $insert->bindParam(':purchaseprice',$productprice);
     $insert->bindParam(':pstock',$stock);
     $insert->bindParam(':pdescription',$description);
     $insert->bindParam(':pimage',$productimage);     
   }
    

我注意到只插入了 1 个项目。我想要的是遍历数组, 插入到 tbl_product 表中,插入成功后,我想从 tbl_pending 表中删除插入的角色。 我将不胜感激。

【问题讨论】:

  • 嗨 Magnus,感谢您的快速回复。我的 $pdo->prepare 已经在 foreach 循环之前。我已经包含了 insert-execute(); . 除非我对 $pdo-prepare 不了解。我的问题是唯一的第一个元素被插入。如何遍历该数组。
  • 您的插入准备不在循环之外(我指的是循环)。在下面检查我的答案。您还错误地使用了foreach。它实际上应该在这里抛出一堆警告。
  • 这能回答你的问题吗? PHP Insert data from one table to another - 即使下面的答案应该可以解决您的问题,这也是一种更好的方法(直接在 MySQL 中进行一次查询)

标签: php arrays associative


【解决方案1】:

您没有正确使用准备好的语句。

Prepared statements 让您可以准备一个语句,以后可以使用不同的值多次重复使用。

你也用错了foreach()。在您的示例中:

foreach($row as $product_details => $key)

您将$product_details 设置为数组索引,而$key 将包含实际数据。

请参阅下面的示例以了解它应该如何:

// Prepare the statement only once, before the loop so we can reuse it inside
$insert = $pdo->prepare("insert into tbl_product(pname,pcategory,purchaseprice,pstock,pdescription,pimage) values(:pname,:pcategory,:purchaseprice,:pstock,:pdescription,:pimage)");

// Iterate each row as $product_details
foreach($row as $product_details) {

    // Now all we need to do is bind the new values for the placeholders    
    $insert->bindParam(':pname', $product_details['pname']); 
    $insert->bindParam(':pcategory', $product_details['pcategory']);
    $insert->bindParam(':purchaseprice', $product_details['pproductprice']);
    $insert->bindParam(':pstock', $product_details['stock']);
    $insert->bindParam(':pdescription', $product_details['pdescription']);
    $insert->bindParam(':pimage', $product_details['pimage']);     
    
    // And now we need to execute the statement to store those bound values
    $insert->execute();
}

注意:
不过,您需要修复一些变量/数组键名,因为您的代码似乎有一些拼写错误/重复(例如产品名称和类别都设置为 pname 等)。但这显示了正确的流程。

【讨论】:

  • 请看我的最后一条评论。当您可以让 MySQL 直接执行此操作时,没有理由在 PHP 中执行此操作。它更快更好。
猜你喜欢
  • 2023-03-04
  • 2012-06-13
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 2014-03-04
相关资源
最近更新 更多