Codeigniter:更新无法使用相同的用户名更新

时间:2015-02-06 05:26:41

标签: php codeigniter

嗨,我有这组代码正在工作的控制器:

public function edit_book(){
        $this->load->library('form_validation');

        $this->form_validation->set_rules('bName', 'Book Name', 'required|trim|xss_clean|strip_tags|max_length[255]|is_unique[books.book_name]');
        $this->form_validation->set_rules('desc', 'Description', 'required|trim|xss_clean|strip_tags|max_length[255]');
        $this->form_validation->set_rules('author', 'Author', 'required|trim|xss_clean|strip_tags|callback__alpha_dash_space|max_length[255]');
        $this->form_validation->set_rules('publisher', 'Publisher', 'required|trim|xss_clean|strip_tags|max_length[255]');
        $this->form_validation->set_rules('pages', 'Pages', 'required|trim|xss_clean|strip_tags|numeric|max_length[5]|is_natural');
        if($this->form_validation->run()){
            $month = $this->input->post('months');
            $day = $this->input->post('days');
            $year = $this->input->post('years');
            $pDate = date("d-m-Y",mktime(0,0,0,$month,$day,$year));
        }
        $this->form_validation->set_rules('price', 'Price', 'required|trim|xss_clean|strip_tags|max_length[9]|decimal');
        $this->form_validation->set_rules('status', 'Status', 'required');
        $this->form_validation->set_rules('quantity', 'Quantity', 'required|trim|xss_clean|strip_tags|max_length[3]|numeric|is_natural');
        $this->form_validation->set_rules('genres', 'Genres', 'required|trim|xss_clean|strip_tags|max_length[255]|alpha_dash_space');
        $this->form_validation->set_rules('reviews', 'Reviews', 'required|trim|xss_clean|strip_tags|max_length[255]');

        if ($this->form_validation->run()){
            $this->load->model('model_books');
            if ($this->model_books->edit_book($pDate)){
                $data = array(
                    'title' => 'Edited Page'
                );
                $data2 = array(
                    'operation' => 'edited',
                    'repeat' => '',
                    'page' => 'edit'
                );
                $this->load->view("header", $data);
                $this->load->view("operation_done", $data2);
                $this->load->view("footer");
            } else {
                echo "Error editing books";
            }
        } else {
            $this->edit_error();
        }
    }

观点:

