单击“提交”按钮后显示错误

时间:2018-03-08 01:59:41

标签: html mysql phpmyadmin

单击“taskupdate.php”上的“提交”按钮后显示错误,它显示错误并将“任务”值和“日期”删除为0

错误: 注意:未定义的索引:第4行的C:\ xampp \ htdocs \ assigment \ taskupdate.php中的id

警告:mysqli_error()期望在第10行的C:\ xampp \ htdocs \ assigment \ taskupdate.php中给出1个参数,0

“list.php的”

<?php
session_start();
if(!empty($_SESSION['username'])){
    echo "<h1> Welcome " . $_SESSION['username'] , "!</h1>";
    echo "<a href='logout.php'><h1>Logout</h1></a>";
}else{
    header ("Location:login.php");
}

?>
<?php 
    // initialize errors variable
    $errors = "";

    // connect to database
    $db = mysqli_connect("localhost", "root", "", "atodolist");

    // insert a quote if submit button is clicked
    If(isset($_POST['submit'])){

        If(!empty($_POST['task']) && !empty($_POST['date'])){ //this check if both task and date got data
            $task = $_POST['task'];
            $date = $_POST['date'];
            $sql = "INSERT INTO tasks (task,date) VALUES ('$task','$date')";
            mysqli_query($db, $sql);
            header('location: list.php');

         }else{

                If(empty($_POST['task'])){

                            $errors = "You must fill in the task";

                  }else If(empty($_POST['date'])){

                           $errors = "You must fill in the date";

                   }
        }
    }


    /*if (isset($_POST['submit'])) {
        if (empty($_POST['task'])) {
            $errors = "You must fill in the task";
        }else{
            $tasks = $_POST['task'];
            $sql = "INSERT INTO tasks (task) VALUES ('$tasks')";
            mysqli_query($db, $sql);
            header('location: list.php');
        }
    }   
     {
        if (empty($_POST['date'])) {
            $errors = "You must fill in the date";
        }else{
            $tasks = $_POST['date'];
            $sql = "INSERT INTO tasks (date) VALUES ('$tasks')";
            mysqli_query($db, $sql);
            header('location: list.php');
        }
        }*/

if (isset($_GET['del_task'])) {
    $id = $_GET['del_task'];

    mysqli_query($db, "DELETE FROM tasks WHERE id=".$id);
    header('location: list.php');
}   

?>

<!DOCTYPE html>
<html>
<head>
    <title>ToDo List Application PHP and MySQL</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>

    <div class="heading">
        <h2 style="font-style: 'Hervetica';">To Do List</h2>
    </div>
    <form method="post" action="list.php" class="input_form">
    <?php if (isset($errors)) { ?>
    <p><?php echo $errors; ?></p>
<?php } ?>
        <input type="text" placeholder="Insert task here" name="task" class="task_input">
        <input type="date"  name="date" class="date_input">
        <button type="submit" name="submit" id="add_btn" class="add_btn">Add Task</button>
    </form>
</form>

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Tasks</th>
            <th>Date</th>
            <th style="width: 60px;">Action</th>
        </tr>
    </thead>

    <tbody>
        <?php 
        // select all tasks if page is visited or refreshed
        $tasks = mysqli_query($db, "SELECT * FROM tasks");
        $date = mysqli_query($db, "SELECT * FROM date");
        $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
            <tr>
                <td> <?php echo $i; ?> </td>
                <td class="task"> <?php echo $row['task']; ?> </td>
                <td class="date"><?php echo $row['date']; ?>
                <td class="delete"> 

                    <a href="list.php?del_task=<?php echo $row['id'] ?>"><img src='delete.png' class='button' /> 
                    <a href="taskupdate.php?id=<?php echo $row['id']  ?>"><img src='update.png' class='button' /> 
                </td>
            </tr>
        <?php $i++; } ?>    
    </tbody>
</table>
</body>
</html>

“taskupdate.php”

<?php
require('mysqlconsetting.php');
$taskErr = $dateErr = '';
$id = $_REQUEST['id'];

$sql_select = "Select * from tasks where id=".$id.";";
//echo $sql_select;
//echo $id;

$result = mysqli_query($conn, $sql_select) or die (mysqli_error());

$row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"  >

<label for="task">Task: </label>
<input type="text" name="username" value="<?php echo $row['task']; ?>" required placeholder="Please key in your task" size="50"   /><?php echo "*" . $taskErr; ?><br/>


<label for="date">Date: <label>
<input type="date" name="email" value="<?php echo $row['date']; ?>"  size="50" /><?php echo "*" . $dateErr; ?><br/>

<input type="submit" name="submit" value="Update" />
</form>
</body>

</html>
<?php
if(isset($_POST['submit'])){
    $task = $_POST['task'];
    $date = $_POST['date'];

    $sql_update = "update tasks set task = '$task' , date='$date' where id='$id'";

    if(mysqli_query($conn,$sql_update)){
        echo "Updated successfully!";
        header("Location: taskupdate.php?msg=Record update successfully");
    }else{
        echo "Error updating record".mysqli_error($conn);
    }
}
require('dtclose.php');
?>

1 个答案:

答案 0 :(得分:0)

第一个是因为您没有任何名为id的表单元素或查询字符串(如<input name="id">) - &gt;点击taskupdate时$id = $_REQUEST['id'];

第二个可能是评论中的副本。