1

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; } 
}
3
  • Yes is related bcz has to pass form jquery repeater to ajax , i think Commented May 11, 2021 at 20:12
  • My answer is only focusing on the mysql/php interactions.. that sounds like the root of the problem though Commented May 11, 2021 at 20:28
  • Thank you a lott, i will test'it now Commented May 11, 2021 at 20:30

2 Answers 2

0

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Fatal error: Uncaught Error: Cannot unpack array with string keys
Opps, can you try with updated array_values function?
yep, now i got this: Fatal error: Uncaught Error: Call to a member function bind_param() on boolean
What does var_dump(implode('', array_fill(0, count($fields), 's')), ...array_values($array)); give back?
string(4) "ssss" string(5) "test1" string(2) "33" string(5) "23:55" string(2) "33"
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.