我尝试为用户制作待办事项列表。在下面的代码中,希望添加按钮工作正常并调用'add_wish()'函数,但'删除'/'更新'按钮不调用相关函数。而是调用文件''。尝试搜索论坛几个小时。为什么会发生这种情况,如何使用从数据库中提取数据的approch来调用相关函数。我的代码:
<body>
<?php
global $id, $adatabase;
include 'connect_to_database.php';
$adatabase = new database('localhost','root','');
function delete_wish($post_id) {
echo 'BLAH BLAH BLAH';
if(!empty($new_todo)) {
global $id,$adatabase;
$query = "DELTE FROM todolist WHERE post_id = '".$post_id."''";
if ($query_run = mysqli_query($adatabase->mysqli_link,$query)) {
get_user_todos($id);
} else 'Failed to add new wish.Please try again later!';
}
}
function update_wish($post_id) {
echo 'OKA';
}
function get_user_todos($user_id) {
global $id, $adatabase;
$id= $user_id;
echo $user_id;
$query = "SELECT todo,post_id FROM todolist WHERE user_id = '".$user_id."'";
if ($query_run = mysqli_query($adatabase->mysqli_link,$query)) {
?>
<div id="todo_list">
<table border="10" cellpadding="10">
<?php
while($query_data = $query_run->fetch_row()) {
echo '<tr>';
echo '<td>'.$query_data[0].'</td>';
echo "<td> <form method='post' action=\"<?php update_wish(".$query_data[1].");?>\"> <input type='submit' value='update'> </form> </td>";
echo "<td> <form method='post' action=\"<?php delete_wish(".$query_data[1].");?>\"> <input type='submit' value='delete'> </form> </td>";
echo '</tr>';
}
?>
</table>
</div>
<?php
}
}
function add_wish() {
global $id;
if(isset($_POST['new_todo'])) {
$new_todo = $_POST['new_todo'];
if(!empty($new_todo)) {
global $id,$adatabase;
$query = "INSERT INTO todolist (todo,user_id) VALUE ('".mysqli_real_escape_string($adatabase->mysqli_link,$new_todo)."','".$id."')";
if ($query_run = mysqli_query($adatabase->mysqli_link,$query)) {
get_user_todos($id);
} else 'Failed to add new wish.Please try again later!';
}
}
}
session_start();
if(isset($_SESSION['user_id'])) {
get_user_todos($_SESSION['user_id']);
?>
<form method="post" action=" <?php add_wish(); ?>">
<textarea rows="4" cols="8" name="new_todo"></textarea>
<input type="submit">
</form>
<?php
}else
header('Location: login.php');
?>
</body>