Welcome Guest, Not a member yet? Register   Sign In
Update Array values to DB
#1
Sad 
(This post was last modified: 10-09-2019, 05:33 AM by ciadmin.)

I was trying to update the values as array. The problem that i face is it gets added to the values instead of getting updated.

Eg. when i did insert (Add) in country section i had India,Australia. Now i tried to update it to USA,UK.
But my update goes and inserts as India,AustraliaUSA,UK => exactly like this.

My code is,

Code:
public function edit($id)
{

    // If there is no testimonial in this id, then redirect
    $tot = $this->Model_movie->movie_check($id);
    if(!$tot) {
    redirect(base_url().'admin/movie/view');
        exit;
    }
     
      $data['setting'] = $this->Model_common->get_setting_data();
$error = '';
$success = '';


if(isset($_POST['movie_edit']))
{

$valid = 1;

$this->form_validation->set_rules('movie_name', 'Worldwide Name', 'trim|required');
$this->form_validation->set_rules('movie_original_name', 'Original Name', 'trim|required');
$this->form_validation->set_rules('movie_description', 'Description', 'trim|required');
$this->form_validation->set_rules('movie_stars', 'Enter Casts', 'trim|required');
$this->form_validation->set_rules('movie_director', 'Director', 'trim|required');
$this->form_validation->set_rules('movie_trailer', 'Trailer', 'trim|required');
$this->form_validation->set_rules('release_date', 'Release Date', 'trim|required');
$this->form_validation->set_rules('release_year', 'Release Year', 'trim|required');
$this->form_validation->set_rules('movie_genre[]', 'Select Genres', 'trim|required');
$this->form_validation->set_rules('movie_rating', 'Enter Rating', 'trim|required');
$this->form_validation->set_rules('movie_country[]', 'Select Country', 'trim|required');
$this->form_validation->set_rules('movie_runtime', 'Enter Runtime', 'trim|required');
$this->form_validation->set_rules('is_featured', 'Is Featured', 'trim|required');

if($this->form_validation->run() == FALSE) {
$valid = 0;
                $error .= validation_errors();
            }

            $path = $_FILES['movie_image']['name'];
    $path_tmp = $_FILES['movie_image']['tmp_name'];

    if($path!='') {
        $ext = pathinfo( $path, PATHINFO_EXTENSION );
        //$file_name = basename( $path, '.' . $ext );
        $file_name = basename( $path );
        $ext_check = $this->Model_common->extension_check_photo($ext);
        if($ext_check == FALSE) {
            $valid = 0;
            $error .= 'You must have to upload jpg, jpeg, gif or png file for featured photo<br>';
        }
    }

    if($valid == 1)
    {
    $data['movie'] = $this->Model_movie->get_movie($id);


    $val_movie_genre = $this->input->post('movie_genre');
        foreach($val_movie_genre as $row)
{
  //$movie_genre .= $row . ', ';
$movie_genre = $movie_genre . $row. ",";
}
$movie_genre = substr($movie_genre, 0, -1);

$val_movie_country = $this->input->post('movie_country');
foreach($val_movie_country as $row1)
{
  //$movie_country .= $row1 . ', ';
$movie_country = $movie_country . $row1. ",";
}
$movie_country = substr($movie_country, 0, -1);


    if($path == '') {
    $form_data = array(
'movie_name'        => $_POST['movie_name'],
'movie_original_name' => $_POST['movie_original_name'],
'movie_image'      => $final_name,
'movie_description'    => $_POST['movie_description'],
'movie_stars'    => $_POST['movie_stars'],
'movie_director'    => $_POST['movie_director'],
'movie_trailer'    => $_POST['movie_trailer'],
'release_date'    => $_POST['release_date'],
'release_year'    => $_POST['release_year'],
//'movie_genre'    => $_POST['movie_genre'],
'movie_genre'    => $movie_genre,
'movie_rating'    => $_POST['movie_rating'],
//'movie_country'    => $_POST['movie_country'],
'movie_country'    => $movie_country,
'movie_runtime'    => $_POST['movie_runtime'],
'is_featured'    => $_POST['is_featured']
            );
            $this->Model_movie->update($id,$form_data);
}
else {
unlink('./public/images/movies/'.$data['movie']['movie_image']);

//$final_name = 'testimonial-'.$id.'.'.$ext;
//$final_name = $path.'.'.$ext;
$final_name = $path;
        move_uploaded_file( $path_tmp, './public/images/movies/'.$final_name );


        $val_movie_genre = $this->input->post('movie_genre');
        foreach($val_movie_genre as $row)
{
  //$movie_genre .= $row . ', ';
$movie_genre = $movie_genre . $row. ",";
}
$movie_genre = substr($movie_genre, 0, -1);

$val_movie_country = $this->input->post('movie_country');
foreach($val_movie_country as $row1)
{
  //$movie_country .= $row1 . ', ';
$movie_country = $movie_country . $row1. ",";
}
$movie_country = substr($movie_country, 0, -1);

        $form_data = array(
'movie_name'        => $_POST['movie_name'],
'movie_original_name' => $_POST['movie_original_name'],
'movie_image'      => $final_name,
'movie_description'    => $_POST['movie_description'],
'movie_stars'    => $_POST['movie_stars'],
'movie_director'    => $_POST['movie_director'],
'movie_trailer'    => $_POST['movie_trailer'],
'release_date'    => $_POST['release_date'],
'release_year'    => $_POST['release_year'],
//'movie_genre'    => $_POST['movie_genre'],
'movie_genre'    => $movie_genre,
'movie_rating'    => $_POST['movie_rating'],
//'movie_country'    => $_POST['movie_country'],
'movie_country'    => $movie_country,
'movie_runtime'    => $_POST['movie_runtime'],
'is_featured'    => $_POST['is_featured']
            );
            $this->Model_movie->update($id,$form_data);
}

$success = 'Movie is updated successfully';
$this->session->set_flashdata('success',$success);
redirect(base_url().'admin/movie/view');
    }
    else
    {
    $this->session->set_flashdata('error',$error);
redirect(base_url().'admin/movie/edit'.$id);
    }
         
} else {
$data['movie'] = $this->Model_movie->get_movie($id);
      $this->load->view('admin/view_header',$data);
$this->load->view('admin/view_movie_edit',$data);
$this->load->view('admin/view_footer');
}

}


