从数据数组写入循环sql查询(php,mysql)

时间:2015-08-14 07:58:51

标签: php jquery mysql arrays wordpress

我的php脚本有问题。

我有一个数组,它是从表单生成的,其中$_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 insert statment并循环它。

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

HTML CODE:

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

3 个答案:

答案 0 :(得分: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
}

您需要确保在存储到数据库之前正确转义所有用户提供的值。

答案 1 :(得分:0)

如果Array被称为$ array,那么你可以像这样访问数组值;

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

然后,如果他们要进入一个专栏(这是一个糟糕的形式,我不建议,那么你可以做这样的事情;

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

然后,您可以在查询中使用$prod_string

或者,如果你需要为每个键插入一行;

// 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. 
}

然后你将拥有每个人的价值。

注意 - 我没有检查干净的帖子值。即,消毒输入。这不属于这个问题的范围。

答案 2 :(得分: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); 
}