简体   繁体   English

使用 jquery 中继器在 mysql 中插入多行

[英]Insert multiple rows in mysql using jquery repeater

I have a form repeater ( https://github.com/DubFriend/jquery.repeater ) that adds multiple inputs if needed, but the only thing is.. i don't know how to make'it to insert in sql all the data from form at once.我有一个表单转发器( https://github.com/DubFriend/jquery.repeater ),如果需要,它可以添加多个输入,但唯一的事情是..我不知道如何让它插入 sql 所有一次从表单中获取数据。 So here's what i have so far, sorry for my bad English, I am a learner and u guys are the best.所以这就是我到目前为止所拥有的,对不起我的英语不好,我是一个学习者,你们是最好的。

HTML: HTML:

<form action="" method="POST">
        <div class="modal-body">
          <div class="repeater-default">
            <div data-repeater-list="sectiuni" class="col-md-12">
              <div data-repeater-item="">
                <div class="row">
                  <div class="form-group mb-1 col-sm-12 col-md-4">
                    <label for="email-addr">Sectiunea</label>
                    <br>
                    <input type="text" class="form-control" id="sectiunea" name="sectiunea[]" placeholder="Introdu sectiunea">
                  </div>
                  <div class="form-group mb-1 col-sm-12 col-md-2">
                    <label for="pass">Nr.Dansatori</label>
                    <br>
                    <input type="number" class="form-control" name="nrdansatori[]" id="nrdansatori" placeholder="Numarul dansatorilor">
                  </div>
                  <div class="skin skin-flat form-group mb-1 col-sm-12 col-md-2">
                    <label for="tel-input">Timp piesa</label>
                    <br>
                    <input class="form-control" type="text" name="timpsectiune[]" id="timpsectiune" placeholder="2:34">
                  </div>

                  <div class="form-group mb-1 col-sm-12 col-md-2">
                    <label for="pret">Pret</label>
                    <br>
                    <input class="form-control" type="number" name="pretsectiune[]" id="pretsectiune" placeholder="250">
                  </div>
                  <div class="form-group col-sm-12 col-md-2 mt-1">
                    <button type="button" class="btn btn-danger" style="margin-top: 12px;" data-repeater-delete=""> <i
                        class="feather icon-trash"></i> Delete</button>
                  </div>
                  <hr>
                </div>
              </div>
            </div>
            <div class="form-group overflow-hidden">
              <div class="col-12">
                <button type="button" data-repeater-create="" class="btn btn-primary col-sm-12 btn-sm">
                  <i class="feather icon-plus"></i> ADD ONE MORE SECTION
                </button> 
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <input type="reset" class="btn btn-secondary" data-dismiss="modal" value="Close">
          <input type="submit" id="save" class="btn btn-success" value="Save">
        </div>
      </form>

Javascript: Javascript:

$("#save").click(function(e) {
        e.preventDefault();
        var sectiunea    = $("#sectiunea").val();
        var nrdansatori  = $("#nrdansatori").val();
        var timpsectiune = $("#timpsectiune").val();
        var pretsectiune = $("#pretsectiune").val();
        var infos = {
            sectiunea   : sectiunea,
            nrdansatori : nrdansatori,
            timpsectiune: timpsectiune,
            pretsectiune: pretsectiune
        };
        $.ajax({
            type: 'POST',
            data: infos,
            url: 'sql-insert.php',
            success: function(data) {
                if (data === TRUE) {
                    alert('Success'); 
                } else {
                     alert("ERROR");
                }
            }
       });

PHP: PHP:

    if(isset($_POST['sectiunea'])) {
    $table = "`".RDCP_PREFIX."sectiuni`";
        $data = array(
            'sectiune' => trim($db->escape($_POST['sectiunea'])),
            'max_d'    => trim($db->escape($_POST['nrdansatori'])),
            'timp'     => trim($db->escape($_POST['timpsectiune'])),
            'pret'     => trim($db->escape($_POST['pretsectiune']))
          );
          foreach ($data as $name) {
         
         $db->insert($table, $data);
          
        }
    } 
public function insert($table,$fields) { 
$field = array_keys($fields); 
$single_field = implode(",", $field); 
$val = implode("','", $fields); 
$stmt = $this->conn->prepare("INSERT INTO ".$table."(".$single_field.") VALUES('".$val."')"); 
$stmt->execute(); 
if($stmt === true) { echo true; } 
else { echo false; } 
}

Your insert function is likely the issue.您的insert function 可能是问题所在。 You are inserting each column into its own record, you also are misusing prepared statements.您将每一列插入到自己的记录中,您也在滥用准备好的语句。 You should write the function something like this (pseudo code below):您应该像这样编写 function (下面的伪代码):

public function insert($table, $array) { 
    $fields = array_keys($array); 
    $stmt = $this->conn->prepare('INSERT INTO ' . $table . '(' . implode(",", $fields) .') VALUES (' . implode(',', array_fill(0, count($fields), '?')) .')'); 
    $stmt->bind_param(implode('', array_fill(0, count($fields), 's')), ...array_values($array));
    $stmt->execute(); 
    if($stmt === true) { 
        echo true; 
    } else { 
        echo false; 
    } 
}

See https://stackoverflow.com/a/50654198/3783243 for potential bind_param issues.有关潜在的bind_param问题,请参阅https://stackoverflow.com/a/50654198/3783243

Then call it like:然后这样称呼它:

if(isset($_POST['sectiunea'])) {
    $table = "`".RDCP_PREFIX."sectiuni`";
    $data = array(
            'sectiune' => trim($_POST['sectiunea']),
            'max_d'    => trim($_POST['nrdansatori']),
            'timp'     => trim($_POST['timpsectiune']),
            'pret'     => trim($_POST['pretsectiune'])
          );
     $db->insert($table, $data);
}

I think i have done'it with this and is working bcz jquery repeater does the names like this cat[0]name:我想我已经完成了它并且正在工作 bcz jquery 中继器的名称类似于此 cat[0] 名称:

if(!empty(isset($_POST))) {
    $table = "`".RDCP_PREFIX."sectiuni`";
    foreach($_POST as $key => $value){
        for ($i=0; $i < count($value); $i++) {
            $data = array(
                'sectiune' => $value[$i]['sectiunea'],
                'max_d'    => $value[$i]['nrdansatori'],
                'timp'     => $value[$i]['timpsectiune'],
                'pret'     => $value[$i]['pretsectiune']
              );
          $db->insert($table, $data);
        }
    }  
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM