Hello,
I'm a newbie here and quite a newbie and self-learner with php/mysql. I have been trying to make a script to add existing members from one database into another type of database. My idea was to take a CSV file, import it into a new tempusers table in the new database, then copy the necessary fields across to the actual CC users table. I've so far kind of got the first part to work (I think) - but am getting MySQL syntax errors :-( I've spent hours trying to get rid of them ... can anyone please advise me? I feel sure it is a basic error of some kind.
Everything else is working I think except these syntax errors at the point where the data is inserted.
The other part is I need to then make the section to copy from the tempusers table to the users table. If anyone could help with that or at least point me to the right page/technique to do it, I'd be so grateful.
Many thanks, the code is below that I have so far put together.
Kevin
<?
# CC setup file call
include("setup.php");
# email address for results to be sent to
$emailaddress = "testaddress@domain.net";
# subject for email
$subject = "CC data imported";
# from email address
$emailfrom = "anyaddress@domain.net";
# name of table to use in database
$tablename = tempusers;
# Create the table
mysql_query("CREATE TABLE `".mysql_prefix."$tablename` (`email` varchar(64)
NOT NULL default '', `firstnm` varchar(32) NOT NULL default '')");
# empty the table of its current records
$deleterecords = "TRUNCATE TABLE `$tablename`";
mysql_query($deleterecords);
# initialize counter variables etc
$table = $tablename;
$array = file("$tablename.csv");
$length = count($array);
// $i = 0;
$pass = 0;
$fail = 0;
# .csv is added to the table name to get the name of the csv file
$file_handle = fopen ("$tablename.csv", "r");
# process the csv file
while (($row = fgetcsv($file_handle, 1000, "," )) !== false){
# following is a previous method I tried that didnt work either :-(
// $query = "INSERT INTO `$tablename` values(\".$array[$i].\")";
// $result = mysql_query($query);
$insertrecord = "INSERT INTO `$tablename` values(".implode(", ",
$row).")";
mysql_query($insertrecord);
if(mysql_error()) {
echo "ERROR: " . mysql_error() . "<br />\n";
$fail += 1; # increments if there was an error importing the record
}
else
{
$pass += 1; # increments if the record was successfully
imported
}
// $i++;
}
$message .= "Table $tablename: Success=$pass Failure=$fail \n";
# set to the date and time the script was run
$runtime = (date("d M Y H:i"));
# add the run time to the body of the email message
$message .= "\nTime of the message: $runtime (server time zone)\n\n";
# Send the email message
mail($emailaddress, $subject, $message, "From: '$emailfrom'");
?>