非常奇怪的输入问题

时间:2011-05-05 10:45:27

标签: php mysql codeigniter

我有一个非常奇怪的输入问题,似乎我的seo描述框和主要内容框是链接的,因为如果我将任何数据输入到seo输入框中,它在内容区域的数据库中也会改变但是而不是相反。

查看:

<?php
//Setting form attributes
$formpageEdit = array('id' => 'pageEdit', 'name' => 'pageEdit');
$formInputTitle = array('id' => 'title', 'name' => 'title');
$formSEODescription = array('id' =>'seoDescription', 'name' => 'seoDescription');
$formTextareaContent = array('id' => 'textContent', 'name' => 'textContent');
?>

<?php print_r($page);?>

<div id ="formLayout" class="editPage">
<?php echo form_open('admin/editpage/index/'.$page[0]['id'].'/'.url_title($page[0]['name'],'dash', TRUE),$formpageEdit); ?>
<?php echo form_fieldset(); ?>
<h4>You are editing: <?= $page[0]['name']; ?> </h4>
<section id = "validation"><?php echo validation_errors();?></section>
<?php
if($success == TRUE) {
echo '<section id = "validation">Page Updated</section>';   
}
?>
<label><?php echo form_label ('SEO Description:', 'description');?><span class="small">Required Field</span></label>
<?php echo form_input($formSEODescription, $page[0]['description']); ?>
<label><?php echo form_label ('Content:', 'content');?><span class="small">Required Field</span></label>
<?php echo form_textarea($formTextareaContent, $page[0]['content']); ?>
<script type="text/javascript">CKEDITOR.replace('textContent');</script>
<?php echo form_submit('submit','Submit'); ?>
<?php echo form_fieldset_close();
      echo form_close(); ?>
</div>

模型

<?php
/**
* This model handles the sql for the checking of the username in the database
*/
class Page_model extends CI_Model
{

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

    function Page_model(){
        parent::Model();
    }

    function getCMSContent($id = NULL) {
        $this->db->where('id', $id);
        $query = $this->db->get('pages', 1);

        if($query->num_rows() > 0) {
            $row = $query->result_array();
            return $row;
        }else{
            return FALSE;
        }
    }

    function updatePage($id = NULL, $data = NULL){
        #set the $data passed to the function into an array, content being the column name.
        $data = array('content' => $data, 'description' => $data);

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

        return TRUE;
    }
}


?>

控制器

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Editpage extends CI_Controller {

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

  function index($id){

        if(!$this->session->userdata('logged_in'))redirect('admin/home');

        if ($this->input->post('submit')){

            #The User has submitted updates, lets begin!

            #Set The validation Rules   
            $this->form_validation->set_rules('textContent', 'Content', 'trim|required|xss_clean');
            $this->form_validation->set_rules('seoDescription', 'SEO Description', 'trim|required|xss_clean');

            #if the form_validation rules fail then load the login page with the errors. Otherwise continue validating the user/pass
            if ($this->form_validation->run() == FALSE){

                $data['cms_pages'] = $this->navigation_model->getCMSPages($id);
                #connect to getCMSCotent and set the page info equal to the $data['page'] where the row is equal to the passed $id from the URL.
                $data['page'] = $this->page_model->getCMSContent($id);
                $data['sales_pages'] = $this->sales_model->getSalesPages();

                $data['content'] = $this->load->view('admin/editpage', $data, TRUE);
                $this->load->view('admintemplate', $data);

            }               
            #Form Validation passed, so lets continue updating.
                #lets set some variables.
                $content = $this->input->post('textContent', TRUE);
                $content = $this->input->post('seoDescription', TRUE);
                $this->db->escape($content);
                #Now if updatePage fails to update hte database then show "there was a problem", you could echo the db error itself
                if($this->page_model->updatePage($id, $content)) {
                    $data['cms_pages'] = $this->navigation_model->getCMSPages($id);
                    #connect to getCMSContent and set the page info equal to the $data['page'] where the row is equal to the passed $id from the URL.
                    $data['page'] = $this->page_model->getCMSContent($id);
                    $data['success'] = TRUE;
                    $data['content'] = $this->load->view('admin/editpage', $data, TRUE);
                    $this->load->view('admintemplate', $data);
                }//END if updatePage
            }else{
            $data['cms_pages'] = $this->navigation_model->getCMSPages($id);
            $data['sales_pages'] = $this->sales_model->getSalesPages();
            #connect to getCMSCotent and set the page info equal to the $data['page'] where the row is equal to the passed $id from the URL.
            $data['page'] = $this->page_model->getCMSContent($id);
            $data['content'] = $this->load->view('admin/editpage', $data, TRUE);
            $this->load->view('admintemplate', $data);
        }//END if post submitted
    } //END function index()

}

1 个答案:

答案 0 :(得分:0)

找到它:

  $content = $this->input->post('textContent', TRUE);
  $content = $this->input->post('seoDescription', TRUE);

[更新如下]

您正在覆盖$content变量,其内容为seoDescription,因此textContent永远不会到达您的数据库。

您需要更新updatePage功能:

function updatePage($id = NULL, $content = NULL, $description = NULL){
    #set the $data passed to the function into an array, content being the column name.
    $data = array('content' => $content, 'description' => $description);

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

    return TRUE;
}

并适当地调用它:

[...]
    #Form Validation passed, so lets continue updating.
        #lets set some variables.
        $content     = $this->input->post('textContent', TRUE);
        $description = $this->input->post('seoDescription', TRUE);
        $this->db->escape($content);
        $this->db->escape($description);
        #Now if updatePage fails to update hte database then show "there was a problem", you could echo the db error itself
        if($this->page_model->updatePage($id, $content, $description)) {
[...]

顺便提一下,您确定正确使用db->escape吗?只有escape函数通过引用接受参数(例如,在参数名称前使用&并将此值设置为转义值),您调用它的方式才有效。我希望这段代码是正确的版本,但我不知道你的db类,所以我可能错了:

        $content     = $this->db->escape($this->input->post('textContent', TRUE));
        $description = $this->db->escape($this->input->post('seoDescription', TRUE));