Hi,
I have written a function to import passed information into any database table dependant on the information that is passed to it. All the required information is passed to the function to dynamically create the mysql insert query.
The problem is, I always get the following error:
INSERT INTO Trainer_Details (FName, LName, Phone, AHPhone, Fax, Mobile, Email, Address, Suburb, State, PostCode, WebSite, UserName, Password, AreaServiced) VALUES ('John', 'Noffke', '', '', '', '0402 825 953', 'toolport@interworx.com.au', '', '', 'QLD', '', '', 'Toolport', '5953', 'Rockhampton to Gold Coast Brisbane and Queensland Wide ')
Warning: Supplied argument is not a valid MySQL-Link resource in /var/www1/html/adm/import_data.php on line 56
Could not insert record into database
Matt suggested that $connection had to have the information to connect to the database but I have already checked that and initialize the variable using the following code:
include "mysqlconnectinfo.php";
// Create Connection
$connection = mysql_connect($dbhostname,$dbusername,$dbpassword) or die("Couldn't make connection.");
//Select Database
$db = mysql_select_db("tsoft", $connection) or die("Couldn't select database");
The file mysqlconnectinfo.php initializes the correct values into the variables used in the $connection variable declaration.
I was wondering whether you can tell me how to get around the problem I am having, or if I am doing something wrong. I think the problem might be that the insert query is a variable and not hard coded into the source code:
function import_data($counter,$data_line,$fields,$variable_names,$table_name)
{
$table_fields = "";
$table_field_data = "";
while ($counter < $fields)
{
$table_fields .= $variable_names[$counter] . ", ";
$table_field_data .= "'" . $data_line[$counter] . "', ";
$counter++;
}
$nbr = strlen($table_fields);
$nbr = $nbr - 2;
$table_fields1 = substr($table_fields,0,$nbr);
$nbr = strlen($table_field_data);
$nbr = $nbr - 2;
$table_field_data1 = substr($table_field_data,0,$nbr);
$sql = "INSERT INTO $table_name ($table_fields1) VALUES ($table_field_data1)";
print "$sql";
$sql_result = mysql_query($sql,$connection) or die("Could not insert record into database");
}
Any suggestions would be greatly appreciated, as I have a limited time frame, and need to get this working.
Yours sincerely,
Anthony Irwin
PS: If you can not help me with this, than perhaps you can point me to some documentation that could assist me.