2017-06-17 8 views
0

「SyntaxError:JSON.parse: 1つのJSONデータ "を参照してください。残念ながら、私はまだ私のコードでこのエラーを修正することはできませんでしたが、私はHTMLコードを削除したと考えました。誰かあなたのアイデアはありますか?私はあらゆるヒントに感謝しています。 このPHPファイルには他にもファイルがあります。そのため、このデータベースにはデータベースもデータも表示されません。JSONを使用したPHP-mysqli-Database:SyntaxError:JSON.parse:JSONデータの1行目のカラム1の予期しない文字

(ハローのツザンメンは、 ICHワイス、DASS ESのfürFehlermeldung死ぬショーンAnfragen gibt "にSyntaxError:JSON.parse JSONデータの行1列1に予想外の文字を"、ICHのmiR durchgelesenのhabeダイLeider habeのICHのdiesen。 Fehler in meinem HTMLコードを記述するためのコードは、HTMLコードを記述するために使用されています。帽子は、アイデアで見つかるか、見つかったかを確認するために使用します。 PHPの日付、時刻、現地日時、dieser Datenbankでweshalbののhier "学生" einer Tabelleのangezeigtのwerdenでweder DATEN eingetragen NOCH)

コード:

<?php 
//Fehlermeldung: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 
header('Content-Type: application/json'); 

$host = 'localhost'; 
$user = 'root'; 
$password = ''; 
$database = 'mmn1617'; 

$c = mysqli_connect($host, $user, $password, $database); 

/* 
* here's a query that creates a table "students" if it does not exist already. 
* The table contains a PersonID, the primary key which will be automatically incremented 
*/ 
$createstudentsQuery = "CREATE TABLE IF NOT EXISTS students(
    ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    firstName VARCHAR(255) NOT NULL, 
    lastName VARCHAR(255) NOT NULL, 
    semester VARCHAR(255) NOT NULL, 
    course VARCHAR(255) NOT NULL, 
    grade DOUBLE NOT NULL 
)"; 

//we execute the query here, the result is returned to the $result 
$result = mysqli_query($c, $createstudentsQuery); 

/*result will be "true" if the query was successful 
Note: The query doesn't contain the PersonID, because 
it will be automatically created by MySQL*/ 

if (!$result) { 
    //the $createstudentsQuery/selectStudentsQuery somehow failed. 
    echo "Could not create students table"; 
} else{ 
    echo "Students table ist working."; 
} 

//array missingParameters wird angelegt 
$missingParameters = array(); 

//hier wird überprüft ob etwas gepostet wurde 
if(isset($_POST['firstName'])&&isset($_POST['lastName'])&&isset($_POST['semester'])&&isset($_POST['course'])&&isset($_POST['grade'])){ 

//wenn inputs nicht leer sind, wird die Eingabe einer Variable übergeben 
if (!empty($_POST['firstName'])){ 
    $firstName = $_POST['firstName']; 
}else{ 
    $missingParameters [] = 'firstName'; 
}if (!empty($_POST['lastName'])){ 
     $lastName = $_POST['lastName']; 
}else{ 
    $missingParameters [] = 'lastName'; 
} 
if (!empty($_POST['semester'])){ 
    $semester = $_POST['semester']; 
}else{ 
    $missingParameters [] = 'semester'; 
} 
if (!empty($_POST['course'])){ 
    $course = $_POST['course']; 
}else{ 
    $missingParameters [] = 'course'; 
} 
if (!empty($_POST['grade'])){ 
    $grade = $_POST['grade']; 
}else{ 
    $missingParameters [] = 'grade'; 
} 

/**wenn das array missingParamater nicht leer ist, also eine Eingabe fehlt*/ 
if (!empty($missingParameters)) { 
    echo "Please enter an information."; 
    //array missingParameters wird in der Variable responseArray gespeichert 
    $responseArray = array("missing parameters"=>$missingParameters); 
    //und mit der Methode json_encode als json ausgegeben 
    echo json_encode($responseArray); 

/**wenn die Eingaben vollständig sind, werden die Werte in der Tabelle gespeichert*/ 
} else { 
    $insertstudentsQuery = "INSERT INTO students(firstName, lastName, semester, course, grade) 
    VALUES('$firstName', '$lastName', '$semester', '$course', '$grade')"; 

    $insertResult = mysqli_query($c, $insertstudentsQuery); 

    //This tells the client that the data was successfully inserted. 
    $responseArray = array("message"=>"OK"); 
    echo json_encode($responseArray); 

} 

} >

答えて

0

これはほぼ確実のために行われます?

  1. 不正な形式のJSONを、および/またはから
  2. 空の文字列が

を返さ代わりにJSON一つの方法

0のようにするには
をデバッグしてください。
if (json_received =="") json_received = "{}"; 

これは、作業を開始した場合、あなたはそれが不正な形式のJSONだ100%確実である(空のオブジェクト)

{}の一時的な最小公分母JSON値を提供します。考慮すべき

他のもの:UTF-8に設定してMySQLデータベース形式は

  1. ですか?
  2. $ responseArrayをJSONの解析/エンコード関数に渡す前にログを試しましたか?
+0

こんにちはInfiniteStack、あなたの答えをありがとう!私は "エコー"を削除しなければならないことを知りました。今はJsonがうまくいきます。ご協力いただきありがとうございます! – MiHei

+0

よくお知りになりたい:) JSON形式の出力が期待されているようですが、通常の文字列のテキストがそこにあります。 – InfiniteStack

関連する問題