Răsfoiți Sursa

Bugg rättning

Axel Nordh 5 ani în urmă
părinte
comite
99e31d0a92
1 a modificat fișierele cu 20 adăugiri și 14 ștergeri
  1. 20 14
      dbFiles/ReadQuestionsFromFile.php

+ 20 - 14
dbFiles/ReadQuestionsFromFile.php

@@ -15,28 +15,34 @@
 // get Filename, remove ending, this is the category name
 // get Filename, remove ending, this is the category name
 // get file contents 
 // get file contents 
 
 
-if (file_exists($argv[1]) {
+if (file_exists($argv[1])) {
 	$file = fopen($argv[1], "r");
 	$file = fopen($argv[1], "r");
 	$fileinfo = pathinfo($argv[1]);
 	$fileinfo = pathinfo($argv[1]);
 	
 	
 	$categoryName = str_replace("_"," ", $fileinfo['filename']);
 	$categoryName = str_replace("_"," ", $fileinfo['filename']);
 	
 	
-	while ($data = fgetcsv($file, 1000, ",") !== false) {
-		$answer = $data[0];
+	while (($data = fgetcsv($file, 1000, ",")) !== false) {
+		$answer = intval($data[0]);
 		$question = $data[1];
 		$question = $data[1];
 	 
 	 
-		if ($answer == null || $question == null || $answer = "" || $question == "") {
+		if ($answer == null || $question == null || $answer == "" || $question == "") {
 			echo "Failed to get answer and/or question from " . print_r($data,true) .  "\r\n";
 			echo "Failed to get answer and/or question from " . print_r($data,true) .  "\r\n";
 			die();
 			die();
 		}
 		}
 	 
 	 
-		$sql = "INSERT INTO questions (answer, question) 
-			VALUES ($answer, '$question') 
-			ON DUPLICATE KEY UPDATE SET 
-			answer = $answer,
-			id = LAST_INSERT_ID(id)";
-		$conn->querry($sql);
-		$questionId = $conn->insert_id;	 
+		$sql = "SELECT id FROM questions WHERE question = '$question'";
+		$questionId = $conn->query($sql)->fetch_object()->id;
+		
+		if($questionId == null) {
+			$sql = "INSERT INTO questions (answer, question) 
+				VALUES ($answer, '$question') 
+				ON DUPLICATE KEY UPDATE 
+				answer = $answer,
+				id = LAST_INSERT_ID(id)";
+			$conn->query($sql);
+			$questionId = $conn->insert_id;	 
+		}
+		
 		if ($questionId == 0) {
 		if ($questionId == 0) {
 		
 		
 			echo "Nothing insered or updated by sql: \r\n";
 			echo "Nothing insered or updated by sql: \r\n";
@@ -45,7 +51,7 @@ if (file_exists($argv[1]) {
 		}
 		}
 		
 		
 		$sql = "SELECT id FROM category WHERE name = '$categoryName'";
 		$sql = "SELECT id FROM category WHERE name = '$categoryName'";
-		$result = $conn->querry($sql);
+		$result = $conn->query($sql);
 		
 		
 		$categoryId = -1;
 		$categoryId = -1;
 		while ($row = $result->fetch_assoc()) {
 		while ($row = $result->fetch_assoc()) {
@@ -54,12 +60,12 @@ if (file_exists($argv[1]) {
 		
 		
 		if ($categoryId == -1) {
 		if ($categoryId == -1) {
 			$sql = "INSERT INTO category (name, r,g,b,a) VALUES ($categoryName, 255,255,255,255)";
 			$sql = "INSERT INTO category (name, r,g,b,a) VALUES ($categoryName, 255,255,255,255)";
-			$conn->querry($sql);
+			$conn->query($sql);
 			$categoryId = $conn->insert_id;
 			$categoryId = $conn->insert_id;
 		}
 		}
 		
 		
 		$sql = "INSERT INTO questionToCategory (questionId, categoryId) VALUES ($questionId, $categoryId)";
 		$sql = "INSERT INTO questionToCategory (questionId, categoryId) VALUES ($questionId, $categoryId)";
-		$conn->querry($sql);
+		$conn->query($sql);
 		
 		
 		echo "inserted question $question into category $categoryName \r\n";
 		echo "inserted question $question into category $categoryName \r\n";
  }
  }