I am unable to find if the row already exists in mysql db using time function in php mysql query?

有些话、适合烂在心里 提交于 2019-12-12 06:47:32

问题


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
 }

回答1:


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.



来源:https://stackoverflow.com/questions/54699148/i-am-unable-to-find-if-the-row-already-exists-in-mysql-db-using-time-function-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!