在下面的函数中,我有两个帖子请求。一个用于在数据库中插入值(submit.php),另一个用于根据businessid(edit.php)编辑数据库中的现有值。但是,脚本只执行第一部分。也就是说,当" keuze"是选择,当单击提交时,脚本很好地插入到数据库中。但是,除了" keuze"如果选择并单击提交,则脚本不会编辑数据库中的记录。我该怎么做才能解决这个问题?
submitformfunction: function(){
$(document).on('submit', '#form', function(){
var data = $(this).serialize();
var target = $('#busselect option:selected').val();
var businessid = $('#busselect').children(":selected").attr("id");
if(target == 'keuze'){
$.ajax({
type : 'POST',
url : 'submit.php',
data : data,
success : function(data)
{
alert('Company added');
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
}
else
{
$.ajax({
type : 'POST',
url : 'edit.php',
data : businessid+data,
success : function(data)
{
alert('Company modified');
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
}
});
},
submit.php:
<?php
$mysqli = new mysqli("localhost", "root", "", "brandveiligheid");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if($_POST )
{
$naam = addslashes(htmlentities($_POST['uitgevoerd_door_naam']));
$mysqli->query("INSERT INTO form (uitgevoerd_door_naam)
VALUES ('$naam')");
}
mysqli_close($mysqli);
?>
edit.php:
<?php
$mysqli = new mysqli("localhost", "root", "", "brandveiligheid");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if($_POST )
{
$naam = addslashes(htmlentities($_POST['uitgevoerd_door_naam']));
$mysqli->query("UPDATE form SET uitgevoerd_door_naam='$naam' WHERE id='$_POST[business_id]'");
}
mysqli_close($mysqli);
?>
答案 0 :(得分:0)
似乎查询中存在问题,因为&#39; $ _ POST [business_id]&#39;将假定为varchar,而id的数据类型必须在数据库中为int。请尝试以下;
"UPDATE form SET uitgevoerd_door_naam='$naam' WHERE id=addslashes(htmlentities($_POST[business_id]))"
也始终保证您的查询安全;)。
答案 1 :(得分:-1)
做几步,让我知道你得到了什么, 1.在edit.php中
if($_POST )
{
$naam = addslashes(htmlentities($_POST['uitgevoerd_door_naam']));
echo $query = "UPDATE form SET uitgevoerd_door_naam='$naam' WHERE id='$_POST[business_id]'";
$mysqli->query($query);
}
在js中: 其他 {
$.ajax({
type : 'POST',
url : 'edit.php',
data : test+businessid,
success : function(test)
{
alert(test);
alert('Company modified');
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
}
让我知道你的警觉。