我在使用 Jquery 更新 MySQL 行时遇到问题。
这是我当前的代码。
<script type="text/javascript">
$(document).ready(function(){
$(".insert").click(function(){
var id=$("#id").val();
var ip=$("#ip").val();
var permissions=$("#permissions").val();
var password=$("#password").val();
var key=$("#key").val();
$.post('myscript.php?action=updateAPIuser', {id: id, ip: ip, permissions: permissions, password: password, key:key },
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(1500);
});
return false;
});
});
</script>
这是html:
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<th>Customer</th>
<th>Permission</th>
<th>IP</th>
<th>Key</th>
<th>Password</th>
<th>Date added</th>
<th>Action</th>
</tr>
<tr class="record" id="record-1"><td>John Doe</td>
<td><select id="permissions" name="permissions" />
<option value="ro" selected>Read Only</option>
<option value="rw">Read Write</option>
</select></td>
<td><input type="text" id="ip" name="ip" value="1.1.1.13">
<input type="hidden" id="id" name="id" value="1"></td>
<td><input type="text" id="key" name="key" value="mijnkey"></td>
<td><input type="text" id="password" name="password" value="password000"></td>
<td>2012-10-24 20:48:25</td><td><a class="insert" title="Update data" href="#"><img src="images/icons/save.png" title="Save" alt="Save"></a>
</td></tr>
<tr class="record" id="record-2"><td>Jane Doe</td>
<td><select id="permissions" name="permissions" />
<option value="ro" selected>Read Only</option>
<option value="rw">Read Write</option>
</select></td>
<td><input type="text" id="ip" name="ip" value="2.2.1.13">
<input type="hidden" id="id" name="id" value="1"></td>
<td><input type="text" id="key" name="key" value="janeskey"></td>
<td><input type="text" id="password" name="password" value="pass000"></td>
<td>2012-10-24 20:48:25</td><td><a class="insert" title="Update data" href="#"><img src="images/icons/save.png" title="Save" alt="Save"></a>
</td></tr>
</table>
当然,我有用于更新 MySQL 行的 php。
问题是这样的:当我点击保存(又名更新)时,Jquery 总是更新 ID 为 1 的行。无论我点击哪一行。
所以我的问题是:如何更改代码,以便正确的行得到更新?
干杯,枢纽
PS这是我更新行的PHP代码:
function updateAPIuser($req){
global $db_whmcs, $_LANG;
$id = $req['id'];
$ip = $req['ip'];
$permissions = $req['permissions'];
$password = $req['password'];
$key = $req['key'];
$updated = date("Y-m-d H:i:s");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
$query = "UPDATE `mod_powerdns_api` SET
`ip` = '".$ip."',
`key` = '".$key."',
`permissions` = '".$permissions."',
`updated` = '".$updated."',
`password` = '".$password."'
WHERE `id` = ".$id;
mysql_query($quer) or pdns_die('Can not update user'.mysql_error());
} // 结尾