在我的程序中有mysql表'company'.companyid和activation_code是該表中的一些列.i顯示錶中的值在html表中包含comapproval.php page.that html表每行都有一個接受按鈕,當我點擊該按鈕時,公司表應該更新,該行從html table.update中刪除是'approvecompany.php'page.i使用jquery ajax for this.when我cliick接受按鈕,我得到只成功alert.but我不能做updaet table.even sucessfull警報只獲得第一行如果html table.i是新的ajax.help我來解決這個問題。jquery ajax更新mysql表
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
}
?>
[小巴比](HTTP://巴比-tables.com/)說* ** [您的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***瞭解[已準備好](http://en.wikipedia.org/wiki/Prepared_statement)對[MySQLi]的聲明(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –
[你看過瀏覽器開發工具中的AJAX請求/響應了嗎?你有沒有在項目中包含jQuery庫?是否有任何錯誤報告?你是在網絡服務器上運行這個嗎?](http://jayblanchard.net/basics_of_jquery_ajax.html) –
[ID必須是唯一的](http://stackoverflow.com/questions/5611963/can-multiple-different- html-elements-have-the-same-id-if-theyre-different-eleme),具體是因爲它會導致[JavaScript]中的問題(https://developer.mozilla.org/en-US/docs/Web/ HTML/Global_attributes/id)和CSS,當你嘗試與這些元素進行交互時。如果你有多行'id ='accept'',事情就不會按計劃進行。將其更改爲一個類。 –