Jump to content

Upload CSV to Mysql database


Peg

Recommended Posts

I have a bit of code that I have used for years to upload to upoad data from a csv file to a database. I built a website on a Godaddy Server, then after the site was tested and approved, the site was moved another server. I do not have any access to the new server and the code does not work on the new server. I'm sure that it has to do with security issues on the server?

 

The first part of the code that truncates the table did work. So I know that the connection to the database is correct.

 

Here is the code:

 

<?php
$sql3 = "TRUNCATE TABLE this_table";
$result3 = mysql_query($sql3,$db) or die(mysql_error(). "<br />SQL: $sql3");
ini_set('auto_detect_line_endings',true);
if (($handle = fopen($csvfile, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                $sql = "INSERT INTO this_table (first_name,last_name,title,field_1,field_2,field_3,field_4) VALUES(";
                $search = array(" & ",'"',"'",",");
                $replace = array(" & ",'"', '’',",");
        
                for ($c=0; $c < $num; $c++) {
                        $sql .= "'". str_replace($search,$replace,$data[$c]) . "',";                    
                }
                $sql = substr($sql, 0, -1) .")";
                $result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql");    
        }
        fclose($handle);
}
ini_set('auto_detect_line_endings',false);
?>

Link to comment
Share on other sites

the code does not work

 

 

sorry, but that's useless information. by not telling us what it does do (even telling us it resulted in a blank page is a clue that helps narrow down the problem), we cannot help you with the many possible things that could go wrong with any piece of code.

 

the only apparent issue with the posted code is it doesn't have any logic in it to tell you if the fopen() statement has failed.

Link to comment
Share on other sites

Does anyone know if there is a difference between the way this code would work on a windows server and how it works on a Lynx. The code worked great on the Godaddy lynx server, but does not work on the current server that is a windows server.

Link to comment
Share on other sites

The page does not go blank. The code does not produce any php errors when the csv is uploaded to the site. I say this because the page loads completely.

Does that help any? Sorry, this is difficult for me because I do not have access to the server. I can not ftp into the site.

Link to comment
Share on other sites

Maybe it is this code that is not working on their server. I might assume that since it is a server with security on it, that the temporary files are accessed differently. Does that make sense?

 

$csvfile = $_FILES['userfile']['tmp_name'];
$size = $_FILES['userfile']['size'];
 

Link to comment
Share on other sites

Well, you could test for the existence of the csv file before you try to open it to see if that's the issue. As mac_gyver intimated, you could also output an error if the fopen fails. That might narrow things down for you.

Link to comment
Share on other sites

Yes, that makes sence to me. I do not however have ftp access to the site. I will have to wait until Tuesday when my clients tech is in. Then have him upload that code to the server. This makes it difficult to trouble shoot!

Thank You!

Link to comment
Share on other sites

  • 4 weeks later...

@Peg,
 
I'm with GoDaddy and came across your post.
 
Are you still running into the issues you were experiencing with your code? Additionally, you mentioned the site was built on a Linux Server with GoDaddy, is the Windows Server also with us?
 
If you still are running into issues feel free to reply or send me a private message.

Link to comment
Share on other sites

  • 3 weeks later...

Thank you Ken. No the Windows server is not with GoDaddy. The server is "on site" in my clients building. That is actually all I know, besides the fact that there is protected information on the server.

 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.