2016-08-04 26 views
0

Ich habe ein Problem mit mySQL Ich erhalte diese FehlerFehler: INSERT INTO nicht mysql

Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33 
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES (pinocchio , 1 ,-1) 

Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33 
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES (alice's adventures in wonderland , 2 ,-1) 

Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33 
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES (peter pan , 3 ,-1) 

Notice: Undefined property: PDO::$error in C:\MAMP\htdocs\paresFiles.php on line 33 
Error: INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES (snow white , 4 ,-1) 

I in Werte mysql 3 meiner Suchtabelle einfügen möchten, und wenn ich sie einsetzen bekomme ich diesen Fehler und didn‘ t irgendwelche Daten in meiner Tabelle erhalten. Was muss ich tun, um diesen Fehler zu beheben?

dies ist mein Code (für Insert-Daten):

include_once 'connect.php'; 

$dir = "storage"; 
$title; 
$auther; 
$fileNum; 
// Open a directory, and read its contents 
foreach (glob($dir."/*.txt") as $filename) { 
    $handle = fopen($filename, "r"); 
    if ($handle) { 
    while (($line = fgets($handle)) !== false) { 
     if(substr($line, 0, 3) === "#id") 
     { 
      $fileNum=substr($line, 5, 3); 
     } 
     if(substr($line, 0, 6) === "#title") 
     { 
      $title=substr($line, 8, 100); 
     } 
     if(substr($line, 0, 5) === "#name") 
     { 
      $auther=substr($line, 8, 12); 
     } 
    } 
    $titleVal=-1; 

    $sql = "INSERT INTO `searchtable`(`word`, `array`, `count`) VALUES  ($title,$fileNum,$titleVal)"; 

     if ($db->query($sql) === TRUE) { 
      echo "New record created successfully"; 
     } else { 
      echo "Error: " . $sql . "<br>" . $db->error; 
     } 
    fclose($handle); 
} 

zu mysql verbinden:

<?php 
    $db = new PDO('mysql:host=localhost;dbname=search;charset=utf8mb4', 'root', 'root'); 
    if (!$db) { 
     die('Could not connect:' . mysql_error()); 
    } 
    echo 'Connected successfully'; 
?> 
+1

fehlenden Anführungszeichen in Werten. Bessere Vorbereitung vorbereiten !! – Saty

+0

wie folgt: $ sql = "INSERT in Suchtabelle (" Wort "," Array "," Anzahl ") VALUES ($ title, $ fileNum, $ titleVal)"; ? @Saty – user3488862

+0

lesen Sie http://php.net/manual/en/pdo.prepared-statements.php – Saty

Antwort

2

sollten Sie Anweisungen vorbereiten verwenden.

so etwas wie diese

$sql = "INSERT INTO searchtable (word, array, count) VALUES (:word , :array, :count)"; 

$statement = $db->prepare($sql); 
$statement->exec(array(
    'word' => $title, 
    'array' => $fileNum, 
    'count' => $titleVal 
)); 
+0

es schreibt mich Fataler Fehler: Uncaught Fehler: Aufruf zu undefined Methode PDOStorement :: exec() in C: \ MAMP \ htdocs \ paresFiles.php: 31 Stack-Trace: # 0 {main} geworfen in C: \ MAMP \ htdocs \ paresFiles.php in Zeile 31 – user3488862

+0

Müssen 'exec' zu ändern 'execute' – Saty

+0

@Saty ok Ich habe es geändert, aber jetzt habe ich einen neuen Fehler: Hinweis: Array zu string Umwandlung in C: \ MAMP \ htdocs \ paresFiles.php in Zeile 40 Fehler: INSERT INTO Suchtabelle (Wort, Position, Anzahl) VALUES (: Wort,: Position,: Anzahl) Array – user3488862