我无法在 php mysql 查询中使用 time 函数找到该行是否已存在于 mysql db 中? [英] I am unable to find if the row already exists in mysql db using time function in php mysql query?

查看:54
本文介绍了我无法在 php mysql 查询中使用 time 函数找到该行是否已存在于 mysql db 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个模块,教师可以通过选择由教师提供的信息动态生成的下拉列表(选择框)来向学生发布出勤情况.当教师选择特定年份和部分时,受尊重的学生名单是检索并显示在表格中.但要求是一旦出勤被发布到特定课程/部分的特定课程/部分,教师将无法再次打开

我已经尝试使用 mysql_num_rows() 函数来检查在特定日期数据库中是否已经存在任何行.但它没有按照我想要的方式工作

这是我的模块的全部代码,不包括 db 文件

<br><table class="table table-bordered table-hover"><tr><th>S.no</th><th>学生姓名</th><第>卷数</th><th>现在</th><th>缺席</th></tr><?phpif (isset($_POST['search'])){$stu="学生";$yr=$_POST['年'];$se=$_POST['section'];$subdr=mysql_query("SELECT subject FROM schedule WHERE id='$cuid' AND day='$d' AND class='$yr' AND section='$se'");$subj=mysql_fetch_assoc($subdr);$dis_date=date("Y-m-d H:i:s");$subj_d=$subj['subject'];$display=mysql_query("select * from Attend_records where id='$cuid' AND ondate='".$dis_date."' And subject='$subj_d'");$rec=mysql_num_rows($display);如果($rec){echo "记录已发布";}别的{$display=mysql_query("select name,id from login where role='$stu' AND Academic='$yr' AND section='$se'");$sno=0;$count=0;而 ($row=mysql_fetch_array($display)) {$sno++;?><tr><td><?php echo $sno ?></td><td><?php echo $row['name'] ?><input type="hidden" name="name[]" value="<?php echo $row['name'] ?>"></td><td><?php echo $row['id'] ?><input type="hidden" name="id[]" value="<?php echo $row['id'] ?>"></td><td><input type="radio" name="attendance_status[<?php echo $count ?>]" value="Present" required></td><td><input type="radio" name="attendance_status[<?php echo $count ?>]" value="Absent" required></td></tr><?php$count++;}}?><tr><td colspan=5><center><label><?php echo "Subject : ".$subj['subject'];?></label></center></td></tr><input type="hidden" name="yr" value="<?php echo $_POST['year']; ?>"><input type="hidden" name="set" value="<?php echo $_POST['section']; ?>"><?php?><center><input type="submit" name="submit" value="Submit" class="btn btn-primary" ></中心>

预期的输出应该显示一条消息,说记录已发布"基于如下查询:

$dis_date=date("Y-m-d H:i:s");$subj_d=$subj['subject'];$display=mysql_query("select * from Attend_records where id='$cuid'AND ondate='".$dis_date."' And subject='$subj_d'");$rec=mysql_num_rows($display);如果($rec){echo "记录已发布";}别的{#显示学生列表}

解决方案

您的作业是:

$dis_date=date("Y-m-d H:i:s");

so $dis_date 包含日期和时间.仅当表中的记录具有完全相同的一天中的时间,而不仅仅是相同的日期时,查询才会匹配.

您应该将时间排除在变量之外:

$dis_date=date("Y-m-d");

如果表中列的数据类型是DATETIME,还需要从中过滤掉时间,用:

AND DATE(ondate)='$dis_date' And subject='$subj_d'"

如果数据类型是 DATE,则不需要这样做.

i am currently working on a module where faculty can post attendance to the students by selecting a dropdown(select box) which is generated dynamically by the information given by the faculty.When faculty selects a particular year and section respected student list is retrived and displayed in a table.But the requirement is once the attendance is posted to a particular class/section on a particular it cannot be opened again by the faculty

I have tried using mysql_num_rows() function to check if any rows are already present in the db or not on that particular date.But its not working the way i wanted

here is my entire code of the module excluding db file

<form action="take.php" method="Post">
<br>
<table class="table table-bordered table-hover ">
    <tr>
        <th>S.no</th>
        <th>Student Name</th>
        <th>Roll Number</th>
        <th>Present</th>
        <th>Absent</th>
    </tr>
    <?php 
        if (isset($_POST['search'])) 
            { 
            $stu="Student";
            $yr=$_POST['year']; 
            $se=$_POST['section'];                             

            $subdr=mysql_query("SELECT subject FROM schedule WHERE id='$cuid' AND day='$d' AND class='$yr' AND section='$se'");
            $subj=mysql_fetch_assoc($subdr);
                $dis_date=date("Y-m-d H:i:s");
                $subj_d=$subj['subject'];
                $display=mysql_query("select * from attendance_records where id='$cuid' AND ondate='".$dis_date."' And subject='$subj_d'");
                $rec=mysql_num_rows($display);
                if($rec){
                echo "Records posted";
            }
            else{
                $display=mysql_query("select name,id from login where role='$stu' AND academic='$yr' AND section='$se'");

                $sno=0;
                $count=0;

                while ($row=mysql_fetch_array($display)) {
        $sno++;
        ?>
    <tr>
        <td><?php echo $sno  ?></td>
        <td>
            <?php echo $row['name']  ?>
            <input type="hidden" name="name[]" value="<?php echo $row['name']  ?>">
        </td>
        <td>
            <?php echo $row['id']  ?>
            <input type="hidden" name="id[]" value="<?php echo $row['id']  ?>">
        </td>
        <td>
            <input type="radio" name="attendance_status[<?php echo $count ?>]" value="Present" required>
        </td>
        <td>
            <input type="radio" name="attendance_status[<?php echo $count ?>]" value="Absent" required>
        </td>
    </tr>
    <?php 
        $count++;
        }
        }
        ?>
    <tr>
        <td colspan=5>
            <center><label><?php echo "Subject : ".$subj['subject']; ?></label></center>
        </td>
    </tr>
    <input type="hidden" name="yr" value="<?php echo $_POST['year']; ?>">
    <input type="hidden" name="set" value="<?php echo $_POST['section']; ?>">
    <?php 
        } ?>
</table>
<center><input   type="submit" name="submit" value="Submit" class="btn btn-primary" >
</center>
</div>

the expected output should display a message saying "Records posted" based on query like :

$dis_date=date("Y-m-d H:i:s");
$subj_d=$subj['subject'];
$display=mysql_query("select * from attendance_records where id='$cuid' 
   AND ondate='".$dis_date."' And subject='$subj_d'");
$rec=mysql_num_rows($display);
if($rec){
 echo "Records posted";
 }
 else{
  #display the student list
 }

解决方案

Your assignment is:

$dis_date=date("Y-m-d H:i:s");

so $dis_date contains both a date and a time of day. The query will only match if the records in the table have the exact same time of day, not just the same date.

You should leave the time out of the variable:

$dis_date=date("Y-m-d");

If the datatype of the column in the table is DATETIME, you also need to filter out the time from that, with:

AND DATE(ondate)='$dis_date' And subject='$subj_d'"

You don't need to do this if the datatype is DATE.

这篇关于我无法在 php mysql 查询中使用 time 函数找到该行是否已存在于 mysql db 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