我在使用创建表时遇到问题,如果在打开的购物车中不存在,这里是我的代码
class ModelAccountOpentickets extends Model {
public function sendMessage($data) {
$this->db->query("INSERT INTO " . DB_PREFIX . "rbm_message SET customer_id = '" . (int)$data['customer_id'] . "', message = '" . $this->db->escape($data['message']) . "', sender = 'customer', date_added=NOW()");
}
public function getMessagesByCustomerId($data) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "rbm_message WHERE customer_id = '" . (int)$data['customer_id'] . "' ORDER BY date_added DESC");
return $query->rows;
}
public function CreateTable(){
$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "rbm_message` (`message_id` int(11) NOT NULL AUTO_INCREMENT,`user_id` int(11) NOT NULL,`customer_id` int(11) NOT NULL,`sender` varchar(20) NOT NULL,`message` text NOT NULL,`read_status` tinyint(1) NOT NULL,`date_added` datetime NOT NULL,PRIMARY KEY (`message_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");
}
}
这是结果
注意:错误:表'databas_rbm_message'不存在
错误号:1146
INSERT INTO databas_message SET customer_id ='2',message ='ahmed khairy' , /mysqli.php 中的'customer',date_added = NOW() 41 {“成功”:“text_success”} 做错了什么,谢谢你
答案 0 :(得分:0)
我认为将CreateTable代码放入类的构造中会更好:
public function __construct() {
$this->db->query("CREATE TABLE IF NOT EXISTS .......
}
这样,在运行任何其他方法/函数之前,它总是会检查表是否存在(如果它不存在则创建它)。