0

I'm making a table with translation:

English | Italian | French
help    | ayuto   | amo

and am creating a functionality to add new language (Russian). I have it all set up but the import doesn't work correctly. It imports the values under already existing fields. So it doesn't add them directly under the newly added language. Looks like this:

English | Italian | French | Russian(added)
help    | ayuto   | amo    |
        |         |        | помощь(imported)
        |         |        | извините(imported)

I need that to start it from the top like this:

English | Italian | French | Russian(added)
help    | ayuto   | amo    | помощь(imported)
        |         |        | извините(imported)

Here is the code:

    if (isset($_POST['submit'])){
  $lang_name = htmlspecialchars($_POST['lang_name'], ENT_QUOTES);  
  $str = strtolower($lang_name);
  $lang_lawname = str_replace(' ', '_',$str); 


  $sql = "INSERT INTO translation_lang (languages) VALUES ('".$lang_name."')";
  $sql2 = "ALTER TABLE translation ADD $lang_lawname VARCHAR( 255 ) DEFAULT ''";   


    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    if (!mysqli_query($con,$sql2)) {
          die('Error: ' . mysqli_error($con));
        }


    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
        //echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
        //echo "<h2>Displaying contents:</h2>";
        readfile($_FILES['filename']['tmp_name']);

        //Import uploaded file to Database
    $handle = fopen($_FILES['filename']['tmp_name'], "r");

    do {
        if ($data[0]) { 

        $import="INSERT into translation($lang_lawname) values('$data[0]')";

        mysqli_query($con,$import) or die(mysqli_error());
    }
    }
    while ($data = fgetcsv($handle,1000,",","'")); 
    }
Grasper
  • 1,293
  • 12
  • 26
  • 2
    Sidenote: Change `or die(mysql_error());` to `or die(mysqli_error($con));` you can't mix those two APIs. – Funk Forty Niner Jun 13 '14 at 17:18
  • 2
    just wondering in myself, couldn't it be a more efficient to have different languages on separate lines, with a two char field containing the idiom? – Félix Adriyel Gagnon-Grenier Jun 13 '14 at 17:20
  • Also make sure you escape commas in the data. – Prashant Jun 13 '14 at 17:27
  • @FélixGagnon-Grenier - I'm not sure what you meant... – Grasper Jun 13 '14 at 17:28
  • 1
    Sidenote #2: Just a quick note about your translations and to use the proper words before you go any further, because you'll most likely end up having to fix a lot of improperly translated words. I speak a few languages, two of which being italian and french. "Help" in italian is "aiuto" and in french is "aide". Thought you might like to know and save you some potential headaches down the road ;-) – Funk Forty Niner Jun 13 '14 at 17:29
  • 1
    @PrashantBalan, I'm planning not to use commas. Just one column... – Grasper Jun 13 '14 at 17:29
  • @Fred-ii- please ignore the translation, it's just an example. I know it's incorrect... – Grasper Jun 13 '14 at 17:31
  • Ok. Had I known that, I wouldn't have posted my comment. I just call 'em as I see 'em ;-) Cheers (Just trying to help). – Funk Forty Niner Jun 13 '14 at 17:32
  • 1
    `SELECT text WHERE wordid=1726 AND language='fr'` is faster then `SELECT French WHERE wordid=1726`. less internal informations to manage for the server. also, it can scale up if each row contain just one language. what will you do with 10 more languages? – Félix Adriyel Gagnon-Grenier Jun 13 '14 at 17:33
  • @Grasper what Felix meant was to use a table with keyword, lang, translation. This will be scalable in mysql. Save you time if you want to add more languages (no need to add another column using alter table) – Prashant Jun 13 '14 at 17:34
  • I'm not trying to translate a page. I just need a very simple table which list the translations – Grasper Jun 13 '14 at 17:37
  • To get back to the empty row issue. I think you may have an importing problem with unrecognized syntax, or headers above in your `.csv` file. See this Q&A http://stackoverflow.com/q/3635166/ for another method to actually import it in SQL. That page was found after Googling "csv inserts blank line in database mysql". – Funk Forty Niner Jun 13 '14 at 17:37
  • See this other Q&A http://stackoverflow.com/q/8938880/ which may also be of help. – Funk Forty Niner Jun 13 '14 at 17:39
  • @Prashant thanks for clarifying. Reading my comment again I see it can be quite confusing... – Félix Adriyel Gagnon-Grenier Jun 13 '14 at 17:45
  • @FélixGagnon-Grenier Happy to throw some light always. After all, we all went through this sometime or the other. – Prashant Jun 13 '14 at 17:47
  • Sidenote #3: I'd probably make a lookup table rather than having a structure where you keep adding columns for each language. – NinjaCat Jun 13 '14 at 18:15

0 Answers0