<?php

    echo form_open('admin/edit_book');

    echo validation_errors();

    foreach($books as $book) {
        $bID = $book->book_id;
        $bName = $book->book_name ;
        $desc = $book->description;
        $author = $book->author;
        $publisher = $book->publisher;
        $pages = $book->pages;
        $pDate = $book->publication_date;
        $price = $book->price;
        $status = $book->status;
        $quantity = $book->quantity;
        $genres = $book->genres;
        $reviews = $book->reviews;
    };

    echo "<p style= font-size:20px;color:#00FF33;>Book ID:". $bID . "</p>";

    $data = array(
              'name'        => 'bID',
              'id'          => 'bID',
              'value'       => $bID,
              'style'       => 'font-size:18px',
              'size'        => '35',
              'type'        => 'hidden'
            );
    echo form_input($data);

    $data = array(
              'name'        => 'bName',
              'id'          => 'bName',
              'value'       => $bName,
              'style'       => 'font-size:18px',
              'size'        => '35',
              'placeholder' => 'Book Name',
            );
    echo form_input($data);
    echo "<br /><br />";

    $data = array(
              'name'        => 'desc',
              'id'          => 'desc',
              'value'       => $desc,
              'rows'        => '4',
              'cols'        => '34',
              'style'       => 'font-size:18px',
              'placeholder' => 'Description',
            );
    echo form_textarea($data);
    echo "<br /><br />";

    $data = array(
              'name'        => 'author',
              'id'          => 'author',
              'value'       => $author,
              'style'       => 'font-size:18px',
              'size'        => '35',
              'placeholder' => 'Author',
            );
    echo form_input($data);
    echo "<br /><br />";

    $data = array(
              'name'        => 'publisher',
              'id'          => 'publisher',
              'value'       => $publisher,
              'style'       => 'font-size:18px',
              'size'        => '35',
              'placeholder' => 'Publisher',
            );
    echo form_input($data);
    echo "<br /><br />";

    $data = array(
              'name'        => 'pages',
              'id'          => 'pages',
              'value'       => $pages,
              'style'       => 'font-size:18px',
              'size'        => '35',
              'placeholder' => 'Pages',
            );
    echo form_input($data);
    echo "<br /><br />";

    $months = array('' => 'Month',
                 '1'  => 'Jan',
                 '2'  => 'Feb',
                 '3'  => 'Mar',
                 '4'  => 'Apr',
                 '5'  => 'May',
                 '6'  => 'Jun',
                 '7'  => 'Jul',
                 '8'  => 'Aug',
                 '9'  => 'Sep',
                 '10' => 'Oct',
                 '11' => 'Nov',
                 '12' => 'Dec'
                );
         $days[''] = 'Day';         
         for($i=1;$i<=31;$i++){
            $days[$i] = $i;
         }

         $start_year = date("Y",mktime(0,0,0,date("m"),date("d"),date("Y")-80));
         $years[''] = 'Year';

         for ($i=$start_year;$i<=date("Y");++$i) {
            $years[$i] = $i;
         }
         $date = DateTime::createFromFormat("d-m-Y", $pDate);
    echo form_label('Publication Date','pDate')."<br/>".
        form_dropdown('days',$days, $date->format("d"), 'required="required"'). " " . form_dropdown('months',$months, $date->format("m"), 'required="required"'). " " . form_dropdown('years',$years, $date->format("Y"), 'required="required"');
    echo "<br /><br />";


    echo form_label('$:');
    $data = array(
              'name'        => 'price',
              'id'          => 'price',
              'value'       => $price,
              'style'       => 'font-size:18px',
              'size'        => '34',
              'placeholder' => 'Price',
            );
    echo form_input($data);
    echo "<br /><br />";

    echo "<p>Status: ";
    $statusOptions = array(
                  ''    => '',
                  'Available'   => 'Available',
                  'Out-Of-Stock'  => 'Out-Of-Stock',
                  'Requires Ordering'  => 'Requires Ordering',
                  'Archived' => 'Archived'
                );
    echo form_dropdown('status', $statusOptions, $status, 'required="required"');
    echo "</p>";

    $data = array(
              'name'        => 'quantity',
              'id'          => 'quantity',
              'value'       => $quantity,
              'style'       => 'font-size:18px',
              'size'        => '35',
              'placeholder' => 'Quantity',
            );
    echo form_input($data);
    echo "<br /><br />";

    $data = array(
              'name'        => 'genres',
              'id'          => 'genres',
              'value'       => $genres,
              'style'       => 'font-size:18px',
              'size'        => '35',
              'placeholder' => 'Genres',
            );
    echo form_input($data);
    echo "<br /><br />";

    $data = array(
              'name'        => 'reviews',
              'id'          => 'reviews',
              'value'       => $reviews,
              'rows'        => '4',
              'cols'        => '34',
              'style'       => 'font-size:18px',
              'placeholder' => 'Reviews',
            );
    echo form_textarea($data);
    echo "<br /><br />";

    echo "<p>";
    echo form_submit('edit_book', 'Edit');
    echo "</p>";

    echo form_close();
    ?>

一切正常,但每当我想编辑书籍而不更改书名时,验证检查会说该书已经存在于数据库中,并且不允许我更新我已更改的记录,除非我改变书名,请帮助我!

3 个答案:

答案 0 :(得分:0)

您确定tablename.fieldname是正确还是

尝试将is_unique[books.book_name]更改为is_unique[books.bName]

答案 1 :(得分:0)

请勿删除您的验证。

仅在名称与现有名称不同时才包含验证。否则Coideigniter不允许您创建重复的字段。您需要手动检查名称是否已更改。

答案 2 :(得分:0)

编辑时可以使用:

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');

并且函数edit_unique()可用here