Welcome Guest, Not a member yet? Register   Sign In
How can I use current value of the column by "Active Record Class"?
#1

[eluser]fell0206[/eluser]
Hi, everybody!
I use "Active Record Class" of "Update", sometimes I want use current value of the column, but it always work incorrect.
Below is my code:
Code:
$data=array(
    'Title'=>$title,
    'Level'=>$level,
    'Image1'=>isset($file_names[0])?"$file_names[0]":`Image1`,
    'Image2'=>isset($file_names[1])?"$file_names[1]":`Image2`,
    'Image3'=>isset($file_names[2])?"$file_names[2]":`Image3`,
    'Status'=>$status
);
$this->db->where('ID',$id);
$this->db->update('A',$data);

But when I use query to run my sql, it is work correct.
Below is my code:
Code:
$Image1=isset($file_names[0])?"'$file_names[0]'":"`Image1`";
$Image2=isset($file_names[1])?"'$file_names[1]'":"`Image2`";
$Image3=isset($file_names[2])?"'$file_names[2]'":"`Image3`";
$this->db->query("
    UPDATE `watchandspeak`
        SET `Title` = '$title', `Level` = $level,
        `Image1` = $Image1, `Image2` = $Image2,
        `Image3` = $Image3, `Status` = '$status'
        WHERE `ID` = $id
    ");
How can I use current value of the column by "Active Record Class"? Thanks.
#2

[eluser]byde[/eluser]
Whats the error?? what does the mysql server says??

Try sprintf

Code:
$Image1=isset($file_names[0])?"'$file_names[0]'":"`Image1`";
$Image2=isset($file_names[1])?"'$file_names[1]'":"`Image2`";
$Image3=isset($file_names[2])?"'$file_names[2]'":"`Image3`";
$query = sprintf("
    UPDATE `watchandspeak`
        SET `Title` = '%s', `Level` = %s,
        `Image1` = %s, `Image2` = %s,
        `Image3` = %s, `Status` = '%s'
        WHERE `ID` = %s
    ",$title, $level, $Image2, $Image1, $Image3, $status, $id);

Or also you can use this way for active records

Code:
$data=array(
    'Title'=>$title,
    'Level'=>$level,
    'Status'=>$status
);
if (isset($file_names[0]))
    $data['Image1']=$file_names[0];
if (isset($file_names[1]))
    $data['Image2']=$file_names[1];
if (isset($file_names[2]))
    $data['Image3']=$file_names[2];

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




Theme © iAndrew 2016 - Forum software by © MyBB