我为学生的学校网站创建了一个简单的标记系统。现在标签系统工作得很好,现在我还必须将标签与相应的文章 ID 保存在通知表中,以便稍后通知学生他们被标记在哪篇文章中,即使我设法做到了。但是现在,如果您偶然想删除标签,并且在键入文章时意识到不需要标记该人,那么第一个放置标签也会在数据库中更新。
//ajax代码(attach.php)
<?php
include('config.php');
if(isset($_POST))
{
$u=$_POST['v'];
mysql_query("INSERT INTO `notify` (`not_e`) VALUES ('$u')");
}
?>
//标签系统js代码
<script type="text/javascript">
var id = '<?php echo $id ?>';
$(document).ready(function()
{
var start=/%/ig;
var word=/%(\w+)/ig;
$("#story").live("keyup",function()
{
var content=$(this).text();
var go= content.match(start);
var name= content.match(word);
var dataString = 'searchword='+ name;
if(go.length>0)
{
$("#msgbox").slideDown('show');
$("#display").slideUp('show');
$("#msgbox").html("Type the name of someone or something...");
if(name.length>0)
{
$.ajax({
type: "POST",
url: "boxsearch.php",
data: dataString,
cache: false,
success: function(html)
{
$("#msgbox").hide();
$("#display").html(html).show();
}
});
}
}
return false();
});
$(".addname").live("click",function()
{
var username=$(this).attr('title');
$.ajax({
type: "POST",
url: "attach.php",
data: {'v': username},
});
var old=$("#story").html();
var content=old.replace(word,"");
$("#story").html(content);
var E="<a class='blue' contenteditable='false' href='profile2.php?id="+username+"'>"+username+"</a>";
$("#story").append(E);
$("#display").hide();
$("#msgbox").hide();
$("#story").focus();
});
});
</script>
最佳答案
看起来您的问题出现在 php 代码中的 if 语句上: 即使 $_POST['v'] 为空并且 sql 仍然被执行。
引用另一个线程:
” 使用 !empty 而不是 isset。 isset 对 $_POST 返回 true,因为 $_POST 数组是超全局的并且始终存在(设置)。
或者更好地使用 $_SERVER['REQUEST_METHOD'] == 'POST' ”
或者在我看来。 只需放入
if ($_POST['v']){
//sql query
}
希望有帮助;)
关于javascript - 即使字段为空也会执行mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348438/