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

时间:2015-05-21 09:55:27

标签: 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);
            // };
       // }
    //}     
    ?>

1 个答案:

答案 0 :(得分:0)

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

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

但您只在7中提供 execute