我想扩展我的PHP技能,所以我通读了tutorialzine教程。我理解本教程中提供的说明。但是当谈到扩展它时,我似乎缺乏连接。我的主要目标是在单击标签时简单地删除选定的笔记。不过,我不知道如何选择分配给笔记的ID,以便能够将它传递给我的删除功能。如何在点击链接时删除数据库条目
来源:http://tutorialzine.com/2010/01/sticky-notes-ajax-php-jquery/
感谢您的帮助。
<?php
error_reporting(E_ALL^E_NOTICE);
require 'connect.php';
mysql_query("DELETE FROM notes WHERE id>3 AND dt<SUBTIME(NOW(),'0 1:0:0')");
$query = mysql_query("SELECT * FROM notes ORDER BY id DESC");
$notes = '';
$left='';
$top='';
$zindex='';
while($row=mysql_fetch_assoc($query)){
list($left,$top,$zindex) = explode('x',$row['xyz']);
$notes.= '
<div class="note '.$row['color'].'" style="left:'.$left.'px;top:'.$top.'px;z-index:'.$zindex.'">
'.htmlspecialchars($row['text']).'
<div class="author">'.htmlspecialchars($row['name']).'</div>
<span class="data">'.$row['id'].'</span>
<a id="remove_note" href="javascript:;" onclick="deleteNote('<? echo $row['id']; ?>');"> </a>
</div>';
}
function deleteNote(id){
$sql="DELETE FROM notes WHERE id='$rows['id']'";
$result=mysql_query($sql) or die("Error when tryin to delete note.");
}
?>
更新:
我一直在玩这个周围,既安德鲁和sachleen所提供的答案。由于您提到了整个SQL注入问题,因此不适合使用AJAX替代方案。但我仍然有问题将ID传递给remove.php文件。我相信这与$ note是如何从数据库创建信息有关。
我这样说是因为我得到:解析错误:语法错误,意想不到的T_STRING在/home/avonamyd/public_html/projects_php/sticky_notes/demo.php上线24
而这仅仅是当我有代码来自sachleen。但是,当我更新它以解释单引号时,我有以下代码。该id存在并传递给remove.php文件,但我仍然收到错误。这是当我使用我的代码或你提供的。
$notes.= '
<div class="note '.$row['color'].'" style="left:'.$left.'px;top:'.$top.'px;z-index:'.$zindex.'">
'.htmlspecialchars($row['text']).'
<div class="author">'.htmlspecialchars($row['name']).'</div>
<span class="data">'.$row['id'].'</span>
<a id="remove_note" target="_blank" href="remove.php?id='.$row['id'].'"> </a>
</div>';
下面是我目前在我的remove.php文件:
<?php
include 'connect.php';
$_GET['id'];
function deleteNote($id){
$sql="DELETE FROM notes WHERE id='$id'";
}
$result=mysql_query($sql) or die("Error when tryin to delete note.");
>
更新
我在另一回声线增加整个remove.php,这就是我想要的。
<?php
include 'connect.php';
$_GET['id'];
echo $id; --doesnt show
function deleteNote($id){
echo "hello"; --doesnt show
$sql="SELECT FROM notes WHERE id='$id'";
}
echo 'hello2'; --shows
$result=mysql_query($sql) or die("Error when tryin to delete note.");
>
更新: 谢谢大家的这个项目,我终于得到了概念点击在我的脑海一些修修补补后,周围的帮助。我将发布下面的功能代码,以供任何其他人发现此代码。 = D Thx Everyone!
demo.php
error_reporting(E_ALL^E_NOTICE);
require 'connect.php';
mysql_query("DELETE FROM notes WHERE id>3 AND dt<SUBTIME(NOW(),'0 1:0:0')");
$query = mysql_query("SELECT * FROM notes ORDER BY id DESC");
$notes = '';
$left='';
$top='';
$zindex='';
while($row=mysql_fetch_assoc($query)){
list($left,$top,$zindex) = explode('x',$row['xyz']);
$id = $row['id'];
$notes.= '
<div class="note '.$row['color'].'" style="left:'.$left.'px;top:'.$top.'px;z-index:'.$zindex.'">
'.htmlspecialchars($row['text']).'
<div class="author">'.htmlspecialchars($row['name']).'</div>
<span class="data">'.$row['id'].'</span>
<a id="remove_note" target="_blank" href="remove.php?id='.$row['id'].'"> </a>
</div>';
}
remove.php
<?php
include 'connect.php';
$id = intval($_GET['id']);
$sql="DELETE FROM notes WHERE id=$id";
$result = mysql_query($sql) or die("Unable to delete database entry.");
?>
在代码的更新版本中,您有'deleteNote'函数,但它不会被调用。此外,您正尝试使用您尚未分配的变量“$ id”。我会说你应该摆脱deleteNote函数并分配$ id = $ _GET ['id']'请记住,_ajax不会保护你免受sql injection_ - 你仍然需要清理id传入。 – Andrew