Jump to content

Undefined query error!?


bluegray

Recommended Posts

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?  :wtf:

Link to comment
https://forums.phpfreaks.com/topic/203401-undefined-query-error/
Share on other sites

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.