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,",","'")); 
    }
16
  • 2
    Sidenote: Change or die(mysql_error()); to or die(mysqli_error($con)); you can't mix those two APIs. Commented Jun 13, 2014 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? Commented Jun 13, 2014 at 17:20
  • 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 ;-) Commented Jun 13, 2014 at 17:29
  • 1
    @PrashantBalan, I'm planning not to use commas. Just one column... Commented Jun 13, 2014 at 17:29
  • 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? Commented Jun 13, 2014 at 17:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.