for循环使用php中的PDO转换为foreach循环

时间:2016-10-04 05:41:00

标签: php arrays for-loop pdo foreach

转换为" foreach循环"来自" for loop"在将数据插入MySQl

之后使用数组和计数数组positon与$ serviceTitle的位置
for($i=0;$i<count($serviceTitle);$i++){        
    $statement = $db->prepare("INSERT INTO invoice (orderNo,customerNo,productName,
                 price,quantity,amount,createDate,createTime,
                 createBy) VALUES (?,?,?,?,?,?,?,?,?)");        
    $statement->execute(array($orderNo,$customerNo,$serviceTitle[$i],$price[$i],$quantity[$i],$amount[$i],$date,$time,$createBy));
 }

在回答问题之前,请检查此问题:Insert Array values insert to single ID into mysql database using php and PDO

先谢谢

2 个答案:

答案 0 :(得分:2)

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ) {

        date_default_timezone_set('Asia/Dacca');

        /* check that all variables exist in the POST array */
        if( isset( $_POST['Submit'], $_POST['serviceTitle'], $_POST['price'], $_POST['quantity'], $_POST['amount'], $_POST['orderNo'], $_POST['customerNo'] ) ){

            /*
                Assuming that the fields serviceTitle,price,quantity and amount
                are named like field[] in the form then their values will be 
                arrays in the POST array
            */
            $serviceTitle = $_POST['serviceTitle'];
            $price = $_POST['price'];
            $quantity = $_POST['quantity'];
            $amount = $_POST['amount'];
            $orderNo = $_POST['orderNo'];
            $customerNo = $_POST['customerNo'];


            $date = date('Y-m-d');
            $time = date('h:i:s');
            $createBy = $_SESSION['sess_username'];




            /* check that key variables are arrays as you intend to access as arrays below */
            if( is_array( $serviceTitle ) && is_array( $price ) && is_array( $quantity ) && is_array( $amount ) ){

                /* prepare the sql statement */
                $sql='insert into `invoice` ( `orderno`, `customerno`, `productname`, `price`, `quantity`, `amount`, `createdate`, `createtime`, `createby` ) values (?,?,?,?,?,?,?,?,?)';
                $stmt=$db->prepare( $sql );

                if( $stmt ){
                    foreach( $serviceTitle as $i => $arr ){
                        try{
                            $args=array(
                                $orderNo,
                                $customerNo,
                                $arr[$i],
                                $price[$i],
                                $quantity[$i],
                                $amount[$i],
                                $date,
                                $time,
                                $createBy
                            );
                            $stmt->execute( $args );
                        }catch( PDOException $e ){
                            echo $e->getMessage();
                        }
                    }

                    $stmt->close();
                    $db->close();

                    $_SESSION['orderNo'] = $_POST['orderNo'];
                    $_SESSION['customerNo'] = $_POST['customerNo'];

                    ob_clean();
                    header( "location: order_confirm_tech_step2.php" );

                } else {
                    echo "error preparing sql"
                }
            } else {
                echo "error, one or more variables are not arrays"
            }
        } else {
            echo "error, one or more variables are not available"
        }
    }
?>

答案 1 :(得分:0)

这是在$ serviceTitle上使用foreach循环的代码

    $statement = $db->prepare("INSERT INTO invoice (orderNo,customerNo,productName,price,quantity,amount,createDate,createTime,createBy) VALUES (?,?,?,?,?,?,?,?,?)");
    foreach ($serviceTitle as $key => $value) {
        $statement->execute(array($orderNo,$customerNo,$value,$price[$key],$quantity[$key],$amount[$key],$date,$time,$createBy));
    }

然而,最好使用批量插入而不是单独插入每一行。