Welcome Guest, Not a member yet? Register   Sign In
Simple Insert issue
#1

[eluser]ericsodt[/eluser]
Why is it that my code will only insert into the database when I used the following:
Code:
$insertData = array(
           '`id`'            => NULL,
               '`desc`'         => $this->Event->desc,
               '`start_date`'         => $this->Event->startDate,
               '`end_date`'         => $this->Event->endDate,
               '`start_time`'         => $this->Event->startTime,
               '`end_time`'         => $this->Event->endTime,
               '`recurring`'         => $this->Event->recurring,
               '`neighborhood_id`'     => NULL,
               '`event_type_id`'     => NULL                              
         );
        
         $this->db->insert('`EVENT`', $insertData);

However when I do it this way it doesnt work:
Code:
$insertData = array(
           'id'            => NULL,
               'desc'             => $this->Event->desc,
               'start_date'         => $this->Event->startDate,
               'end_date'         => $this->Event->endDate,
               'start_time'         => $this->Event->startTime,
               'end_time'         => $this->Event->endTime,
               'recurring'         => $this->Event->recurring,
               'neighborhood_id'     => NULL,
               'event_type_id'         => NULL                              
         );
        
         $this->db->insert('EVENT', $insertData);

Is there a way of fixing this or is this how you have to use db->insert ??
#2

[eluser]Alex007[/eluser]
Does it give an error message when it "doesn't work" ? What does it say ?
#3

[eluser]ericsodt[/eluser]
The standard mySQL error message saying there is an error in the syntax and to consult the manual
#4

[eluser]Alex007[/eluser]
Can you post it ?
#5

[eluser]deviant[/eluser]
EVENT is a keyword in MySQL, either name your table something else or you'll have to use the funny quotation marks or whatever they are.
#6

[eluser]Michael Wales[/eluser]
Yeah - I think it's only the INSERT statement causing the issue, not all of the variables within your array. So, this should work:

Code:
$insertData = array(
           'id'            => NULL,
               'desc'             => $this->Event->desc,
               'start_date'         => $this->Event->startDate,
               'end_date'         => $this->Event->endDate,
               'start_time'         => $this->Event->startTime,
               'end_time'         => $this->Event->endTime,
               'recurring'         => $this->Event->recurring,
               'neighborhood_id'     => NULL,
               'event_type_id'         => NULL                              
         );

$this->db->insert('`EVENT`', $insertData);
#7

[eluser]Alex007[/eluser]
[quote author="deviant" date="1190407404"]EVENT is a keyword in MySQL, either name your table something else or you'll have to use the funny quotation marks or whatever they are.[/quote]

This is what I tought first, but I can't find it in the MySQL Reserver Keywords list...

http://dev.mysql.com/doc/refman/5.0/en/r...words.html
http://dev.mysql.com/doc/refman/4.1/en/r...words.html

But I still agree with you, the funny quotes around the table name should be enough.
#8

[eluser]deviant[/eluser]
Article about EVENT
#9

[eluser]ericsodt[/eluser]
Thanks for the replies guys... but I still need the "`" around all attributes otherwise it doesnt work. Below is a some sample code I wrote

Code:
$insertData = array(
           '`id`'            => 6,
               '`desc`'            => $this->Event->desc,
               '`start_date`'         => $this->Event->startDate,
               '`end_date`'         => $this->Event->endDate,
               '`start_time`'         => $this->Event->startTime,
               '`end_time`'         => $this->Event->endTime,
               '`recurring`'         => $this->Event->recurring,
               '`neighborhood_id`'    => NULL,
               '`event_type_id`'     => NULL                              
         );
        
         // NEED TO CHANGE.  SHOULD BE EVENT->SAVE
         $this->db->insert('`EVENT`', $insertData);
        
        // ONLY ERROR 1062 DUP VALUE ON INDEX
        if( $errno = $this->db->_error_number() ){
            $this->db->where('id', $insertData['`id`']);
            $obj = $this->db->update('`EVENT`', $insertData);
        }

If the "`" are not in the code then both inserts and updates do not work correctly... also if you can think of another way to update when an insert fail, could you let me know. thx Smile
#10

[eluser]deviant[/eluser]
Well, you have also managed to choose another MySQL keyword: DESC :-)




Theme © iAndrew 2016 - Forum software by © MyBB