我设计了表格。仅创建了两个字段fistname,lastname。输入fistname和lastname后,单击add按钮记录成功地追加到表上。但是当我尝试将所有表记录保存到数据库中时。我没有收到任何错误,但是记录没有保存到数据库中。编码我尝试过的内容,以便在下面进行编写。拜托,任何人,请帮助我给我适当的解决方案,将记录添加到数据库中。谢谢
表格
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered" id="product">
<caption> Products</caption>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
<form class="form-horizontal" id="product_list">
<tbody> </tbody>
</form>
</table>
</div>
</div>
表格
<div class="row">
<div class="col-sm-12">
<div class="col-md-1">
<label>firstname</label>
<input type="text" class="form-control" id="firstname" name="firstname">
</div>
<div class="col-md-1">
<label>lastname</label>
<input type="text" class="form-control" id="lastname" name="lastname">
</div>
<div class="col-md-1">
<button type="button" id="save" class="btn btn-info" onclick="addProject()">Upadate
</button>
<button type="button" id="clear" class="btn btn-warning" onclick="save()">Save</button>
</div>
</div>
</div>
JQuery
function addProject()
{
var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
var markup = "<tr><td>" + firstname + "</td><td>" + lastname + "</td></tr>";
$("table tbody").append(markup);
}
function save() {
var table_data = [];
$('table tbody tr').each(function(row,tr)
{
var sub = {
'firstname' : $(tr).find('td:eq(0)').text(),
'lastname' : $(tr).find('td:eq(1)').text(),
};
table_data.push(sub);
});
var _method;
$.ajax({
type : "POST",
url : "save.php",
dataType: "JSON",
data : {data:table_data},
success: function (data) {
console.log(table_data);
}
});
}
save.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sal";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
for($x = 0; $x < count($relative_list); $x++)
{
$stm = $conn->prepare("INSERT INTO record(firstname,lastname)
VALUES (?,?)");
$stm->bind_param("ss",$firstname,$lastname);
$firstname= $relative_list[$x]['firstname'];
$lastname= $relative_list[$x]['lastname'];
if ($stm->execute()) {
echo 1;
} else {
echo $conn->error;
}
$stm->close();
}
?>