bluegray Posted May 31, 2010 Share Posted May 31, 2010 Hey Php freaks! Okay, I've been determined to make a function that processes form submissions into a database for me, but I'm getting this odd undefined error. I'm going nuts! :'( Here are the two functions I built: function array2fieldtest ($data) { //var_dump($data) ; //$element_count = count ($data) ; foreach ($data as $ky) { $key = key ($data) ; $keys[] = $key ; next ($data) ; } $fields = implode (', ', $keys) ; return $fields ; } and function array2valuetest ($data) { //var_dump($data) ; //$element_count = count ($data) ; $results = '\"' . implode ('\", \"', $data). '\"' ; return $results ; } And here's how I use those functions. $input = $_POST ['test'] ; $fields = array2fieldtest ($input) ; $values = array2valuetest ($input) ; $query = "INSERT INTO hobbies ($fields) VALUES ($values)" ; $result = mysql_query($query) or trigger_error( $query . mysql_error() . E_USER_ERROR) ; this is the error message I get: Notice: INSERT INTO hobbies (namefirst, namelast, social, solo, additional) VALUES (\"1\", \"2\", \"3\", \"\", \"End Here.\")You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\"1\", \"2\", \"3\", \"\", \"End Here.\")' at line 1256 in C:\Program Files\WAMP\www\test\form.php on line 73 What doesn't make sense is that when I manually replace the $fields and $values variables in the query with the values that are actually in the variables, the query works fine. Does SQL just hate me? Link to comment https://forums.phpfreaks.com/topic/203401-undefined-query-error/ Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 could be because of the quote escaping you're using in the second function try this $results = '"' . implode ('", "', $data). '"' ; Link to comment https://forums.phpfreaks.com/topic/203401-undefined-query-error/#findComment-1065548 Share on other sites More sharing options...
kenrbnsn Posted May 31, 2010 Share Posted May 31, 2010 Change <?php function array2valuetest ($data) { //var_dump($data) ; //$element_count = count ($data) ; $results = '\"' . implode ('\", \"', $data). '\"' ; return $results ; } ?> to <?php function array2valuetest ($data) { //var_dump($data) ; //$element_count = count ($data) ; $results = "'" . implode ("','", $data). "'" ; return $results ; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/203401-undefined-query-error/#findComment-1065549 Share on other sites More sharing options...
bluegray Posted May 31, 2010 Author Share Posted May 31, 2010 could be because of the quote escaping you're using in the second function try this $results = '"' . implode ('", "', $data). '"' ; Thaaaaank you!! It's ALIIIIIVE!! *maniacal laugh* But do you know why the escapes have such a dual effect? Why would it work manually, but fail in a variable? Link to comment https://forums.phpfreaks.com/topic/203401-undefined-query-error/#findComment-1065550 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 Well, in a var, quote usually get escaped automatically. (unless you turn it off) Link to comment https://forums.phpfreaks.com/topic/203401-undefined-query-error/#findComment-1065552 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.