简体   繁体   中英

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. So here's what i have so far, sorry for my bad English, I am a learner and u guys are the best.

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:

$("#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:

    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. 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):

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.

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:

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);
        }
    }  
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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