2015-08-14 39 views
0

我有一個PHP腳本的問題。從數據數組(PHP,MySQL)寫循環sql查詢

我有一個數組,這是從形式,其中$_POST['store']是從jQuery的形式與功能的陣列,以添加多行生成:

Array 
(
    [client] => 
    [darvad] => 
    [owca] => 
    [ldrive] => 
    [store] => Array 
     (
      [product] => Array 
       (
        [0] => 430 
        [1] => 440 
        [2] => 430 
       ) 

      [quantity] => Array 
       (
        [0] => 123 
        [1] => 1223 
        [2] => 232 
       ) 

      [segums] => Array 
       (
        [0] => Mixed park 
        [1] => Light vehicle 
        [2] => Trucks 
       ) 

      [deadline] => Array 
       (
        [0] => 2015-08-04 
        [1] => 
        [2] => 
       ) 

      [renewal] => Array 
       (
        [0] => 1 
       ) 

     ) 

) 

,我需要獲取從這個數組的值到SQL插入statment並循環它。

$sql_rec = "INSERT INTO tsales_funnel_mrecord (product, quantity, segums, deadline) VALUES (...),(...),(...).... ";

HTML代碼:

  <div id="container"> 
      <div id="content" role="main"> 

      <?php 
       echo "<pre>"; 
       print_r($_POST); 
       echo "</pre>"; 
      ?> 

<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post" id=multiForm> 
    <label for="client">Klients: *</label><input id="client" type="text" name="client" placeholder="Reg.nr | Pk.kods" value="" /></br> 
    <label for="selector1">Darījuma vadītājs: *</label> 
    <select id="selector1" name="darvad" > 
     <option value="">-Dar. vadītājs-</option> 
<?php 
    $sql = "SELECT Vards_Uzvards, Tables_ID FROM users"; 
    $results = $wpdb->get_results($sql); // return an object, not ARRAY_N 
if ($results) { 
    foreach ($results as $row) { 
     echo "<option value = '".$row->Tables_ID."'>".$row->Vards_Uzvards."</option>"; 
}} 
    echo "</select></br>";       
?> 
<label for="owcafind"><a href="<?php echo site_url('/sample-page/owca/'); ?>" target="_blank">Meklēt OWCA kodu:</a> *</label><input id="owcafind" type="text" name="owca" placeholder="OWCA Kods (8)" value="" /></br> 

<label for="ldrive">Mape L diskā:</label><input id="ldrive" type="text" name="ldrive" placeholder="Mape L diskā" value="" /></br> 

Produkti: <a href="#" class="addRow"><img src="<?php echo site_url('/img/plus-icon.png'); ?>" width="15px"></a><br/> 
<table class="multi"> 
<!-- table title --> 
<tr><th>Produkts</th><th>Vienību skaits</th><th>Riska segums:</th><th>Deadline:</th><th>Atjaunojums</th><th>[Option]</th></tr> 
<!-- row template, when added new row --> 
<tr style="display:none;" class="templateRow"> 
<td><select name="store[product][]"> 
<option value="" selected="selected">-Produkts-</option> 
<option value="430">OCTA</option> 
<option value="440">KASKO</option> 
</select></td> 
<td><input type="text" name="store[quantity][]" /></td> 
<td><select name="store[segums][]"> 
<option value="" selected="selected">-Riska segums-</option> 
<option value="Mixed park">Mixed park</option> 
<option value="Light vehicle">Light vehicle</option> 
<option value="Trucks">Trucks</option> 
<option value="Buss">Buss</option> 
</select></td> 
<td><input type="date" name="store[deadline][]" class="datepicker" /></td> 
<td><input type="checkbox" name="store[renewal][]" value="1" /></td> 
<td><a class="del" href="#"><img src="<?php echo site_url('img/minus-icon.jpg'); ?>" width="15px"></a></td> 
</tr> 
<!-- default values --> 
<tr> 
<td><select name="store[product][]" > 
<option value="" selected="selected">-Produkts-</option> 
<option value="430">OCTA</option> 
<option value="440">KASKO</option> 
</select></td> 
<td><input type="text" name="store[quantity][]" /></td> 
<td><select name="store[segums][]"> 
<option value="" selected="selected">-Riska segums-</option> 
<option value="Mixed park">Mixed park</option> 
<option value="Light vehicle">Light vehicle</option> 
<option value="Trucks">Trucks</option> 
<option value="Buss">Buss</option> 
</select></td> 
<td><input type="date" name="store[deadline][]" class="datepicker" /></td> 
<td><input type="checkbox" name="store[renewal][]" value="1" /></td> 
<td></td> 
</tr> 
<!-- /default values --> 
</table> 
+1

等什麼是你嘗試的結果?任何可以幫助我們的錯誤消息? – DevDonkey

+0

數據如何存儲? 「值(...),(...),(...)....」;「 - 這可能只是一個很長的字符串中的」0,1,2,3,4「的值? – Christian

+0

表格看起來像: http://s12.postimg.org/t4shn4mml/form_example.png – AlexIL

回答

1

從你的問題,看起來這是你追求的

$itemCount = sizeof($array['store']['product']); 

for ($i = 0; $i < $itemCount; $i++) { 
    $sql_rec = "INSERT INTO tsales_funnel_mrecord (product, quantity, segums, deadline) VALUES ("' . $array['store']['product'][$i] . '", "' . $array['store']['quantity'][$i] . '", "' . $array['store']['segums'][$i] . '", "' . $array['store']['deadline'][$i] . '");"; 

    // Run the sql statement on the database here 
} 

你需要確保所有用戶提供的值存儲在數據庫之前正確轉義。

+0

我有一個錯誤與此,$ itemCount返回0,並且代碼不起作用 – AlexIL

+0

已修改您的代碼,謝謝! – AlexIL

0

如果數組稱爲$陣列,那麼你就可以訪問像這樣的數組值;

// product; 
$array['store']['product']; 
// quantity; 
$array['store']['quantity']; 
// etc. 

然後,如果他們進入一列(這是壞的形式,我不建議,那麼你可以做這樣的事情;

// product; 
$prod_string = ''; 
foreach ($array['store']['product'] as $key => $prod) { 
    $prod_string .= $prod; 
} 

然後你可以使用$prod_string

OR,您的查詢,如果你需要插入的每個鍵的排;

// We use the key from the product loop to get the others; 
foreach ($array['store']['product'] as $key => $prod) { 
    $prod_val = $prod; 
    $qty_val = !empty($array['store']['quantity'][$key]) ? $array['store']['quantity'][$key] : ''; 
    $seg_val = !empty($array['store']['segums'][$key]) ? $array['store']['segums'][$key] : ''; 
    $dl_val = !empty($array['store']['deadline'][$key]) ? $array['store']['deadline'][$key] : ''; 
    // Right here create your query and insert. 
    $sql_rec = "INSERT INTO tsales_funnel_mrecord (product, quantity, segums, deadline) VALUES ($prod_val, $qty_val, $seg_val, $dl_val);" 
    // I'm not sure what library you're using for your db management, so will leave that out. 
} 

然後你會得到每個人的價值。

注意 - 我還沒有檢查乾淨的後期值。即消毒輸入。這超出了這個問題的範圍。

+0

試圖運行您的代碼,有錯誤: 警告:無效的參數foreach() – AlexIL

+0

也許你應該用你正在運行的確切代碼更新你的問題。請記住,一個警告不是一個錯誤,代碼執行不會停止。 – Christian

+0

我已經用html代碼更新我的問題。 我已經嘗試在你的代碼之前插入$ array = $ _POST;並且差不多完成了。有錯誤: INSERT INTO tsales_funnel_mrecord(Product_type,Vien_skaits,Riska_segums,Deadline)VALUES(440,2321,Light vehicle,2015-08-07); 您的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,在第1行附近'vehicle,2015-08-07)'使用正確的語法 – AlexIL

0

都做到了:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {  
// We use the key from the product loop to get the others; 
$array = $_POST; 
$itemCount = sizeof($array['store']['product']); 
// Loop through all $itemCount 
$values_string = ''; 
for ($i = 0; $i < $itemCount; $i++) { 
    $prod = esc_sql($array['store']['product'][$i]); 
    $quant = esc_sql($array['store']['quantity'][$i]); 
    $seg = esc_sql($array['store']['segums'][$i]); 
    $deadline = esc_sql($array['store']['deadline'][$i]); 
    $renewal = esc_sql($array['store']['renewal'][$i]); 
    if ($i < $itemCount - 1) { 
    $new_str = "('".$prod."','".$quant."','".$seg."','".$deadline."','".$renewal."'),"; 
    } else{ 
    $new_str = "('".$prod."','".$quant."','".$seg."','".$deadline."','".$renewal."');"; 
    } 
    $values_string .= $new_str; 
} 
// Run the sql statement on the database here 
$sql_rec = "INSERT INTO tsales_funnel_mrecord (Product_type, Vien_skaits, Riska_segums, Deadline, Atjaunojums) VALUES $values_string"; 
$wpdb->query($sql_rec); 
} 
+0

正如我在我的答案的評論中所說的,esc_sql()不會防止注入攻擊。 – Christian