2013-04-10 34 views
2

我有一個表格插入多行。這表格有六行fields.a用戶可以決定使用所有六行或使用幾個。 當所有六行填滿我使用下面的代碼,這工作正常。下面多行Mysql插入語句與PHP如果塊

的代碼工作正常

//Database connection string 
global $conn; 

//insert Records 
$strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values 

('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product3."','".$Af_Date."','".$Af_Dist."','".$Af_Price3."','".$Af_Qty3."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product4."','".$Af_Date."','".$Af_Dist."','".$Af_Price4."','".$Af_Qty4."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product5."','".$Af_Date."','".$Af_Dist."','".$Af_Price5."','".$Af_Qty5."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product6."','".$Af_Date."','".$Af_Dist."','".$Af_Price6."','".$Af_Qty6."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product1."','".$Af_Date."','".$Af_Dist."','".$Af_Price1."','".$Af_Qty1."','".$Af_Trans."')"; 
db_exec($strSQLInsert,$conn); 

但是當我嘗試包括PHP的if語句來離開了空行它給我一個錯誤。 在下面的代碼中,我已經嘗試過測試第一行,但它不工作。如果您有任何想法,請協助。

//Database connection string 
global $conn; 

//insert Records 
$strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values 

"if ($Af_Qty2 > 0){  //error occurs here 
('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'), 
    }" 

('".$Af_Depot."','".$Af_Product3."','".$Af_Date."','".$Af_Dist."','".$Af_Price3."','".$Af_Qty3."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product4."','".$Af_Date."','".$Af_Dist."','".$Af_Price4."','".$Af_Qty4."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product5."','".$Af_Date."','".$Af_Dist."','".$Af_Price5."','".$Af_Qty5."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product6."','".$Af_Date."','".$Af_Dist."','".$Af_Price6."','".$Af_Qty6."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product1."','".$Af_Date."','".$Af_Dist."','".$Af_Price1."','".$Af_Qty1."','".$Af_Trans."')"; 
db_exec($strSQLInsert,$conn); 
+0

哦,聽說過循環,數組和'implode()'? – Vyktor 2013-04-10 07:48:38

+0

我還是一個初學者,還在學習php,如果你在意示範,我會成爲gratefull – Law 2013-04-10 08:16:35

回答

0

你不能在雙引號內寫if條件。 所以請嘗試如下:

$strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values "; 

if ($Af_Qty2 > 0){  //error occurs here 
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'),"; 
    } 

$strSQLInsert .="('".$Af_Depot."','".$Af_Product3."','".$Af_Date."','".$Af_Dist."','".$Af_Price3."','".$Af_Qty3."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product4."','".$Af_Date."','".$Af_Dist."','".$Af_Price4."','".$Af_Qty4."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product5."','".$Af_Date."','".$Af_Dist."','".$Af_Price5."','".$Af_Qty5."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product6."','".$Af_Date."','".$Af_Dist."','".$Af_Price6."','".$Af_Qty6."','".$Af_Trans."'), 
('".$Af_Depot."','".$Af_Product1."','".$Af_Date."','".$Af_Dist."','".$Af_Price1."','".$Af_Qty1."','".$Af_Trans."')"; 
db_exec($strSQLInsert,$conn); 
+0

非常感謝你的完美工作 – Law 2013-04-10 08:13:37

+0

@Law:歡迎花花公子。 – 2013-04-10 08:17:03

1

試試吧。

//Database connection string 
global $conn; 

//insert Records 
$strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values "; 

if ($Af_Qty2 > 0){ 
    $strSQLInsert .= "('".$Af_Depot."','".$Af_Product2."','".$Af_Date."','".$Af_Dist."','".$Af_Price2."','".$Af_Qty2."','".$Af_Trans."'),"; 
} 

