--- James Keeline <[EMAIL PROTECTED]> wrote: > --- Wade Smart <[EMAIL PROTECTED]> wrote: > > > I have been having a problem inserting data into the db. Well, that isnt > > true. I tested this thoroughly on my own system before uploading it to the > > server and then the error occurred right away. > > > > The query: > > INSERT INTO > > > deer_product(deer_type,name,parent_father,parent_mother,parent_father_father,parent_father_mother,parent_mother_father,parent_mother_mother,parent_father_father_father,parent_father_father_mother,parent_father_mother_father,parent_father_mother_mother,parent_mother_father_mother) > > > > VALUES ('doe', 'Jaky', 'Big Jake', 'Dropper Doe', 'MAx', 'Y-35', 'Dropper', > > 'T > > Condict Found''Angus', 'Waldvogel Chief Doe', 'Waldvogel Boomer', 'Tex > Doe', > > 'T > > Condict Found') > > > > You'll see the problem is just before 'Angus'; there is no comma there. > > > > I use this little script to handle the data query: > > $lastdeerkey = end($deer_key); > > foreach($deer_key as $key => $value){ > > if($value != $lastdeerkey){ > > $deer_key[$key] = $value.","; > > } > > } > > > > // put commas between values > > > > foreach($deer_data as $key => $value){ > > $deer_data[$key] = "'".$value."'"; > > } > > > > $lastdeervalue = end($deer_data); > > foreach($deer_data as $key => $value){ > > if($value != $lastdeervalue){ > > $deer_data[$key] = $value.", "; > > } > > } > > > > I cant see that there is an error but can anyone see something I missed? > > Im not sure why id didnt put in a comma on that one item though. > > > > Wade > > Queries like that become really hard to read and debug when things go wrong. > You are using a format like this: > > INSERT INTO tablename (col1, col2, col3) VALUES ('val1', 'val2', 'val3'); > > When there are just a few columns and values, it's not too hard to work with. > > However, I usually use this alternate syntax which keeps the field and value > together in the query: > > INSERT INTO tablename SET col1='val1', col2='val2', col3='val3'; > > It's even a little shorter. :) The main benefit is that it is easier to > compose the query in loop structures since you usually have both the field > name > and the value at a given instant. > > As far as why you are getting missing commas, I'm not sure. However, if one > of > your inputs had a single quote and it was not escaped, strange things can > occur. Validate all inputs sent to a query or really bad things can happen. > > James
Also, if you have something in an array, why not use the join() or implode() functions to glue the array elements into a string? $string = "'" . join("', '", $array) . "'"; Of course, you may need a loop to apply a function like addslashes() or mysql_real_escape_string() since these only work on strings and not entire arrays. Some functions will apply themselves to every element of an array and there are some array processing tricks which can allow you to do something to each element of an array. James