Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php mysql插入数据和数组值_Php_Mysql_Arrays_Database - Fatal编程技术网

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

使用php mysql插入数据和数组值,php,mysql,arrays,database,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

我正在犯错误

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


         }
我想将上述所有数据插入数据库中的这些字段
item\u 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);
            // };
       // }
    //}     
    ?>
$input=”5001@Cust1|中期1 | 12 | 20-05-2015@soap|2 | 15 | 30和冲浪| 1 | 30 | 30和冲浪| 10 | 20|200@20|240";          
//新增代码
$string1=分解(“@”,$input);
echo$string1[0];
回声“
”; echo$string1[1]; 回声“
”; echo$string1[2]; 回声“
”; echo$string1[3]; $stringitem=$string1[1]; $stringitem1=$string1[1]; $stringitem2=$string1[2]; $stringitem3=$string1[3]; $stringtotal=分解(“|”,$stringitem3); 回显“票据金额”

”; echo$stringtotal[0]; 回声“
”; echo$stringtotal[1]; $stringnew=分解(“|”,$stringitem1); 回显“机器数据”

”; echo$stringnew[0]; 回声“
”; echo$stringnew[1]; 回声“
”; echo$stringnew[2]; 回声“
”; echo$stringnew[3]; echo“产品数据”

”; $stringnew1=分解(&,$stringitem2); $p_count=count($stringnew1); 对于($i=0;$i$sub[0], “数量”=>$sub[1], “费率”=>$sub[2], “金额”=>$sub[3] ); 回声“
”; echo$sub[0]; 回声“
”; echo$sub[1]; 回声“
”; echo$sub[2]; 回声“
”; echo$sub[3]; } 定义(“DB_服务器”、“本地主机”); 定义(“DB_用户”、“根用户”); 定义(“DB_PASS”和“); 定义(“DB_名称”、“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]; $折扣=$stringtotal[0]; $bill_amount=$stringtotal[1]; 试一试{ $dbh=new-PDO('mysql:host='.DB_-SERVER';dbname='.DB_-NAME'',DB_-USER,DB_-PASS); $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常); $dbh->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); }捕获(PDO$e){ echo$e->getMessage(); 模具(); } 试一试{ $query=$dbh->prepare(“插入账单(客户id、机器id、账单号、账单日期、账单金额、项目名称、数量、费率、金额、折扣)值(:客户id、:机器id、:账单号、:账单日期、:项目名称、:数量、:费率、:金额、:折扣、:账单金额)”); $query->setFetchMode(PDO::FETCH_ASSOC); foreach($final作为$value) { $query->execute(数组)( “客户id”=>$customer\u id, “机器id”=>$machine\u id, “账单号”=>$账单号, “账单日期”=>美元账单日期, “账单金额”=>美元账单金额, “项目名称”=>$item\u name, “数量”=>美元数量, “费率”=>美元费率, “金额”=>美元金额, “折扣”=>美元折扣 )); } }捕获(PDO异常$ex){ echo$ex->getMessage(); } //回显“将输出发送到客户端\n”; ////向客户端发送响应 //socket_write($client_socks[$i],$output); // }; // } //} ?>
您的SQL查询需要以下10参数:

:客户id,:机器id,:票据编号,:票据日期,:项目名称,:数量,:费率,:金额,:折扣,:票据金额

但您仅在执行中提供
7

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