有没有办法通过 PHP 删除笔记而无需为其分配 ID?
Is there a way to DELETE notes by PHP without having their ID's assigned with them?
我正在做一个网络应用练习,注册用户可以在其中登录并创建笔记。现在我想添加一个功能,如果我单击 "X" 我将要添加到他们身边,它会删除某个注释,但我无法真正弄清楚如何识别创建的某个注释通过这种方法,更准确地说,我如何 return 它的 id 用于删除查询。
这是您可以查看它现在的样子的网站,下面我将附上我列出评论的方式。提前致谢!
http://laszlovaszi.com/mx_agency/index.php
<?php
function confirm_query($result) {
if (!$result) {
die("Database query failed.");
}
}
function find_notes_by_id($user_id) {
global $connection;
$row = array();
$safe_user_id = mysqli_real_escape_string($connection, $user_id);
$query = 'SELECT content ';
$query .= 'FROM notes ';
$query .= 'WHERE user_id = '.$safe_user_id;
$result = mysqli_query($connection, $query);
confirm_query($result);
$final_data = array();
while($row = mysqli_fetch_assoc($result)){ // iterates over the result-set object to get all data
$final_data[] = $row; //assigns value to the array
}
return $final_data;
}
?>
<div class="notes text-center">
<?php
$notes_set = find_notes_by_id($userRow['id']);
if(empty($notes_set)){
echo "No notes have been posted for this user yet.";
} else {
echo "<div class=\"notelist text-left\">";
foreach($notes_set as $note){
echo "<div class=\"note text-left\">";
echo "● ";
echo htmlentities($note['content']);
echo "<br />";
echo "</div>";
}
echo "</div>";
}
?>
</div>
Now I'd like to add a function, that deletes a certain note if I click
the "X" I'm about to add to their side
使 "X" 成为 link 到 javascript 函数,该函数获取笔记 ID 并向服务器发出 ajax 请求以删除笔记。当您在 foreach($notes_set as $note)
步骤中呈现每个音符时,id 将被放置在函数调用中。 link 可以这样写:
echo "<a onclick='deleteNote($note[id])'> X </a>";
当用户按下"X"时,deleteNote()
函数将执行。此函数将向 delete.php?note=123
发出 AJAX 请求(123 是 PHP 输入的 $note['id']
)。
这里是原始格式的示例 Javascript。如果使用JQuery这样的框架,那就更简单了:
<script>
function deleteNote(noteID) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//remove the note from the DOM and alert user of success
}
};
xhttp.open("GET", "delete.php?note="+noteID, true);
xhttp.send();
}
</script>
在 PHP 中,您可以使用 $id = $_GET['note']
检索笔记 ID。然后可以在您的 DELETE
查询中使用该值。
请记住在删除之前先验证以下内容:
- 用户已通过身份验证
- 笔记 id 确实属于该用户
否则,任何人都可以删除所有人的笔记。
我正在做一个网络应用练习,注册用户可以在其中登录并创建笔记。现在我想添加一个功能,如果我单击 "X" 我将要添加到他们身边,它会删除某个注释,但我无法真正弄清楚如何识别创建的某个注释通过这种方法,更准确地说,我如何 return 它的 id 用于删除查询。 这是您可以查看它现在的样子的网站,下面我将附上我列出评论的方式。提前致谢!
http://laszlovaszi.com/mx_agency/index.php
<?php
function confirm_query($result) {
if (!$result) {
die("Database query failed.");
}
}
function find_notes_by_id($user_id) {
global $connection;
$row = array();
$safe_user_id = mysqli_real_escape_string($connection, $user_id);
$query = 'SELECT content ';
$query .= 'FROM notes ';
$query .= 'WHERE user_id = '.$safe_user_id;
$result = mysqli_query($connection, $query);
confirm_query($result);
$final_data = array();
while($row = mysqli_fetch_assoc($result)){ // iterates over the result-set object to get all data
$final_data[] = $row; //assigns value to the array
}
return $final_data;
}
?>
<div class="notes text-center">
<?php
$notes_set = find_notes_by_id($userRow['id']);
if(empty($notes_set)){
echo "No notes have been posted for this user yet.";
} else {
echo "<div class=\"notelist text-left\">";
foreach($notes_set as $note){
echo "<div class=\"note text-left\">";
echo "● ";
echo htmlentities($note['content']);
echo "<br />";
echo "</div>";
}
echo "</div>";
}
?>
</div>
Now I'd like to add a function, that deletes a certain note if I click the "X" I'm about to add to their side
使 "X" 成为 link 到 javascript 函数,该函数获取笔记 ID 并向服务器发出 ajax 请求以删除笔记。当您在 foreach($notes_set as $note)
步骤中呈现每个音符时,id 将被放置在函数调用中。 link 可以这样写:
echo "<a onclick='deleteNote($note[id])'> X </a>";
当用户按下"X"时,deleteNote()
函数将执行。此函数将向 delete.php?note=123
发出 AJAX 请求(123 是 PHP 输入的 $note['id']
)。
这里是原始格式的示例 Javascript。如果使用JQuery这样的框架,那就更简单了:
<script>
function deleteNote(noteID) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//remove the note from the DOM and alert user of success
}
};
xhttp.open("GET", "delete.php?note="+noteID, true);
xhttp.send();
}
</script>
在 PHP 中,您可以使用 $id = $_GET['note']
检索笔记 ID。然后可以在您的 DELETE
查询中使用该值。
请记住在删除之前先验证以下内容:
- 用户已通过身份验证
- 笔记 id 确实属于该用户
否则,任何人都可以删除所有人的笔记。