【问题标题】:Student which mark his/her attendance once can't mark it again when mark attendance button is pressed考勤一次的学生按考勤键后不能再考勤
【发布时间】:2021-08-01 05:29:11
【问题描述】:

我的目标:一旦按下按钮,就无法再次按下。它应该给出一个错误或某种消息,例如(当第一次按下按钮以标记出勤并且有人再次按下它时)

"已标记出勤,您不能再次标记您的出勤"

 <?php 
 session_start();

 ?>
 <form method="post">
 <button name="attendence" >mark attendence  </button>
 </form>


<?php

if (isset($_POST['attendence'])){
    $id =$_SESSION["id"];
    $con = mysqli_connect('localhost','root','','yo');
    $query = "INSERT INTO attendance (present,absent, datetime, std_id) VALUES ('present','',current_timestamp(), $id ) ";
    $rs=mysqli_query($con,$query);

    if($rs){
        echo "Marked as Present";
    }
    else {
        echo "marked as Absent";
    }
    
}

【问题讨论】:

  • 您的信息将在数据库中。如果考勤被标记,那么执行查询以检查它是否被标记应该返回一行。如果返回一行,拒绝提交并显示您的消息。当您检测到表单已被标记时,您也根本无法呈现表单。
  • @El_Vanja 你能否更具体地了解代码而不是理论。如果您向我展示代码甚至代码的想法..我将非常感谢您。这对我理解场景有很大帮助。谢谢
  • 那么,您知道如何编写 SELECT 查询吗?这应该是你的第一步
  • 附言。另外,这段代码{ echo "Marked as Present"; } else { echo "marked as Absent"; } 没有逻辑意义。如果 $rs 为 false,则仅表示未插入任何行,并不表示此人被标记为缺席。事实上,您的代码只允许将它们标记为存在。目前,您无法标记某人缺席。您打算稍后添加吗?此外,您不需要单独的数据库列来表示存在和不存在。可以接受存在或不存在作为值的单个“状态”列将是更好的设计。
  • 您的SQL查询容易受到代码注入,请查看stackoverflow.com/questions/60174/…

标签: php button html5-formvalidation


【解决方案1】:

如果学生已经被此代码标记为存在,您必须在数据库中搜索:

$id =$_SESSION["id"];
$query = "SELECT * FROM attendance WHERE std_id = '$id' ) ";
$result=mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) { 
    if ($row['present'] == 'present') {
        echo' Student already marked as present';
    }
    else {
        echo'
        <form method="post">
             <button name="attendence" >mark attendence  </button>
        </form>';
    }
}

【讨论】:

  • 您的SQL查询容易受到代码注入,请查看stackoverflow.com/questions/60174/…
  • $id 的值是 $_SESSION 值,而不是 $_GET 或 $_POST 值。 PDO 是最好的解决方案,但在这种情况下创建代码注入非常困难,因为不可能将外部值分配给 $id 变量
【解决方案2】:

首先:如果学生已经被标记/存在,则在数据库中搜索。如果他/她在场,请回显“写一些东西”。 然后:插入查询以标记存在

<?php

if (isset($_POST['attendence'])){
    $id =$_SESSION["id"];
    $con = mysqli_connect('localhost','root','','yo');
    

    $mark_query = "SELECT * FROM `attendance` WHERE date=CURRENT_DATE and std_id=$id ";

    $result = mysqli_query($con, $mark_query);
    $row = mysqli_fetch_assoc($result);
        
    if ($row['present']=='present') {
        echo "Student already marked ".$row['present'];
    }
        
    else{
        $query = "INSERT INTO attendance (present,absent, datetime, std_id,date,marked_status) VALUES ('present','',current_timestamp(), $id, current_timestamp(),'marked' ) ";
        $rs=mysqli_query($con,$query);

        if($rs){
            echo "Marked as Present";
        }
            

    }
    
}   

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    相关资源
    最近更新 更多