I am using the following script to try and insert new rows to my database. For some reason only the first row is inserted, it is like the other rows are not posted. I can't find what is going wrong, I get no error logs and see no typos nor differences..
Does someone know what I am doing wrong?
The PHP & SQL:
if(isset($_POST['submitnewacc']))
{
for($i=0;$i<count($_POST['slno']);$i++)
{
$name = $_POST['name'][$i];
$email = $_POST['accmail'][$i];
$password = $_POST['password'][$i];
$bullets = $_POST['bullets'][$i];
$notes = $_POST['notes'][$i];
$rang = $_POST['rang'][$i];
$betaald = $_POST['betaald'][$i];
$paydate = $_POST['paydate'][$i];
if ( trim($name) != '') { $nameacc = trim($name); }
if ( trim($email) != '') { $emailacc = trim($email); }
if ( trim($password) != '') { $passwordacc = trim($password); }
if($betaald =='') { $betaald = 'No'; }
if($nameacc != '' && $emailacc != '' && $passwordacc != '' && $bullets >= '0')
{
$stmt16 = $mysqli->prepare("
INSERT INTO account (name,email,password,bullets,rang,betaald,testpay,door,group_name,notes)
VALUES (trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?))");
$stmt16->bind_param('ssssssssss',$nameacc,$emailacc,$passwordacc,$bullets,$rang,$betaald,$paydate,$usernamesession,$groupname,$notes);
$stmt16->execute();
$stmt16->close();
if($stmt16 == TRUE)
{
$log_page = "Newaccount";
$log_account = filter_var($name, FILTER_SANITIZE_STRING);
$datum = filter_var(date ("Y-m-d H:i:s"), FILTER_SANITIZE_STRING);
$message = filter_var($usernamesession." added a new account: ".$log_account." (rang: ".$rang.", bullets: ".$bullets.",
, paying: ".$betaald.", notes: ".$notes.")", FILTER_SANITIZE_STRING);
$userip = htmlspecialchars(filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING));
$stmt12 = $mysqli->prepare("INSERT INTO logs (user_ID, log_action, log_time, log_ip, log_account, log_page,
log_owner, log_group) VALUES (trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?),trim(?))");
$stmt12->bind_param('isssssss',$userid,$message,$datum,$userip,$log_account,$log_page,$usernamesession,$groupname);
$stmt12->execute();
$stmt12->close();
$_SESSION['addacc'] = '<p><b><font color="orange">You have succesfully added the accounts to the database!</p>
</font></b></ br>' ;
header('Location: ' . 'lijst_myaccounts.php');
exit();
}
//echo '<div class="alert alert-success" role="alert">Submitted Successfully</div>';
}
else
{
echo '<div class="alert alert-danger" role="alert">Error Submitting in Data</div>';
}
}
}
The form
<table class="userprof" align='left' >
<tr>
<td class="footer">Options: </td>
<td class="footer" >
<button type="button" name="addrow" id="addrow" class="example_e">Add New Row</button>
</td>
</tr> </table><br /><br />
<br/><br/><br/>
<script src="java/sorttable.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<form class="form-horizontal"action="<?php htmlspecialchars($filterpage); ?>" method="post">
<div class="divTable blueTable" style="float:left !important;">
<div class="divTableHeading">
<div class="divTableRow">
<div class="divTableHead" style="width:0px !important;"></div>
<div class="divTableHead">Account Name</div>
<div class="divTableHead">Emailadress</div>
<div class="divTableHead">Password</div>
<div class="divTableHead">Rang</div>
<div class="divTableHead">Bullets</div>
<div class="divTableHead">Notes</div>
<div class="divTableHead">Paying</div>
<div class="divTableHead">Paying untill</div>
<div class="divTableHead" style="width:2px !important;"></div>
</div>
</div>
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCell"><input type="hidden" style="width:2px !important;" class="form-control sl"
name="slno[]" id="slno" value="1" readonly=""></div>
<div class="divTableCell"><input type="text" style="width:190px !important;font-weight: normal !important;"
class="form-control" name="name[]" id="acc_name" placeholder="Enter Account Name" required minlength="3" maxlength="150"></div>
<div class="divTableCell"><input type="email" style="width:210px !important;font-weight: normal !important;"
class="form-control" name="accmail[]" id="acc_email" placeholder="mail@mail.nl" required minlength="3" maxlength="150"
></div>
<div class="divTableCell"><input type="text" style="width:130px !important;font-weight: normal !important;"
class="form-control" name="password[]" id="acc_pass" placeholder="Enter Password" required minlength="3" maxlength="50"></div>
<div class="divTableCell">
<select name="rang[]" id="rang_acc" style="width:80px !important;font-weight: normal !important;" class="form-control">
<option value="Bacteria">Bacteria</option>
<option value="Low Life">Low Life</option>
<option value="Apprentice">Apprentice</option>
</select>
</div>
<div class="divTableCell"><input type="number" style="width:60px !important; font-weight: normal !important;"
required pattern="[0-9]+" value="0" onClick="this.select()" name="bullets[]" id="acc_bullets" class="form-control"></div>
<div class="divTableCell"><input type="text" name="notes[]" id="acc_notes" maxlength="250"
style="width:210px !important; font-weight: normal !important;" placeholder="OPTIONAL enter notes" class="form-control"/></div>
<div class="divTableCell">
<label class="switch">
<input type="checkbox" name="betaald[]" id="acc_betaald" value="Yes" checked/>
<div class="slider round">
<span class="on">Yes</span>
<span class="off">No</span>
</div>
</label></div>
<div class="divTableCell"><input type="text" name="paydate[]" value="2020-01-01"
style="width:100px !important; font-weight: normal !important; color:#F00"
id="acc_paydate" required pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])"
class="form-control" /></div>
<div class="divTableCell"></div>
</div>
</div>
</div>
<br/>
<button type="submit" name="submitnewacc" class="example_e">Add account(s) to database!</button>
</form>
The Script
<script type="text/javascript">
const tableRow = (i) => {return `
<div class="divTableRow">
<div class="divTableCell" >
<input type="hidden" class="form-control sl" style="width:2px !important;" name="slno[]" value="${ i }" readonly="">
</div>
<div class="divTableCell">
<input type="text" style="width:190px !important;font-weight: normal !important;"
class="form-control" name="name[]" id="acc_name${ i }" placeholder="Enter Account Name"
required minlength="3" maxlength="150" >
</div>
<div class="divTableCell"><input type="email" style="width:210px !important;font-weight: normal !important;"
class="form-control" name="accmail[]" id="acc_email${ i }" placeholder="mail@mail.nl" required minlength="3"
maxlength="350" ></div>
<div class="divTableCell"><input type="text" style="width:130px !important;font-weight: normal !important;"
class="form-control" name="password[]" id="acc_pass${ i }" placeholder="Enter Password" required minlength="3" maxlength="150"
></div>
<div class="divTableCell">
<select name="rang[]" id="rang_acc${ i }" style="width:80px !important;font-weight: normal !important;"
class="form-control">
<option value="Bacteria">Bacteria</option>
<option value="Low Life">Low Life</option>
<option value="Apprentice">Apprentice</option>
</select>
</div>
<div class="divTableCell"><input type="number" style="width:60px !important; font-weight: normal !important;"
required pattern="[0-9]+" value="0" onClick="this.select()" name="bullets[]" id="acc_bullets${ i }" class="form-control"></div>
<div class="divTableCell"><input type="text" name="notes[]" id="acc_notes${ i }" maxlength="250"
style="width:210px !important; font-weight: normal !important;" placeholder="OPTIONAL enter notes" class="form-control"/></div>
<div class="divTableCell">
<label class="switch">
<input type="checkbox" name="betaald[]" id="acc_betaald${ i }" value="Yes" checked/>
<div class="slider round">
<span class="on">Yes</span>
<span class="off">No</span>
</div>
</label></div>
<div class="divTableCell"><input type="text" name="paydate[]" id="acc_paydate${ i }" value="2020-01-01"
style="width:100px !important; font-weight: normal !important; color:#F00"
required pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" class="form-control" /></div>
<div class="divTableCell">
<input type="button" class="btnRemove example_b" value="Delete"/>
</div>
</div>
`
}
$('#addrow').click(function() {
var length = $('.sl').length;
var i = parseInt(length) + parseInt(1);
var newrow = $('.divTableBody').append(tableRow(i));
});
// Removing event here
$('body').on('click', '.btnRemove', function() {
$(this).closest('div').parent().remove()
});
</script>