我想用AJAX请求更新每条记录的数据库。问题是,当我提交编辑时,它给我一个500错误。更新' RecordData'给了我所有的数据,但就是这样。
我的更新文件。
$data = $_POST['recordData'];
str_parse($data, $data);
echo "Record Id is: " . $data[0];
echo $query = "UPDATE bier
SET naam = '$data['naam'], brouwer = '$data['brouwer']', type = '$data['type']', gisting = '$data['gisting']', perc = '$data['perc']', inkoop_prijs = '$data['inkoop_prijs']'
WHERE id= '".$data['id']."'";
if (mysqli_query($conn, $query)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
echo $_POST['recordId'];
我的AJAX功能
function editRecord(data){
console.log(data);
$.ajax({
type: "POST",
data: {
database: 'updateRecords',
recordData: data.serialize()
},
url: "actions/update.php",
dataType: "html",
async: false,
success: function(data) {
ModalClose();
}
});
}
我的表格:
<form id="FormBier" action="#" method="post">
<table>
<tr>
<td>Id</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>Naam</td>
<td><input type="text" name="naam"></td>
</tr>
<tr>
<td>Brouwer</td>
<td><input type="text" name="brouwer"></td>
</tr>
<tr>
<td>Type</td>
<td><input type="text" name="type"></td>
</tr>
<tr>
<td>Gisting</td>
<td><input type="text" name="gisting"></td>
</tr>
<tr>
<td>Percentage</td>
<td><input type="text" name="perc"></td>
</tr>
<tr>
<td>Inkoop Prijs</td>
<td><input type="text" name="inkoop_prijs"></td>
</tr>
<tr>
<input type="submit" onclick="editRecord($('#FormBier'))" value="bewerk">
</tr>
</table>
</form>
</div>
答案 0 :(得分:0)
使用isset检查POST和变量,并按照下面的代码更改查询。
echo $query = 'UPDATE bier SET naam = '.$data["naam"].', brouwer = '.$data["brouwer"].', type = '.$data["type"].', gisting = '.$data["gisting"].', perc = '.$data["perc"].', inkoop_prijs = '.$data["inkoop_prijs"].' WHERE id= '.$data["id"];