Welcome Guest, Not a member yet? Register   Sign In
Hi I am getting a mysql error 1064 when inserting data is it me or my
#1

[eluser]Sally D[/eluser]
ok I am trying insert a multidimensional array it looks like this

array('from'=>array(1,2,3,4),'subject'=>array(1,2,3,4), 'body'=>array(1,2,3,4));

into a database set up like this

id (int) primary key,
from text,
subject text,
body text,

no my problem is when ever I do
$this->db->set(array(from=>$from,subject=>$subject,body=>$body));
$this->db->insert('email')

I get an mysql error 1064 is it me or mysql i don't know

here is the code I am working with

This is my model
Code:
<?

class Email2Db extends Model {
    
    

    function Email2Db()
    {
        parent::Model();
    }
    function saveEmail($email)
    {
    
       /*
                this simple test is not working I am getting a mysql database 1064
         $this->db->set(array('from'=>'ray','subject'=>'test','body'=>'test body'));
         $this->db->insert('email');
       */

      /* I want to use this code to update my data base with a multidimentional array that will be passed in from the saveEmail function and it just ain't working I don't know if its the code or mysql the simple test above to work either */

         $feildArray = array('from','subject','body');
                  
                    for($i = 1; $i <= sizeOf($email['body']); $i++)
                    {
                        foreach($feildArray as $val)
                        {
                            $$val = $email[$val][$i];
                            
                            $this->db->set($val,${$val});
                        }
                        
                        $this->db->insert('email');
                    }
          }

}

?&gt;

and here is my controller
Code:
function index()
    {
        
              
              
                $email = $this->mob->get_email();
                $this->email2db->saveEmail($email);
              
         }


Right now I don't know if its the code or if its mysql. Am I calling the $this->db->set function wrong. I'm getting delusional please help thanks alot.
#2

[eluser]Sally D[/eluser]
I got it to work by changing the code in my saveEmail function here is the updated version

but I had to hand code the sql.

Code:
function saveEmail($email)
    {
      
  
    
    $feildArray = array('from','subject','body');
                  
                    for($i = 1; $i <= sizeOf($email['body']); $i++)
                    {
                        $this->db->set('id',null);
                        foreach($feildArray as $val)
                        {
                            
                                $$val = $email[$val][$i];
                                
                              
                            
                        }

// this sql works It adds the email fields to the db

                         $sql = "INSERT INTO `email` ( `id` , `from` , `subject` , `body` ) VALUES ( NULL , '".$from."', '".$subject."', '".$body."') ";
                        
                         $this->db->query($sql);
                         if($this->db->affected_rows() == 1) { continue; } else { return false;}
                        
                    }
               return true;
        
      
    }


I wonder why I could not just use the $this->db->set('') with out getting a mysql errror 1064?




Theme © iAndrew 2016 - Forum software by © MyBB