1.3k Aufrufe
Gefragt in Webseiten HTML von
Hi Leute,

ich schreibe grade ein paar PHP-Funktionen, um mir das Arbeiten mit MySQL und PHP ein wenig zu Vereinfachen. Nun erhalte für meine MySQL-Anweisung folgende Fehlermeldung:

"Einfügen nicht möglich: 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 ''lname','fname') values('test','testII')' at line 1"

hier mal der Quelltext

1. functions_db.php:

/*functions_db.php consists about a few functions for mysql like connect, insert, update, delete etc.*/

/* open_mysql_connection
*
* connects to server; needs server name, user name, user password and database name
* if connection or database connection couldn't be come about open_mysql_connection returns with an error
* else open_mysql_connection returns the connection
*/
function open_mysql_connection($server, $user, $password, $db)
{
@$tmp_link = mysql_connect($server, $user, $password);
@$tmp_db = mysql_select_db($db, $tmp_link);
$tmp_array = array();

if($tmp_link and $tmp_db)
{
$tmp_array[0] = $tmp_link;
$tmp_array[1] = $tmp_db;
$tmp_array[2] = $db;

return $tmp_array;
}
elseif(!$tmp_link)
{
echo "<label>Verbindung zum MySQL-Server fehlgeschlagen: ".mysql_error()."</label>";
return false;
}
elseif(!$tmp_db)
{
echo "<label>Verbindung zur Datenbank fehlgeschlagen: ".mysql_error($tmp_link)."</label>";
return false;
}
}

/* insert_mysql
*
* insert commands into a table
* there isn't a limit of params because this function works with a variable list of parameters
*
* NOTE: from param 3 straight commands has to be equal to the column names in the table instead odd-numbered commands are their values
*/
function insert_mysql($tmp_link, $table)
{
$num_args = func_num_args(); // takes the number of arguments of insert_mysql
$mysql_link = $tmp_link[0]; // link resource
$mysql_db = $tmp_link[1]; // boolean
$db_name = $tmp_link[2];

if(!$mysql_link and !$mysql_db)
return false;
else
{
@$query_table = mysql_query("show tables from $db_name like '$table';", $mysql_link);
@$result_table = mysql_fetch_row($query_table);

if(!$result_table) // if table not exist
{
echo "<label>Tabelle nicht vorhanden</label>";
return false;
}
else
{
$name = array(); // takes column names of table
$value = array(); // takes values to insert in table
$str_name = "";
$str_value = "";

for($i = 2; $i < $num_args; $i++)
{
if(is_string(func_get_arg($i)) and $i % 2 == 0) // straight-numbered saves the column names of table
$name[$i] = "'".func_get_arg($i)."',";
elseif(is_string(func_get_arg($i)) and $i % 2 != 0) // odd-numbered saves their values
$value[$i] = "'".func_get_arg($i)."',";
}

// last comma will be deleted for mysql command insert
$str_name = rtrim(implode($name), ",");
$str_value = rtrim(implode($value), ",");

@$query_insert = mysql_query("insert into $table ($str_name) values($str_value);");
@$result_insert = mysql_fetch_row($query_insert);

if(!$result_insert) // if result's incorrect
{
echo "<label>Einfügen nicht möglich: ".mysql_error($mysql_link)."</label>";
return false;
}
}
}

2. testII.php:

<html>
<head>
<?php include "functions_db.php";?>
</head>
<body>
<?php
$mysql_link = open_mysql_connection("localhost","root","","test");
insert_mysql($mysql_link, "name", "lname", "test", "fname", "testII");
?>
</body>
</html>

An den wichtigsten Stelle habe ich Kommentare gesetzt damit ihr wisst wie die Funktionen funktionieren.

Leider weiß ich nicht was diesen MySQL-Fehler auslöst in der Funktion insert_mysql.
Könnt ihr mir weiterhelfen?

1 Antwort

0 Punkte
Beantwortet von son_quatsch Experte (5.3k Punkte)
Spalten müssen Namen sein, keine Literale.

Falsch:
INSERT INTO tab( 'col' ) VALUES( 'eins' );

Richtig:
INSERT INTO tab( col ) VALUES( 'eins' );
...