我正在使用php,Ajax和MySQL。当我提交表单数据时,数据库表中没有更新。我被困在这里。任何人都可以建议我,我该如何解决这个问题?先谢谢你们。代码如下:
update_details.php
<form id="form6" name="form6" method="post">
<div class="row">
<h4 style="font-size: 16px;padding-left: 15px;"><b>Benefits Delivered by Local People from Plantation Felling:</b></h4><br>
<div class="col-sm-6">
<div align="right" style="padding-right: 10px;">
<label style="font-weight:300;font-size: 15px;margin-top:5px; ">Dependence of Local People on the Site:
</label>
<select name="dependence_of_local_people" id="dependence_of_local_people" style="width:30%;height: 30px; margin-left: 10px; border-radius: 5px;margin-top:5px; ">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br><br>
</div>
</div>
<div class="col-sm-4">
<div align="right" style="padding-right: 10px;">
<label style="font-weight:300;font-size: 15px;">Wage Income:</label>
<input type="text" name="wage_income" id="wage_income" style="width:50%;height: 30px; margin-left: 10px; border-radius: 5px;"><br>
</div>
</div>
<div class="col-sm-2">
<div align="right" style="padding-right: 0px;">
<input class="btn btn-info btn-submit6" type="submit" name="submit5" value="Update" onclick="move5()">
</div>
</div>
</div>
</form>
<script>
jQuery(document).ready(function() {
$('.btn-submit6').click(function(e) {
e.preventDefault();
$.ajax({
url:"modify_func.php",
method:"POST",
data:$('#form6').serialize(),
success:function(data)
{
document.getElementById("form6").reset();
}
});
});
});
</script>
modify_func.php
<?php
$con=mysqli_connect("localhost","root","","forestdb");
if(isset($_POST['dependence_of_local_people'])){
$plantation_journal_no=$_GET['id'];
$dependence_of_local_people=$_POST['dependence_of_local_people'];
$wage_income=$_POST['wage_income'];
$sql="UPDATE `benefits_derived` SET `dependence_of_local_people`='$dependence_of_local_people',`wage_income`='$wage_income' WHERE `plantation_journal_no`='$plantation_journal_no'";
$result=mysqli_query($con,$sql);
}
?>
答案 0 :(得分:1)
我认为使用$_GET['id'];
你试图获取一些url参数,该参数存在于显示表单的url中,但是你使用jquery提交表单,因此$plantation_journal_no=$_GET['id'];
不能在你的代码中使用因为$_GET['id']
未设置。
试试这个:
$('.btn-submit6').click(function(e) {
e.preventDefault();
$.ajax({
url:"modify_func.php",
method:"POST",
data:$('#form6').serialize() + '&id=<?php echo $_GET['id']; ?>',
success:function(data)
{
document.getElementById("form6").reset();
}
});
});
比你在modify_func.php中使用这个:
$plantation_journal_no=$_POST['id'];