【问题标题】:Inserting data and array values using php mysql使用 php mysql 插入数据和数组值
【发布时间】:2015-08-02 22:08:15
【问题描述】:

我遇到了错误..

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

【问题讨论】:

    标签: php mysql arrays database


    【解决方案1】:

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

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

    但您只在execute 中提供7

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-26
      • 2012-07-21
      • 1970-01-01
      • 2017-02-10
      • 2016-01-14
      • 2012-04-20
      相关资源
      最近更新 更多