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):See https://stackoverflow.com/a/50654198/3783243 for potential
bind_param
issues.Then call it like: