控制器文件
public function update()
{
$id= $this->input->post('id');
$data = array(
'dept_name' => $this->input->post('dept_name'),
'dept_descr' => $this->input->post('desc')
);
$udata = $this->Department_model->update($id,$data);
//print_r($udata);
if($udata !== 0)
{
echo 'update';
exit;
}
else
{
echo 'failed';
exit;
}
}
模型文件
我能够将值从模型传递到存储过程。
public function update($id,$data)
{
$userlevel = $this->db->query("CALL sp_insert_dept('tbl_Department','dept_name,dept_descr,id,meta_create_usr, ','''".$data['dept_name']."'',''".$data['dept_descr']."'',".$id.",1','update',@msg)");
return $userlevel;
}
存储过程
使用此存储过程,我可以执行插入操作,但无法执行更新操作。
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_insert_dept`(IN `tablename` LONGTEXT, IN `column_names` LONGTEXT, IN `col_values` LONGTEXT, IN `action_type` LONGTEXT, IN `col_id` LONGTEXT, OUT `msg` LONGTEXT)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION SELECT 'SQLException encountered';
if action_type='insert'
THEN
set @s=CONCAT('insert into ' ,tablename, '(',column_names,') values (',col_values,')');
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
set msg='success';
END IF;
if action_type='update'
THEN
set @j=CONCAT('update ' ,tablename, 'set ' ,column_names=col_values, ' where id=',col_id);
PREPARE stmt1 FROM @j;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
set msg='success';
END IF;
END$$
DELIMITER ;