如何将本地PHP代码转换为CodeIgniter

tnkciper  于 2022-12-07  发布在  PHP
关注(0)|答案(1)|浏览(143)

这是我正在引用的。它是一个原生PHP代码,我想把它转换成CodeIgniter。

<?php
    session_start();
    $server = "localhost";
    $username="root";
    $password="";
    $dbname="attendancedb";

    $conn = new mysqli($server,$username,$password,$dbname);

    if($conn->connect_error){
        die("Connection failed" .$conn->connect_error);
    }

    if(isset($_POST['studentID'])){
        
        $studentID =$_POST['studentID'];
        $date = date('Y-m-d');
        $time = date('H:i:s A');

        $sql = "SELECT * FROM vehicle_info WHERE v_id = '$studentID'";
        $query = $conn->query($sql);

        if($query->num_rows < 1){
            $_SESSION['error'] = 'Cannot find QRCode number '.$studentID;
        } else {
             $row = $query->fetch_assoc();
             $id = $row['STUDENTID'];
             $sql ="SELECT * FROM attendance WHERE STUDENTID='$id' AND LOGDATE='$date' AND STATUS='0'";
             $query=$conn->query($sql);
             if($query->num_rows>0){
                $sql = "UPDATE attendance SET TIMEOUT='$time', STATUS='1' WHERE STUDENTID='$studentID' AND LOGDATE='$date'";
                $query=$conn->query($sql);
                $_SESSION['success'] = 'Successfuly Time Out: '.$row['FIRSTNAME'].' '.$row['LASTNAME'];
             } else {
                $sql = "INSERT INTO attendance(STUDENTID,TIMEIN,LOGDATE,STATUS) VALUES('$studentID','$time','$date','0')";
                if($conn->query($sql) ===TRUE){
                   $_SESSION['success'] = 'Successfuly Time In: '.$row['FIRSTNAME'].' '.$row['LASTNAME'];
                } else {
                   $_SESSION['error'] = $conn->error;
                }    
             }
        }
    } else {
        $_SESSION['error'] = 'Please scan your QR Code number';
    }
header("location: index.php");
       
$conn->close();
?>

这就是我到目前为止所得到的。但是它并没有更新状态为'0'的列。它只是创建了另一条记录。
在我的控制器中:

public function save() {
    $v_id = $this->input->post('v_id');     
    $date = date('Y-m-d');
    $time = date('h:i:s A');
    //check if record exist
    $this->form_validation->set_rules('v_id','Plate Number','trim|callback_get_record');
    //if record not found
    if($this->form_validation->run()==FALSE){
        $ssdata = (object) array(
                  'V_ID'=> $v_id,
                  'TIMEOUT'=> $time,
                  'LOGDATE'=> $date,
                  'STATUS'=> 1);   
        //show error
        $this->session->set_flashdata('error', 'Cannot find QRCode number '.$v_id);
        redirect('attendance/create');
      } else {
        //check if already timein in   
        
        $signin = $this->attendance_model->checktimein($v_id,$date);
        $row = $signin->row_array();    
        $id = $row['V_ID'];     

        //if timein found  
        if($signin==TRUE){ 
              
            $this->attendance_model->update($ssdata);
            $this->session->set_flashdata('success', 'Successfully Time OUT '.$v_id);
            redirect('attendance/create');
        } else {
            $this->attendance_model->save(array('V_ID'=>$v_id,'TIMEIN' => $time,'LOGDATE' => $date,'STATUS' => 0));
            $this->session->set_flashdata('success', 'Successfully Time IN '.$v_id);
            redirect('attendance/create');
        }
    }
}

public function get_record($v_id){ 
    $row = $this->db->where('v_id',$v_id)->get('vehicle_info')->num_rows(); 
    if($row == 1){  
       return TRUE;
    } else { 
       return FALSE;     
    } 
}

在模型中:

public function save($data){
    $this->db->where('V_ID',$data['v_id']);     
    $this->db->insert('attendance',$data);
} 

function update($data){
    return $this->db->set('TIMEOUT',$time)
         ->set('STATUS',1)->where('V_ID',$data['v_id'])
         ->where('LOGDATE',$date)->update('attendance');
}

public function checktimein($v_id,$date){ 
    $row = $this->db->where('V_ID',$v_id)
         ->where('LOGDATE',$date)->where('STATUS',0)
         ->get('attendance')->num_rows(); 
    if($row > 0){  
        return TRUE;            
    } else { 
        return FALSE;            
    } 
}

即使检测到用户有时间进入,它也只保存新记录。
但它没有更新STATUS为“0”的列。它只是创建另一条记录。

qrjkbowd

qrjkbowd1#

$this->db->update()

产生更新字串,并根据您提供的数据执行查询。您可以将数组或对象传递给函数。以下是使用数组的范例:

$data = array(
        'title' => $title,
        'name' => $name,
        'date' => $date
);

$this->db->where('id', $id);
$this->db->update('mytable', $data);

相关问题