2014-05-08 23 views
5

Im new to php. Sto cercando di leggere un file di testo e inserire dati riga per riga nel database. Il mio problema è per alcune query di inserimento di caratteri speciali non funziona
Ad esempio Côte, d.ä., d.y., DAB-sändare queste funzionano tutte. Ma non posso inserire d'affaires. Se rimuovo d'affaires la query verrà eseguita altrimenti non inserirà alcun dato nel database. Il codice php che ho usato per reaf e inserire al database è

I caratteri speciali non possono essere inseriti nel database

mysql_connect("localhost","root",""); 
    mysql_select_db("testdb"); 
    $query="INSERT INTO keywords (id, keyword) VALUES "; 
    $handle = fopen("Ordlista.txt", "r"); 
    if ($handle) { 
     $i=1; 
     while (($line = fgets($handle)) !== false) { 
      // process the line read. 
      // echo $line.'<br>'; 

      if($i==1) 
      { 
      $query.=" (NULL , '".$line."') "; 
      $i++; 
      } 
      else { 
       $query.=" ,(NULL , '".$line."') "; 
      } 

     } 
     $query.=";"; 
    // $qr=htmlspecialchars($query,ENT_QUOTES); 
     echo $query; 
     mysql_query($query); 
    } else { 
     echo 'error opening the file.'; 
     // error opening the file. 
    } 
    fclose($handle); 

AGGIORNATO
Ho usato questo codice durante la creazione di un plug-in WordPress allora i caratteri speciali stanno inserendo come '?'. Nel codice precedente è stato file di lavoro la modifica del codice ho fatto è

mysql_query("TRUNCATE TABLE $table"); 
// $structure = "INSERT INTO $table (`id`, `keyword`) VALUES (NULL, 'test1'), (NULL, 'test2');"; // Keywords for Testing 
// $wpdb->query($structure); 
    //read text file & insert to database start 
    $query="INSERT INTO $table (id, keyword) VALUES "; 
    $fllocation=PLG_URL.'/Ordlista.txt'; 
    $handle = fopen($fllocation, "r"); 
    if ($handle) { 
     $i=1; 
     while (($line = fgets($handle)) !== false) { 
      // process the line read. 
      if($i==1) 
      { 
      $query.=" (NULL , '".mysql_real_escape_string($line)."') "; 
      $i++; 
      } 
      else { 
       $query.=" ,(NULL , '".mysql_real_escape_string($line)."') "; 
      } 
     } 
     $query.=";"; 
     $wpdb->query($query); 
     // echo $query; 
     // mysql_query($query); 
    } else { 
     echo 'error opening the file.'; 
     // error opening the file. 
    } 
    fclose($handle); 
+1

prova mysql_real_escape_string() –

+1

[Perché non dovrei usare le funzioni mysql_ * in PHP?] (http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil

+0

Dopo aver letto il tuo aggiornamento: qual è il tuo charset e il tuo fascicolo tavolo? – Sal00m

risposta

4

Try mysql_real_escape_string();

mysql_connect("localhost","root",""); 
    mysql_select_db("testdb"); 
    $query="INSERT INTO keywords (id, keyword) VALUES "; 
    $handle = fopen("Ordlista.txt", "r"); 
    if ($handle) { 
     $i=1; 
     while (($line = fgets($handle)) !== false) { 
      // process the line read. 
      // echo $line.'<br>'; 

      if($i==1) 
      { 
      $query.=" (NULL , '".mysql_real_escape_string($line)."') "; 
      $i++; 
      } 
      else { 
       $query.=" ,(NULL , '".mysql_real_escape_string($line)."') "; 
      } 

     } 
     $query.=";"; 
    // $qr=htmlspecialchars($query,ENT_QUOTES); 
     echo $query; 
     mysql_query($query); 
    } else { 
     echo 'error opening the file.'; 
     // error opening the file. 
    } 
    fclose($handle); 
+0

se il problema è risolto, per favore non dimenticare di accettare questa domanda come risposta :) –

3

La soluzione migliore sarebbe quella di aggiornare da mysql_* a DOP o mysqli_*, in quanto questi consentono di eseguire query preparate con i parametri. Ma se non si può fare, si deve sfuggire i dati:

while (($line = fgets($handle)) !== false) { 
     // process the line read. 
     // echo $line.'<br>'; 
     $line = mysql_real_escape_string($line); 

     if($i==1) 
     { 
     $query.=" (NULL , '".$line."') "; 
     $i++; 
     } 
     else { 
      $query.=" ,(NULL , '".$line."') "; 
     } 

    } 
3

In primo luogo, non utilizzare il mysql estensione. È stato ufficialmente deprecato.

In secondo luogo, utilizzare una dichiarazione preparata con parametri per evitare qualsiasi problema con l'iniezione SQL.

In terzo luogo, assicurarsi di utilizzare una connessione compatibile, codifica di tabella e colonna/set di caratteri.

Ad esempio, utilizzando mysqli ...

$con = new mysqli('localhost', 'root', '', 'testdb'); 
if ($con->connect_errno) { 
    throw new Exception($con->connect_error, $con->connect_errno); 
} 
$con->set_charset('utf8'); 

$stmt = $con->prepare('INSERT INTO `keywords` (`keyword`) VALUES (?)'); 
if (!$stmt) { 
    throw new Exception($con->error, $con->errno); 
} 
$stmt->bind_param('s', $keyword); 

foreach (file('Ordlista.txt') as $keyword) { 
    if (!$stmt->execute()) { 
     throw new Exception($stmt->error, $stmt->errno); 
    } 
} 
+0

+1 per l'esempio usando mysqli invece delle funzioni mysql_ * deprecate – Sal00m

1

Dopo aver letto l'aggiornamento, penso che il problema è con la fascicolazione e set di caratteri della tabella, eseguire questo:

ALTER TABLE `keywords` CHARACTER SET = utf8 , COLLATE = utf8_unicode_ci ; 
+0

sorry not working – Salini

Problemi correlati