我有这段代码试图更新某些值。 我可以看到我正在从ajax调用中获取所有数据,这是...
$(document).on('click', '#commitbtn1', function() {
var did = document.getElementById("chgelement").value;
var pname = document.getElementById("projects").value;
var elem = document.getElementById("elem").value;
var respon = document.getElementById("respon").value;
var coord = document.getElementById("coord").value;
var total = document.getElementById("total").value;
var submt = document.getElementById("submt").value;
var delv = document.getElementById("delv").value;
$.ajax({
type:'POST',
url:'update_details.php',
data: {pname:pname,elem:elem,respon:respon,coord:coord,total:total,submt:submt,delv:delv,did:did},
timeout: 30000,
success:function(html){
alert(html);
alert("Updated !!");
$('#prevrec').html(html);
},
error:function(error){
console.error(error);
alert(error);
}
});
});
通过ajax重定向或调用php页面“ update_details.php”, 我可以看到alert(“ Updated !!”)。但是,我在phpmyadmin中验证的查询未得到执行... 这是我的文件:
<?php
include 'connect.php';
if(isset($_POST['elem'])||isset($_POST['respon'])||isset($_POST['coord'])||isset($_POST['total'])||isset($_POST['submt'])||isset($_POST['delv'])){
$pname=$_POST['pname'];
$elem=$_POST['elem'];
$respon=$_POST['respon'];
$coord=$_POST['coord'];
$total=$_POST['total'];
$submt=$_POST['submt'];
$delv=$_POST['delv'];
$did=$_POST['did'];
if ( mysqli_connect_errno() ) {
printf( "Connect failed: %s\n", mysqli_connect_error() );
exit();
}
$q1 = $conn->query("SELECT `project_id` FROM `projects` WHERE `project_name` = '$pname';");
$rowCount1 = $q1->num_rows;
if($rowCount1 > 0){
while($row1 = $q1->fetch_assoc()){
$pid = $row1['project_id'];
$conn->query("UPDATE `project_details` SET `elements` = '$elem', `responsibilty` = '$respon', `coordinator` = '$coord', `total_level` = '$total', `sub_status` = '$submt', `del_status` = '$delv' WHERE `project_details`.`detail_id` = '$did';");
$q2 = $conn->query("SELECT * FROM `project_details` WHERE `detail_id` = (SELECT MAX(`detail_id`) FROM `project_details`);");
$rowCount2 = $q2->num_rows;
if($rowCount2 > 0){
echo'
<div class="text-primary">Previous status row entered</div>
<div class="row">
<div class="col-11"><br>
<table>
<thead>
<tr>
<th class=""></th>
<th class=""></th>
<th class=""></th>
<th class=""></th>
<th class=""></th>
<th class=""></th>
</tr>
</thead>
<tbody>';
while($row = $q2->fetch_assoc()){
echo'
<tr>
<td><input type="text" class="prev_input" id="pre_elem" placeholder="" readonly value="'.$row["elements"].'"></td>
<td><input type="text" class="prev_input" id="pre_respon" placeholder="" readonly value="'.$row["responsibilty"].'"></td>
<td><input type="text" class="prev_input" id="pre_coord" placeholder="" readonly value="'.$row["coordinator"].'"></td>
<td><input type="text" class="prev_input" id="pre_total" placeholder="" readonly value="'.$row["total_level"].'"></td>
<td><input type="text" class="prev_input" id="pre_submt" placeholder="" readonly value="'.$row["sub_status"].'"></td>
<td><input type="text" class="prev_input" id="pre_delv" placeholder="" readonly value="'.$row["del_status"].'"></td>
</tr>';
}
echo'
</tbody>
</table>
</div>
</div>';
}
}
}
}
?>
我无法理解我在做什么错。有人可以帮忙吗? 预先感谢!
答案 0 :(得分:0)
您在写$
之前错过了conn->query("UPDATE ..
号。将会是$conn->query("UPDATE ..
。
答案 1 :(得分:0)
显示警报是因为php文件已成功加载。请尝试以下操作:
$conn->query("UPDATE `project_details` SET `elements` = '$elem', `responsibilty` = '$respon', `coordinator` = '$coord', `total_level` = '$total', `sub_status` = '$submt', `del_status` = '$delv' WHERE `project_details`.`detail_id` = '$did';") or die($con->error);
查看查询中是否显示任何错误。
答案 2 :(得分:0)
这将起作用:
$conn->query("UPDATE project_details SET elements = '$elem', responsibilty = '$respon', coordinator = '$coord', total_level = '$total', sub_status = '$submt', del_status = '$delv' WHERE project_details.detail_id = '$did';");
注意:您从phpmyadmin复制了查询,因此您有``迹象表明php不接受,请确保将其删除。