Php 数据库中未存储多行
我正在php中使用jQuery和Ajax。我想一次提交多行。但我的代码无法正常工作,因为“种植园期刊号”是种植园期刊详细信息的主键,也是过去历史物种详细信息的外键。我被困在这里了。谁能告诉我如何解决这个问题??提前谢谢各位。代码如下: index.phpPhp 数据库中未存储多行,php,jquery,ajax,pdo,Php,Jquery,Ajax,Pdo,我正在php中使用jQuery和Ajax。我想一次提交多行。但我的代码无法正常工作,因为“种植园期刊号”是种植园期刊详细信息的主键,也是过去历史物种详细信息的外键。我被困在这里了。谁能告诉我如何解决这个问题??提前谢谢各位。代码如下: index.php <div class="row"> <form method="post" id="insert_form2" style="padding-right: 10px;"> <div clas
<div class="row">
<form method="post" id="insert_form2" style="padding-right: 10px;">
<div class="table-repsonsive">
<span id="error"></span>
<table class="table table-bordered" id="item_table2">
<tr>
<th>Species</th>
<th>Product</th>
<th>Quantity</th>
<th>Value (Rs.)</th>
<th><button type="button" name="add2" class="btn btn-success btn-sm add2"><span class="glyphicon glyphicon-plus"></span></button></th>
</tr>
</table>
</div>
<br>
<div align="right">
<input type="submit" name="submit4" class="btn btn-info" value="Add" onclick="move4()">
</div>
</form>
</div>
<script>
$(document).ready(function(){
$(document).on('click', '.add2', function(){
var html = '';
html += '<tr>';
html += '<td><input type="text" name="species[]" class="form-control species" /></td>';
html += '<td><select name="product[]" class="form-control product"><option value="">---Select---</option><option value="reserved">Reserved</option><option value="protected">Protected</option><option value="Unclassed">Unclassed</option></select></td>';
html += '<td><input type="text" name="quantity[]" class="form-control quantity" /></td>';
html += '<td><input type="text" name="value[]" class="form-control value" /></td>';
html += '<td><button type="button" name="remove2" class="btn btn-danger btn-sm remove2"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
$('#item_table2').append(html);
});
$(document).on('click', '.remove2', function(){
$(this).closest('tr').remove();
});
$('#insert_form2').on('submit', function(event){
event.preventDefault();
var error = '';
$('.species').each(function(){
var count = 1;
if($(this).val() == '')
{
error += "<p>Enter Item Name at "+count+" Row</p>";
return false;
}
count = count + 1;
});
$('.product').each(function(){
var count = 1;
if($(this).val() == '')
{
error += "<p>Enter Item Quantity at "+count+" Row</p>";
return false;
}
count = count + 1;
});
$('.quantity').each(function(){
var count = 1;
if($(this).val() == '')
{
error += "<p>Select Unit at "+count+" Row</p>";
return false;
}
count = count + 1;
});
$('.value').each(function(){
var count = 1;
if($(this).val() == '')
{
error += "<p>Select Unit at "+count+" Row</p>";
return false;
}
count = count + 1;
});
var form_data = $(this).serialize();
if(error == '')
{
$.ajax({
url:"insert2.php",
method:"POST",
data:form_data,
success:function(data)
{
if(data == 'ok')
{
$('#item_table2').find("tr:gt(0)").remove();
}
}
});
}
});
});
</script>
种
产品
量
价值(卢比)
$(文档).ready(函数(){
$(文档).on('单击','.add2',函数(){
var html='';
html+='';
html+='';
html+='---选择---保留保护包括';
html+='';
html+='';
html+='';
$('#item_table2')。附加(html);
});
$(文档).on('单击','.remove2',函数(){
$(this).closest('tr').remove();
});
$('#insert_form2')。在('submit',函数(事件){
event.preventDefault();
var错误=“”;
$('.species')。每个(函数(){
var计数=1;
if($(this.val()='')
{
错误+=“在“+count+”行输入项目名称”;
返回false;
}
计数=计数+1;
});
$('.product')。每个(函数(){
var计数=1;
if($(this.val()='')
{
错误+=“在“+count+”行输入项目数量”;
返回false;
}
计数=计数+1;
});
$('.quantity')。每个(函数(){
var计数=1;
if($(this.val()='')
{
错误+=“在“+count+”行选择单位”;
返回false;
}
计数=计数+1;
});
$('.value')。每个(函数(){
var计数=1;
if($(this.val()='')
{
错误+=“在“+count+”行选择单位”;
返回false;
}
计数=计数+1;
});
var form_data=$(this).serialize();
如果(错误=“”)
{
$.ajax({
url:“insert2.php”,
方法:“张贴”,
数据:表格数据,
成功:功能(数据)
{
如果(数据=='ok')
{
$('#item_table2')。查找(“tr:gt(0)”)。删除();
}
}
});
}
});
});
insert2.php
<?php
//insert.php;
if(isset($_POST["species"]))
{
$connect = new PDO("mysql:host=localhost;dbname=forestdb", "root", "");
$id=uniqid();
$sql="SELECT plantation_journal_no from plantation_journal_basic_details
where plantation_journal_no=(select max(plantation_journal_no) from
plantation_journal_basic_details);";
$state=$connect->prepare($sql);
$state->execute();
$row=$state->fetch();
$plantation_journal_no = $row['plantation_journal_no'];
for($count = 0; $count < count($_POST["species"]); $count++)
{
$query = "INSERT INTO past_history_species_details
(id, plantation_journal_no, species, product, quantity, value)
VALUES (:id, :plantation_journal_no, :species, :product, :quantity,
:value) ";
$statement = $connect->prepare($query);
$statement->execute(
array(
':id' => $id,
':plantation_journal_no' => $plantation_journal_no,
':species' => $_POST["species"][$count],
':product' => $_POST["product"][$count],
':quantity' => $_POST["quantity"][$count],
':value' => $_POST["value"][$count]
)
);
}
$result = $statement->fetchAll();
if(isset($result))
{
echo 'ok';
}
}
?>
前端表:
后端表
我猜
id
是过去历史物种详细信息表的主键。您尝试插入的每一行都使用相同的uniqid()
。每次通过循环时都需要获得一个新ID
for($count = 0; $count < count($_POST["species"]); $count++)
{
$id = uniqid();
$query = "INSERT INTO past_history_species_details
(id, plantation_journal_no, species, product, quantity, value)
VALUES (:id, :plantation_journal_no, :species, :product, :quantity,
:value) ";
$statement = $connect->prepare($query);
$statement->execute(
array(
':id' => $id,
':plantation_journal_no' => $plantation_journal_no,
':species' => $_POST["species"][$count],
':product' => $_POST["product"][$count],
':quantity' => $_POST["quantity"][$count],
':value' => $_POST["value"][$count]
)
);
}
for($count=0;$countprepare($query);
$statement->execute(
排列(
':id'=>$id,
“:种植园日记帐号”=>$PLANTRAIN日记帐号,
“:物种”=>$\u POST[“物种”][$count],
“:product'=>$\u POST[“product”][$count],
':quantity'=>$\u POST[“quantity”][$count],
':value'=>$\u POST[“value”][$count]
)
);
}
如果您将此列设置为自动增量列,事情会变得更容易。您可以将第一个查询更改为仅从Plantain\u journal\u basic\u details中选择MAX(Plantain\u journal\u no)作为Plantain\u journal\u no
。这个问题没有很好地解释这个问题。我不认为使用种植园期刊\u no
作为过去的历史\u物种\u详细信息
的外键有问题。不起作用。仍然没有向数据库中插入多行。出现了什么错误?