开发者

Very Strange Input issue

I have got a very strange input issue in which it seems that my seo description box and main content box are linked due to if I input any data into the seo input box it changes in the database in the content area as well but not the otherway around.

View:

<?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_c开发者_Python百科lose(); ?>
</div>

Model

<?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;
    }
}


?>

Controller

<?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()

}


Found it:

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

[update below]

You are overwriting the $content variable with the content of seoDescription so textContent never reaches your db.

You need to update your updatePage function:

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;
}

and call it appropriately:

[...]
    #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)) {
[...]

By the way are you sure you are using db->escape correctly? The way you're calling it will only work if the escape function accepts parameters by reference (e.g. using & in front of the parameter name, and setting this value to the escaped value). I'd expect this code to be the correct version, but I don't know your db class so I may be wrong:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