一个按钮正常工作,另一个按钮给出错误消息:“注意:未定义的索引:第6行上的dbconfig.php”

时间:2019-04-13 10:35:52

标签: php html

我的页面上有两个按钮“是”和“否”。当我单击“是”按钮时,一切正常,但是当我单击“否”按钮时,出现错误。

但是,当我收到此错误时,数据库仍在更新,但我想要的是它返回后两页,就像下面代码中的“是”按钮一样。

edit.php

<form action="dbconfig.php" method="post">
<select name="studentId" id="" class="form-control">
    <option value="">Select Student</option>
    <option value="35290">student1</option>
    <option value="35316">student2</option>
    <option value="35294">student3</option>
    <option value="35377">student4</option>
</select>

    <select name="createdAt" id="" class="form-control">
                <option value="">Select Time</option>
                <option value="2019-04-02 11:00:00">11:00</option>
                <option value="2019-04-02 12:00:00">12:00</option>
                <option value="2019-04-02 13:00:00">13:00</option>
                <option value="2019-04-02 15:00:00">15:00</option>
    </select>       
<p>
Did the student attend class?
<button name="attended" type="submit" value="1" >Yes</button>
<button name="attended2" type="submit" value="0" >No</button>

dbconfig.php

    <?php

    $con=mysqli_connect("localhost","root","","billy") or die ("Database                                           
           not selected");

    $studentId=$_POST['studentId'];
    $attended=$_POST['attended'];
    $createdAt=$_POST['createdAt'];


    if(isset($_POST['attended']))
      {
        $query=mysqli_query($con,"insert into attendance (studentId, attended, 
            createdAt) values ('$studentId','$attended','$createdAt')") or die                             
            ("Error");
        echo '<script type="text/javascript">'
                   , 'history.go(-2);'
                   , '</script>';
      }
    else (isset($POST['attended2'])) 
      {
           $query=mysqli_query($con,"DELETE FROM attendance WHERE 
                studentId='$studentId' AND createdAt='$createdAt'") or die ("Error")       
      }
    ?>

3 个答案:

答案 0 :(得分:4)

我认为,在“是”按钮和“否”按钮中单击检查不正确,您使用

if (logic){
runs if logic is true;
}else{
runs if logic is wrong;
}

尝试这个

  if(isset($_POST['attended'])){
     $query=mysqli_query($con,"insert into attendance (studentId, attended, 
     createdAt) values ('$studentId','$attended','$createdAt')") or die                             
     ("Error");
     echo '<script type="text/javascript">'
           , 'history.go(-2);'
           , '</script>';}

  if (isset($POST['attended2'])) 
      {
       $query=mysqli_query($con,"DELETE FROM attendance WHERE 
    studentId='$studentId' AND createdAt='$createdAt'") or die ("Error")       
      }

答案 1 :(得分:2)

您不需要if-else条件,只需对每个按钮使用IF条件。 例子

IUserRepository _userRepository => Getit
//If there are entities according to your conditions, this will return them, then use it
_userRepository.Query(u => u.Id == userId);

答案 2 :(得分:1)

只需将代码从第6行($attended=$_POST['attended'];的内部移至If条件if(isset($_POST['attended']))的真实情况下,并使用简单else的elseif