$strSQLInsert .= "('".$Af_Depot."','".$Af_Product3."','".$Af_Date."','".$Af_Dist."','".$Af_Price3."','".$Af_Qty3."','".$Af_Trans."'),"; 
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product4."','".$Af_Date."','".$Af_Dist."','".$Af_Price4."','".$Af_Qty4."','".$Af_Trans."'),"; 
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product5."','".$Af_Date."','".$Af_Dist."','".$Af_Price5."','".$Af_Qty5."','".$Af_Trans."'),"; 
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product6."','".$Af_Date."','".$Af_Dist."','".$Af_Price6."','".$Af_Qty6."','".$Af_Trans."'),"; 
$strSQLInsert .= "('".$Af_Depot."','".$Af_Product1."','".$Af_Date."','".$Af_Dist."','".$Af_Price1."','".$Af_Qty1."','".$Af_Trans."');"; 

db_exec($strSQLInsert,$conn); 
0

正如你在評論中提到,你只是學習PHP的,所以我想指出一些事情。

首先,SQL Injection,只是做你的研究,閱讀它是什麼,以及如何預防它。

其次,使用$Af_PriceN呼叫使用arrays

$prices = array(...); 
$products = array(...); 

或者在理想的世界裏,你將創建class包含的所有數據:

class Product { 
    public $id; 
    public $price; 

    public function __construct($id, $price) 
    { 
     $this->id = $id; 
     $this->price = $price; 
    } 
} 

$p = array(
    new Product(1, 17.85), 
    new Product(2, 19.47), 
    ... 
) 

那麼你可以使用foreach來構建複雜的查詢:

foreach($p as $prod){ 
    // append product to query 
} 

但是,回到你的榜樣,假設你用$Af_foo = array代替了每個$Af_fooN,那麼你可以:

$rows = array(); 

for($i = 0; $i < count($Af_Product); $i++){ 
    if($Af_Qty[$i]){ 
     continue; 
    } 

    $r = "('".$Af_Depot."','".$Af_Product[$i]."','".$Af_Date."','".$Af_Dist."', 
      '".$Af_Price[$i]."','".$Af_Qty[$i]."','".$Af_Trans."')"; 

    array_push($rows, $r); 
} 

// And join rows 
if(count($rows)){ 
    $strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values "; 
    $strSQLInsert .= implode(',', $rows); 
} 

退房array_pushimplode。 但是你仍然沒有逃避價值觀念,所以讓我們將db_esc定義爲類似於mysqli_real_escape_string的東西。

$r = "('".db_esc($Af_Depot)."','".db_esc($Af_Product[$i])...; 

這實在是煩人的類型,所以你可以使用優勢的功能就像array_map

// Inside loop: 
$pieces = array($Af_Depot, $Af_Product[$i], $Af_Date, $Af_Dist, 
       $Af_Price[$i], $Af_Qty[$i], $Af_Trans); 

$pieces = array_map('db_esc', $pieces); 

// And here comes a trick: 
$r = "('" . implode("','", $pieces) . ")"; 

所以總結一下整個代碼:

// Assuming 
$Af_Depot = ...; 
$Af_Date = ...; 
$Af_Trans = ...; 

$Af_Product = array(); 
$Af_Price = array(); 
$Af_Qty = array(); 

// Prepare 
$rows = array(); 

// Go trough items: 

for($i = 0; $i < count($Af_Product); $i++){ 
    if($Af_Qty[$i]){ 
     continue; 
    } 

    $pieces = array($Af_Depot, $Af_Product[$i], $Af_Date, $Af_Dist, 
        $Af_Price[$i], $Af_Qty[$i], $Af_Trans); 

    $pieces = array_map('db_esc', $pieces); 
    $r = "('" . implode("','", $pieces) . ")"; 

    array_push($rows, $r); 
} 

// And join rows 
if(count($rows)){ 
    $strSQLInsert = "insert into tbl_sales (Depot_Id,SalesProduct_Id,Sales_Date,Distributor_Id,Selling_Price,Units_Sold,Trns_type) values "; 
    $strSQLInsert .= implode(',', $rows); 
} 
+0

wow.Am立即做出更改以便使用數組並更正以防止sql注入。非常感謝您。 – Law 2013-04-10 09:31:55