INSERT-INTO-Abfrage schlägt fehl

mojitoweb

Mitglied
Folgender Quelltext sollte funktionieren, es wird aber kein neuer datensatz angelegt. (Die Fehlermeldung weist auf einen Syntaxerror hin)

PHP:
    $qstring = "\"INSERT INTO wk_user ("; $qstring2 = ") VALUES(";
    if(isset($this->first_name)){ $qstring .= "first_name";   $qstring2 .= "'$this->first_name'"; }
    if(isset($this->sur_name))  { $qstring .= ", sur_name";   $qstring2 .= ", '$this->sur_name'"; }
    if(isset($this->password))  { $qstring .= ", password";   $qstring2 .= ", '$this->password'"; }
    if(isset($this->street))    { $qstring .= ", street";     $qstring2 .= ",'$this->street'";}
    if(isset($this->city))      { $qstring .= ", city";       $qstring2 .= ",'$this->city'";}
    if(isset($this->postcode))  { $qstring .= ", postcode";   $qstring2 .= ",'$this->postcode'";}
    if(isset($this->country_id)){ $qstring .= ", country_id"; $qstring2 .= ",'$this->country_id'";}
    if(isset($this->e_mail))    { $qstring .= ", e_mail";     $qstring2 .= ",'$this->e_mail'";}
    if(isset($this->icq))       { $qstring .= ", icq";        $qstring2 .= ",'$this->icq'";}
    if(isset($this->msn))       { $qstring .= ", msn";        $qstring2 .= ",'$this->msn'";}
    if(isset($this->yahoo))     { $qstring .= ", yahoo";      $qstring2 .= ",'$this->yahoo'";}
    if(isset($this->i_year))    { $qstring .= ", i_year";     $qstring2 .= ",'$this->i_year'";}
    if(isset($this->e_year))    { $qstring .= ", e_year";     $qstring2 .= ",'$this->e_year'";}
    if(isset($this->course))    { $qstring .= ", course";     $qstring2 .= ",'$this->course'";}
    if(isset($this->data))      { $qstring .= ", data";       $qstring2 .= ",'$this->data'";}
    if(isset($this->flags))     { $qstring .= ", flags";      $qstring2 .= ",'$this->flags'";}
                                  $qstring .= ", admin_id";   $qstring2 .= ",'0'";
    
    $qstring .= $qstring2;
    $qstring .= ")\"";
    
    mysql_query($qstring) or die(mysql_error());

das seltsame: Folgendes (eigentlich äquivalente) Codestück funktioniert:

PHP:
mysql_query("INSERT INTO wk_user(first_name, sur_name, password, street, city, postcode, country_id, e_mail, icq, msn, yahoo, i_year, e_year, course, data, admin_id) VALUES('o','o','86f7e437faa5a7fce15d1ddcb9eaeaea377667b8','dfj','j','12345','1','ich@web.de', '123456789','ghghj','gdff','8888','7777','jgk','lkshldkgkjw','0')");

Ich hoffe ihr seht es schneller als ich ... mit der Zeit wird man Codeblind ...
 
die funktion macht aus

"INSERT INTO wk_user (first_name, sur_name, password, street, city, postcode, country_id, e_mail, icq, msn, yahoo, i_year, e_year, course, data, admin_id) VALUES('d', 'd', '3c363836cf4e16666669a25da280a1865c2d2874','d','d','11111','4','d@d.d','11','d','d','1111','1111','d','d','0')"

dies

\"INSERT INTO wk_user (first_name, sur_name, password, street, city, postcode, country_id, e_mail, icq, msn, yahoo, i_year, e_year, course, data, admin_id) VALUES(\'d\', \'d\', \'3c363836cf4e16666669a25da280a1865c2d2874\',\'d\',\'d\',\'11111\',\'4\',\'d@d.d\',\'11\',\'d\',\'d\',\'1111\',\'1111\',\'d\',\'d\',\'0\')\"

das bringts also nicht ... meinermeinungnach ist der obere String von der syntax auch in Ordnung...
 
ups.

nungut ich konnte den fehler etwas eingrenzen.

undzwar funktioniert alles solange ich ich die Abfrage direkt ins query schreib:

mysql_query("INSERT INTO wk_user(first_name, sur_name) VALUE('t', 'm')") or die (mysql_error());

wenn ich dann versuche die Abfrage in eine Variable auszlagern bekomm ich einen angebligen Syntaxerror:

$qstring = "\"INSERT INTO wk_user(first_name, sur_name) VALUE('t', 'm')\"";
mysql_query($qstring) or die (mysql_error());

die Fehlermeldung:

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 '"INSERT INTO wk_user(first_name, sur_name) VALUE('t', 'm')"' at line 1
 
Gut, probier mal Folgendes:
PHP:
$values = array();
if( isset($this->first_name) ) {
	$values['first_name'] = $this->first_name;
}
if( isset($this->sur_name) ) {
	$values['sur_name'] = $this->sur_name;
}
if( isset($this->password) ) {
	$values['password'] = $this->password
}
if( isset($this->street) ) {
	$values['street'] = $this->street;
}
if( isset($this->city) ) {
	$values['city'] = $this->city;
}
if( isset($this->postcode) ) {
	$values['postcode'] = $this->postcode;
}
if( isset($this->country_id) ) {
	$values['country_id'] = $this->country_id;
}
if( isset($this->e_mail) ) {
	$values['e_mail'] = $this->e_mail;
}
if( isset($this->icq) ) {
	$value['sicq'] = $this->icq;
}
if( isset($this->msn) ) {
	$values['msn'] = $this->msn;
}
if( isset($this->yahoo) ) {
	$values['yahoo'] = $this->yahoo;
}
if( isset($this->i_year) ) {
	$values['i_year'] = $this->i_year;
}
if( isset($this->e_year) ) {
	$value['e_year'] = $this->e_year;
}
if( isset($this->course) ) {
	$values['course'] = $this->course;
}
if( isset($this->data) ) {
	$values['data'] = $this->data;
}
if( isset($this->flags) ) {
	$values['flags'] = $this->flags;
}
$values['admin_id'] = 0;
$query = '
	INSERT INTO
	        `wk_user`
	  SET';
foreach( $values as $key => $value ) {
	$query .= "\n".'`'.$key.'` = "'.mysql_real_escape_string($value).'",';
}
$query = substr($query, 0, -1);
mysql_query($query)
	or die(mysql_error());
Es wäre übrigens wesentlich einfacher, wenn die Werte bereits in einem Array gespeichert wären. Denn dann könnte auch die Überprüfung automatisiert werden.
 
Zurück