javascript - 当一家商店添加日期和时间也更改为所有其他商店时出现问题

标签 javascript php jquery html mysql

当我尝试添加产品的交货日期时,我在 php 面板中遇到问题,它也会在所有其他用户中发生变化,我不知道这是数据库还是 php 中的错误,我将在下面留下一些引用挤出物。

文件:Time_model.php

<?php
class Time_model extends CI_Model{
        function get_time_slot(){
           $q = $this->db->query("Select * from time_slots limit 1");
            return $q->row();
        }
        
        function get_closing_date($date,$store_id){
           $q = $this->db->query("Select * from closing_hours WHERE store_id=".$store_id." AND date >= '".date("Y-m-d",strtotime($date))."'");
            return $q->result(); 
        }
        function get_closing_hours($date){
           $q = $this->db->query("Select * from closing_hours where date = '".date("Y-m-d",strtotime($date))."'");
            return $q->result(); 
        }
}
?>

文件:Admin.php

public function time_slot(){
        if(_is_user_login($this)){
                $this->load->model("time_model");
                $timeslot = $this->time_model->get_time_slot();
                
                $this->load->library('form_validation');
                $this->form_validation->set_rules('opening_time', 'Opening Hour', 'trim|required');
                $this->form_validation->set_rules('closing_time', 'Closing Hour', 'trim|required');
                if ($this->form_validation->run() == FALSE)
        		{
        		  if($this->form_validation->error_string()!="")
        			  $this->session->set_flashdata("message", '<div class="alert alert-warning alert-dismissible" role="alert">
                                        <i class="fa fa-warning"></i>
                                      <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                                      <strong>Warning!</strong> '.$this->form_validation->error_string().'
                                    </div>');
        		}
        		else
        		{
        		  if(empty($timeslot)){
                    $q = $this->db->query("Insert into time_slots(opening_time,closing_time,time_slot) values('".date("H:i:s",strtotime($this->input->post('opening_time')))."','".date("H:i:s",strtotime($this->input->post('closing_time')))."','".$this->input->post('interval')."')");
                  }else{
                    $q = $this->db->query("Update time_slots set opening_time = '".date("H:i:s",strtotime($this->input->post('opening_time')))."' ,closing_time = '".date("H:i:s",strtotime($this->input->post('closing_time')))."',time_slot = '".$this->input->post('interval')."' ");
                  }  
                }            
            
            $timeslot = $this->time_model->get_time_slot();
            $this->load->view("admin/timeslot/editm",array("schedule"=>$timeslot));
        }
        else
        {
            redirect("admin");

表结构:

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

-- --------------------------------------------------------

--
-- Table structure for table `time_slots`
--

CREATE TABLE `time_slots` (
  `opening_time` time NOT NULL,
  `closing_time` time NOT NULL,
  `time_slot` int(11) NOT NULL,
  `store_id` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `time_slots`
--

INSERT INTO `time_slots` (`opening_time`, `closing_time`, `time_slot`, `store_id`) VALUES
('05:02:00', '12:00:00', 60, '319'),
('05:02:00', '12:00:00', 60, '320'),
('05:02:00', '12:00:00', 60, '321');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

请帮助我,我已经尝试解决这个问题好几天了,但我没有太多知识。

最佳答案

您需要将 store_id 添加到这两个语句中。即,INSERT 语句中的列列表,以及 UPDATE 语句中 WHERE 子句的一部分。目前,由于 UPDATE 语句没有 WHERE 子句,因此它会应用更改所有记录。

我发现在这两个语句的末尾不小心添加了一些额外的引号。如果您复制并粘贴了我的代码,这可能就是它不起作用的原因。对于那个很抱歉!在过去的 15 分钟里,我一直盯着它,试图看看我还错过了什么。

文件:Admin.php

else
{
    if(empty($timeslot)){
        $q = $this->db->query(
        "Insert into time_slots(opening_time,closing_time,time_slot, store_id) values(
        '" . date("H:i:s",strtotime($this->input->post('opening_time'))) . "',
        '" . date("H:i:s",strtotime($this->input->post('closing_time'))) . "',
        '" . $this->input->post('interval') . "',
        " . $this->input->post('store_id') . ")");
    }else{
        $q = $this->db->query("
        Update time_slots set 
        opening_time = '" . date("H:i:s",strtotime($this->input->post('opening_time'))) . "',
        closing_time = '" . date("H:i:s",strtotime($this->input->post('closing_time'))) . "',
        time_slot = '" . $this->input->post('interval') . "',
        WHERE store_id = " . $this->input->post('store_id'));
    }
}

免责声明:我还没有在这里回答很多问题,也没有弄清楚如何在我自己的机器上实现给定的代码进行测试。但是,看看您所提供的内容,我真的认为这至少是您问题的一部分,即使不是全部。

关于javascript - 当一家商店添加日期和时间也更改为所有其他商店时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58108406/

相关文章:

javascript - 如何使用 Jquery 动态创建 li 内的复选框

javascript - (typeof variable === "function") 和 jQuery.isFunction() 有什么区别?

javascript - 如何检查文本溢出:ellipsis is supported in browser

javascript - 访问并填充特定的 div 元素

php - Symfony2 登录后成功处理程序不工作

javascript - 使用 Javascript 移动一行中的列

javascript - WebRTC 无法在同一连接中回答具有多个轨道的报价

php - Codeigniter:如何避免在表中的 PHP foreach 循环中显示重复值或覆盖重复值?

php数组获取某个元素的序号

javascript - 写 "$(' #dialog').modal ('show' );"as plain javascript?