我正在尝试使用AJAX和PHP发送、插入和删除sql查询。我已经检查了开发人员工具的网络选项卡,我可以看到当我单击输入时,它会成功地检索值,所以我遇到的主要问题是将这些值插入到数据库中以及从数据库中删除这些值。
编辑:没有返回关于我的SQL查询的错误消息,所以我不确定发生了什么。
下面是我的HTML和JQUERY代码:
$(document).ready(function() { $("#favbut").click(function() { var username = $("#usernamefav").val(); var lid = $("#lidfav").val(); var favourite = $("#favouritefav").val(); $.ajax({ url: "fave.php", type: "post", dataType: "json", data: { username: username, lid: lid, favourite: favourite $("#unfavbut").click(function() { var username = $("#usernameunfav").val(); var lid = $("#lidunfav").val(); var favourite = $("#favouriteunfav").val(); $.ajax({ url: "unfave.php", type: "post", dataType: "json", data: { username: username, lid: lid, favourite: favourite });
<input type="hidden" name="username" id="usernamefav" value='.$user.'> <input type="hidden" name="lid" id="lidfav" value='.$lid.'> <input type="hidden" name="favourite" id="favouritefav" value=YES> <input class="favButt fav" id="favbut" type="submit" name="faveBTN" value="Fave">'; <input type="hidden" name="username" id="usernameunfav" value='.$user.'> <input type="hidden" name="lid" id="lidunfav" value='.$lid.'> <input type="hidden" name="favourite" id="favouriteunfav" value=YES> <input class="favButt unfav" id="unfavbut" type="submit" name="unfaveBTN" value="unFave">';
下面是php文件:
<?php if(isset($_POST['favbut'])){ include 'includes/dbh.inc.php'; $username = $_POST['username']; $lid = $_POST['lid']; $favourite = $_POST['favourite']; $sql = "INSERT INTO userslocation (fid,username,lid,favourite) VALUES ('','".$username."', '".$lid."','".$favourite."')"; $result = (mysqli_query($conn, $sql)); if(isset($_POST['unfavbut'])){ include 'includes/dbh.inc.php'; $username = $_POST['username']; $lid = $_POST['lid']; $favourite = $_POST['favourite']; $sql = "DELETE FROM userslocation WHERE username='$username' AND lid='$lid'"; $result = (mysqli_query($conn, $sql)); ?>
任何关于哪里出了问题的帮助都是值得感谢的!
发布于 2019-02-16 09:50:24
实际上,您的查询看起来不错。但是@RonCajan说对了可能的SQL注入。
1)我认为你在这里有错误:
if(isset($_POST['favbut'])) { ...