But the Add option is working fine.

EDIT: code tags added for readability. See MyCode
Reply
#2

I made it work in CorePHP but at codeigniter its not working,

foreach($_POST["movie_genre"] as $row)
{
$movie_genre .= $row . ', ';
}
$movie_genre = substr($movie_genre, 0, -2);


foreach($_POST["movie_country"] as $row1)
{
$movie_country .= $row1 . ', ';
}
$movie_country = substr($movie_country, 0, -2);



// Insert image file name into database

$sql_query1 = "UPDATE movies_info SET `movie_name`='$movie_name',`movie_original_name`='$movie_original_name',`movie_image`='$target',
`movie_description`='$movie_description',`movie_stars`='$movie_stars',`movie_director`='$movie_director',
`movie_trailer`='$movie_trailer',`release_date`='$release_date',`release_year`='$release_year',`movie_genre`='$movie_genre',
`movie_rating`='$movie_rating',`movie_country`='$movie_country',`movie_runtime`='$movie_runtime',
`is_featured`='$is_featured' WHERE `movie_id`='$movie_id'";
Reply
#3

@manigopal,

Why don't you var_dump the array so that you can see its value before and after you change it's value before you send it to the model. You could also turn on the page profiler ( https://codeigniter.com/user_guide/gener...t=profiler ). This will allow you to see the queries which are being run on the page.
Reply
#4

(10-09-2019, 07:45 AM)php_rocs Wrote: @manigopal,

Why don't you var_dump the array so that you can see its value before and after you change it's value before you send it to the model.  You could also turn on the page profiler ( https://codeigniter.com/user_guide/gener...t=profiler ).  This will allow you to see the queries which are being run on the page.


could you share the code for update array within array., since i have already tried it but it doesnt works.
Reply
#5

@manigopal,

If it works in corePHP and not CI then the value may be different. Can you print out the value type just to make sure that it is an array? Use gettype(). Let's make sure that it is an array first. Once we do that then we can verify that your loop condition is correct.
Reply
#6

(This post was last modified: 10-11-2019, 07:13 AM by Wouter60.)

Quote:$sql_query1 = "UPDATE movies_info SET `movie_name`='$movie_name',`movie_original_name`='$movie_original_name',`movie_image`='$target',
`movie_description`='$movie_description',`movie_stars`='$movie_stars',`movie_director`='$movie_director',
`movie_trailer`='$movie_trailer',`release_date`='$release_date',`release_year`='$release_year',`movie_genre`='$movie_genre',
`movie_rating`='$movie_rating',`movie_country`='$movie_country',`movie_runtime`='$movie_runtime',
`is_featured`='$is_featured' WHERE `movie_id`='$movie_id'";

I see a few problems.
First of all, if you enclose a variable in single quotes, it will be taken as a literal string, not as it's value.
In CI, you can pass an associated array to the $this->db->update() function, like this:
PHP Code:
$data = array(
  'movie_name' => $movie_name,
  'movie_original_name' => $movie_original_name,
  'movie_director' => $movie_director,
);
$this->db->where('id'$movie_id)->update('table_name'$data); 
(It works the same if you pass the $data to your model).

Another thing is how you convert the array of countries to a comma separated string.
You can use php's implode function:
PHP Code:
$movie_country implode(',' $this->input->post('movie_country')); 
If you read it back from your database, use explode() to convert it to an array.

A better method for storing an array into a table field in your database is php's serialize:
PHP Code:
$movie_country serialize($this->input->post('movie_country')); 
Serialize doesn't only store the array's values, but also the keys, variable-types and string-lengths. It will code all this information into a string.
Use unserialize() to convert it back to an array.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB