I am having a problem with a concatenated string.
I start my string outside of a for() and then, concatenate the string generated with the loop to it.
the string out of the loop looks like this:
$q = "INSERT INTO (asdf1,asdf2,asdf3) VALUES"
then when i add something like this:
$q .="('".$asdf1."', '".$asdf2."', '".$asdf3."')"
when i make the query, mysql returns syntax error because the final string looks something like this:
"INSERT INTO (asdf1,asdf2,asdf3) VALUESasdf1', 'asdf2', 'asdf3')asdf1', 'asdf2', 'asdf3')asdf1', 'asdf2', 'asdf3')asdf1', 'asdf2', 'asdf3')...
So the first part of the concatenated string:"('" ,seem to desapear.
I simplified the query string to focus on the real problem...
here is the function code:
Expand|Select|Wrap|Line Numbers
- /* Insert new content into calendar */
- private function insert_into_calendar(){
- /* Create mysql query code */
- $this->query = 'INSERT INTO calendar (date_id, date, hour, hour_state, hour_sub, hour_sub_state) VALUES';
- $first = true;
- for($i = 1; $i<= $this->days_to_add ; $i++){ //DATE
- $date_to_add = date("d.m.y", mktime(0, 0, 0, $this->table_max_date_exploded['m'] , $this->table_max_date_exploded['d']+$i, "20".$this->table_max_date_exploded['y']));
- for($j = Calendar_auto_updater::DAY_HOUR_START; $j <= Calendar_auto_updater::DAY_HOUR_END; $j++){ //HOUR
- $hour = $j.":00";
- for($k = 0; $k <= $this->hour_sub_max; $k += $this->hour_fraction){ //HOUR_SUB
- if($k == 0){ //allways want an hour format of this kind "hh:mm"
- $hour_sub = $j.":".$k."0";
- }
- else{
- $hour_sub = $j.":".$k;
- }
- if($first == true){ //only want to put a "," before the "(" if its not just after "VALUES" SQL SYNAX
- $this->query .='(\''.$this->table_max_date_id+$i.'\', \''.$date_to_add.'\', \''.$hour.'\', \'0\', \''.$hour_sub.'\', \'0\')';
- $first = false;
- }
- else{
- $this->query .=', (\''.$this->table_max_date_id+$i.'\', \''.$date_to_add.'\', \''.$hour.'\', \'0\', \''.$hour_sub.'\', \'0\' )';
- }
- }
- }
- }
- print $this->query;
- /* Make query */
- $result = mysql_query($this->query, $this->connection) or die(mysql_error());
- if($result == true){
- return true;
- }
- else{
- $this->error['insert'] = "Error while inserting";
- return false;
- }
- }
Thank you very much,
Best Regards,