在我的程序中,有mysql表“company”。companyid和activation_code是该表中的一些列。i显示html表中的表值,其中包括comapproval.php页面。html表的每一行都有一个接受按钮当我点击该按钮时,公司表应该更新,并且该行从 html 表中删除。更新代码位于“approvecompany.php”页面中。我为此使用了 jquery ajax。当我点击接受按钮时,我只收到成功警报。但是我无法更新表。如果 html 表,即使成功,警报也只能获取第一行。我是 ajax 新手。请帮我解决这个问题。
comapproval.php
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th>Action</th>
<th>ID</th>
<th>Registration number</th>
<th>Company Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$conn = mysqli_connect("localhost", "root", "", "internship");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "";
$sql = "select * from company where activation_code=0";
$res = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($res)):
?>
<tr>
<td align="center"><input type="submit" class="btn btn-default" value="Accept" id="accept" name="accept"></input></td>
<td><?php echo $row['companyid']; ?></td>
<td><?php echo $row['government_reg_no']; ?></td>
<td><?php echo $row['company_name']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php
endwhile
?>
</tbody>
</table>
<script>
$("#accept").click(function () {
$.ajax({
type:"POST",
url:"approvecompany.php",
data:{comid:$('<?php $row['companyid']?>').val()},
success:function () {
alert('Successfully approved');
window.location.reload(true);
}
});
});
</script>
approvecompany.php
<?php
$conn=mysqli_connect("localhost","root","","internship");
$comid=$_POST['comid'];
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="update company set activation_code=1 where companyid=$comid";
if ($conn->query($sql) === TRUE) {
?>
<?php
}
else{
?><script>alert("Error...")</script><?php
}
?>
最佳答案
Try Rewriting
comapproval.php
as below:
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th>Action</th>
<th>ID</th>
<th>Registration number</th>
<th>Company Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$conn = mysqli_connect("localhost", "root", "", "internship");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "";
$sql = "select * from company where activation_code=0";
$res = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($res)):
?>
<tr>
<td align="center">
<button type="submit" class="btn btn-default myButton" value="<?php echo $row['companyid']; ?>" id="accept" name="accept">Accept</button>
</td>
<td><?php echo $row['companyid']; ?></td>
<td><?php echo $row['government_reg_no']; ?></td>
<td><?php echo $row['company_name']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php
endwhile
?>
</tbody>
</table>
<script>
$(".myButton").click(function () {
var company_id = $(this).val();
$.ajax({
type:"POST",
url:"approvecompany.php",
data:{ comid: company_id },
success:function () {
window.location.reload(true);
}
});
});
</script>
关于php - jquery ajax更新mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43400791/