php - 使用 php mysql 插入数据和数组值

标签 php mysql arrays database

我遇到了以下错误..

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens    

告诉我如何使用下面的代码将数组数据插入数据库..

  $final= array(
         'item_name' => $sub[0],
         'quantity' => $sub[1],
         'rate' => $sub[2],
         'amount' => $sub[3]
         );
         echo "<br/>";
         echo $sub[0];
        echo "<br/>";
         echo $sub[1];
         echo "<br/>";
         echo $sub[2];
         echo "<br/>";
         echo $sub[3];


         }

以上代码的输出

soap
2
15
30
Surf
1
30
30
RIN
10
20
200 

我想将以上所有数据插入数据库中的这些字段 item_name, quantity, rate, amount,...发现我已经编写了代码,但它显示错误并且无法正常工作

    $input="5001@Cust1|MID1|12|20-05-2015@soap|2|15|30&Surf|1|30|30&RIN|10|20|200@20|240";          
                //new code added
            $string1 = explode("@",$input);

    echo $string1[0]; 
    echo "<br/>";
    echo  $string1[1]; 
    echo "<br/>";
    echo $string1[2];
    echo "<br/>";
    echo $string1[3];

    $stringitem=$string1[1];
    $stringitem1=$string1[1];
    $stringitem2=$string1[2];
    $stringitem3=$string1[3];

    $stringtotal=explode("|",$stringitem3);
    echo "<p>Bill Amount</p>";
    echo $stringtotal[0]; 
    echo "<br/>";
    echo  $stringtotal[1]; 

    $stringnew=explode("|",$stringitem1);

    echo "<p>Machine Data</p>";

    echo $stringnew[0]; 
    echo "<br/>";
    echo  $stringnew[1]; 
    echo "<br/>";
    echo $stringnew[2];
    echo "<br/>";
    echo $stringnew[3];

    echo "<p>Product Data</p>";


    $stringnew1=explode("&",$stringitem2);
     $p_count = count($stringnew1);

     for($i=0;$i<$p_count;$i++){
     $sub = explode('|',$stringnew1[$i]);

     $final= array(
     'item_name' => $sub[0],
     'quantity' => $sub[1],
     'rate' => $sub[2],
     'amount' => $sub[3]
     );
     echo "<br/>";
     echo $sub[0];
    echo "<br/>";
     echo $sub[1];
     echo "<br/>";
     echo $sub[2];
     echo "<br/>";
     echo $sub[3];

     }


                define("DB_SERVER", "localhost");
    define("DB_USER", "root");
    define("DB_PASS", "");
    define("DB_NAME", "pmradmin");

    $customer_id=$stringnew[0];
    $machine_id=$stringnew[1];
    $bill_no = $stringnew[2];
    $bill_date=$stringnew[3];
    $item_name = $sub[0];
    $quantity = $sub[1];
    $rate = $sub[2];
    $amount = $sub[3];
    $discount = $stringtotal[0];
    $bill_amount = $stringtotal[1];

    try {
        $dbh = new PDO('mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME . '', DB_USER, DB_PASS);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
    } catch (PDOException $e) {
        echo $e->getMessage();
        die();
    }





        try {
              $query = $dbh->prepare("INSERT INTO billing (customer_id, machine_id, bill_no, bill_date, bill_amount, item_name, quantity, rate, amount, discount) VALUES (:customer_id, :machine_id, :bill_no, :bill_date, :item_name, :quantity, :rate, :amount, :discount, :bill_amount)");
     $query->setFetchMode(PDO::FETCH_ASSOC);
      foreach($final as $value)
{
    $query->execute(array(
             "customer_id" => $customer_id,
              "machine_id" => $machine_id,
             "bill_no" => $bill_no, 
             "bill_date" => $bill_date,
             "bill_amount" => $bill_amount,
             "item_name" => $item_name,
             "quantity" => $quantity,
             "rate" => $rate,
             "amount" => $amount,
             "discount" => $discount
            ));

       }
 } catch (PDOException $ex) {
     echo $ex->getMessage();
 }



               // echo "Sending output to client \n";

                // //send response to client
                // socket_write($client_socks[$i] , $output);
            // };
       // }
    //}     
    ?>

最佳答案

您的 SQL 查询需要以下 10 参数:

:customer_id, :machine_id, :bill_no, :bill_date, :item_name, :quantity, :rate, :amount, :discount, :bill_amount

但是你只在execute中提供了7

关于php - 使用 php mysql 插入数据和数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30370370/

相关文章:

php - 使用 GET 获取正确的 URL

mysql - 将带有左连接的 mysql 查询转换为 Eloquent

javascript - sql 和 php 数据库条目 'lagging behind'

使用日志函数(没有 math.h)和数组计算 C 中的字母表

php - (PHP) 向 json 文件添加不正确的文本

php - 这个查询有什么问题?我正在尝试根据第二个表中列的值更新列值

带有后查询的 PHP/CSS 按钮样式

php - 多个 xml feed、sql 匹配

mysql - 帮助查询各自酒店下特定日期之间的免费客房分组

PHP 代码 - 这部分代码是什么意思?