2

I am trying to save data in two different database tables , when i run two query it save 0 in the integer field.

     $sql = 'insert into photo_album( user_id, photo_id, album_id )
            values(". $user_id.",".$photo_id.",". $album_id.")';

          $query = $this->db->query($sql); 

but when i run query as hard coded values it work fine.

      INSERT INTO
       photo_album( photo_id, user_id, album_id )
        VALUES(8299,214,316)

can some one tell what is wrong ?

Sohail Yasmin
  • 498
  • 5
  • 16

7 Answers7

1
$data = array(
   'user_id' => '$user_id' ,
   'photo_id' => '$photo_id' ,
   'album_id' => '$album_id'
);

$this->db->insert('photo_album', $data);
CaPs LoCk
  • 150
  • 4
0

This should work for you:

(Don't mix single and double quotes! They are different :D)

$sql = "INSERT INTO `photo_album`(photo_id, user_id, album_id)
        VALUES('" . $photo_id . "', '" . $user_id . "', '" . $album_id . "')";

EDIT after comment:

Then try it to put ' around the values. Maybe the are stored as strings.

Also don't change the order of the column's to insert :D

Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

try like this in codeigniter format

$data = array(
                'user_id' => $user_id,
                'photo_id' => $photo_id,
                'album_id' => $album_id
             );
            $this->db->insert('photo_album', $data); 
Dexter
  • 1,804
  • 4
  • 24
  • 53
0

Try this

 $sql="INSERT INTO photo_album (user_id,photo_id,album_id)
                VALUES('$this->user_id','$this->photo_id','$this->album_id')";
zref
  • 37
  • 10
0

since you are using codeigniter then try like this:

$dataArr = array(
    'photo_id' => $photo_id,
   'user_id' => $user_id,
   'album_id' => $album_id
);

$this->db->insert('photo_album', $dataArr);
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

Try This Separate variables

$sql = 'insert into photo_album( `user_id`, `photo_id`, `album_id` )
        values("' .$user_id. '","' .$photo_id. '","' .$album_id. '")';
Arun Kumar
  • 1,607
  • 1
  • 18
  • 33
0

finely i manage to solve this issue , It is very strange you people will laugh at me / not believe me , I rename the field to some other name it start saving the value again , then i again rename to original name , I saw every this is working perfectly.

Thank you for your help.

please tell me if this is bug in mysql or not

Sohail Yasmin
  • 498
  • 5
  • 16