php - PyroCMS/Codeigniter 数据库问题

标签 php database validation codeigniter pyrocms

我可能遗漏了一些非常明显的东西,但我还是有点迷茫。实际的数据库交互似乎很重要。我的意思是,该表是在安装时创建的,但实际上发送信息似乎不起作用,因为无论我输入什么,它都会触发验证。如果我关闭字段要求,数据库将无法完全接收信息。

来自 Controller :

Admin 类扩展了 Admin_Controller {

private $show_validation_rules = array(
    array(
        'field' => 'date',
        'label' => 'Date',
        'rules' => 'trim|max_length[100]|required'
    ),
    array(
        'field' => 'location',
        'label' => 'Location',
        'rules' => 'trim|max_length[300]'
    ),
    array(
        'field' => 'support',
        'label' => 'Support',
        'rules' => 'trim|required'
    )

);

public function __construct()
{
    parent::__construct();

    $this->load->model('shows_m');
    $this->load->library('form_validation');
    $this->lang->load('shows');
    $this->load->helper('html');

    $this->template->set_partial('shortcuts', 'admin/partials/shortcuts');
}

public function index()
{
    $view_data = array();
    $view_data['shows'] = $this->shows_m->get_all();
    $this->template->build('admin/index', $view_data);
}

public function create()
{

    $shows = $this->shows_m->get_all();

    $this->form_validation->set_rules($this->show_validation_rules);

    if ( $this->form_validation->run() )
    {
        if ($this->shows_m->insert_show($this->input->post()))
        {
            $this->session->set_flashdata('success', lang('shows.create_success'));
            redirect('admin/shows/index');
        } else {
            $this->session->set_flashdata('error', lang('shows.create_error'));
            redirect('admin/shows/create');
        }
    }

    foreach($this->show_validation_rules as $rule)
    {
        $shows[$rule['field']] = $this->input->post($rule['field']);
    }
    $view_data = array(
            'shows' => $shows
        );
    $this->template->build('admin/create', $view_data);
}

public function edit($id)
{
    $this->form_validation->set_rules($this->show_validation_rules);

    $show = $this->shows_m->get($id);

    if ( empty($show) )
    {
        $this->session->set_flashdata('error', lang('shows.exists_error'));
        redirect('admin/shows');
    }

    if ( $this->form_validation->run() )
    {

        if ( $this->shows_m->update_entry($id, $this->input->post()) === TRUE )
        {
            if ( isset($this->input->post()['delete']) )
            {
                $this->session->set_flashdata('success', lang('shows.delete_success'));
                redirect('admin/shows/');
            }
            else
            {
                $this->session->set_flashdata('success', lang('shows.update_success'));
                redirect('admin/shows/edit/' . $id);
            }
        } else {
            if ( isset($this->input->post()['delete']) )
            {
                $this->session->set_flashdata('error', lang('shows.delete_error'));
                redirect('admin/shows/edit/' . $id);
            }
            else
            {
                $this->session->set_flashdata('error', lang('shows.update_error'));
                redirect('admin/shows/edit/' . $id);
            }
        }
    }

    foreach($this->show_validation_rules as $rule)
    {
        if ($this->input->post($rule['field']))
        {   
            $show[$rule['field']] = $this->input->post($rule['field']);
        }
    }
    $view_data = array(
            'shows' => $show
        );
    $this->template->build('admin/edit', $view_data);
}

public function delete($id = NULL)
{
    $id_array = array();

    if ( $this->input->post() )
    {
        $id_array = $this->input->post()['action_to'];
    }
    else
    {
        if ( $id !== NULL )
        {
            $id_array[0] = $id;
        }
    }

    if ( empty($id_array) )
    {
        $this->session->set_flashdata('error', lang('shows.id_error'));
        redirect('admin/shows');
    }

    foreach ( $id_array as $id)
    {

        $show = $this->shows_m->get($id);

        if ( !empty($show) )
        {

            if ( $this->shows_m->delete($id) == FALSE )
            {
                $this->session->set_flashdata('error', lang('shows.delete_error'));
                redirect('admin/shows');
            }
        }
    }

    $this->session->set_flashdata('success', lang('shows.delete_success'));
    redirect('admin/shows');
}

来自 details.php 文件

public function install()
{
    $this->dbforge->drop_table('shows');
    $shows = "
        CREATE TABLE ".$this->db->dbprefix('shows')." (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `date` varchar(100) NOT NULL,
          `location` varchar(300) NOT NULL,
          `support` text,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ";

    if($this->db->query($shows))
    {
        return TRUE;
    }
}

简而言之,该模块的目的是通过将模块生成的标签插入网站的相关页面来输出演出日期、地点和任何支持乐队以及其他信息。

我倾向于认为它应该非常简单并且我遗漏了一些明显的东西,但谁知道呢。如果是,请随时对我大喊大叫,我将不胜感激。

编辑:

模型代码

类 Shows_m 扩展了 MY_Model {

public function get_all($limit = NULL)
{
    $this->db->order_by("id", "desc");
    if (isset($limit)){ $this->db->limit($limit); }
    $results = $this->db->get('shows');
    $result = $results->result_array();
    return $result;
}


public function get($id)
{
    $results = $this->db->get_where('shows', array('id' => $id));
    $result = $results->row_array();
    return $result;
}


public function insert_show($input)
{

    $to_insert = array(
        'date' => $input['date'],
        'location' => $input['location'],
        'support' => $input['support']
    );


    if ($this->db->insert('shows',$to_insert))
    {
        return TRUE;
    } else {
        return FALSE;
    }
}


public function update_entry($id, $input)
{
    $new_data = array(
        'date' => $input['date'],
        'location' => $input['location'],
        'support' => $input['support']
    );

    if (isset ($input['delete']) )
    {
        if($this->delete($id))
        {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {

        $this->db->where('id', $id);
        if ($this->db->update('shows', $new_data))
        {
            return TRUE;    
        } else {
            return FALSE;    
        }
    }
}


public function delete($id)
{
    if ($this->db->delete('shows', array('id' => $id)))
    {
        return TRUE;    
    } else {
        return FALSE;    
    }
}
}

最佳答案

它可能是你在模型中的insert

if ($this->db->insert('shows',$to_insert))
    {
        return TRUE;
    } else {
        return FALSE;
    }

改为尝试:

$this->db->insert('shows',$to_insert);
$query_result = $this->db->insert_id();
if($query_result){
    return TRUE;
}else{
    return FALSE;
}

我不认为 insert 返回任何东西。

无论如何,这听起来不像是验证问题。查询未到达数据库。如果以上不是问题,请尝试仅echo 输出POST 数据;确保它到达模型(确保 HTML 符合预期——输入名称等)。

关于php - PyroCMS/Codeigniter 数据库问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7235578/

相关文章:

sql-server - 将 SQL Server Express 2008 与 SQL Server 2005 数据库一起使用 - 如何检查兼容性

php - Laravel 表单输入整数长度

php - 我如何强制 Joomla 更新用户 session

php - android-php-mysql : json + rmi

PHP fatal error : Cannot redeclare function

asp.net - 从数据库中检索文件路径以设置图像控件

php - 同源策略——JavaScript 调用 PHP

php - 数据排名/排序的正确且最有效的方法是什么

jquery-ui - jquery datepicker 和自定义验证

Java 有效日期和 JOptionPane