PHP-mysqli-Database with json: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I know that there are already requests for the error message "SyntaxError: JSON.parse: unexpected character at line 1 of the JSON data", which I have read through. Unfortunately, I have still not been able to fix this error in my code, although I thought to have removed any HTML code. Has anyone of you an idea? I am grateful for every tip. Please do not be surprised, there are other files for this PHP file, which is why neither database nor data is displayed in this database.

(Hallo zusammen, ich weiß, dass es für die Fehlermeldung "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data" schon Anfragen gibt, die ich mir durchgelesen habe. Leider habe ich diesen Fehler in meinem Code bisher immer noch nicht beheben können, obwohl ich dachte, jeglichen HTML-Code entfernt zu haben. Hat jemand von euch vielleicht eine Idee, oder findet gar den Fehler? Bitte nicht wundern, zu dieser PHP Datei gehören noch weitere Dateien, weshalb hier in dieser Datenbank "students" weder Daten eingetragen noch in einer Tabelle angezeigt werden.)

Code:

    <?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);

}

} ?>

1 answer

  • answered 2017-06-17 18:28 InfiniteStack

    This almost certainly happens because of:

    1. malformed JSON, and/or
    2. empty string returned instead of JSON

    One way to start debugging this is... to do something like:

    if (json_received =="") json_received = "{}";
    

    This provides a temporary lowest common denominator JSON value of {} (empty object)

    If it starts working you will be 100% sure it's malformed JSON.

    Other things to consider:

    1. Is your MySQL database format set to UTF-8?
    2. Have you tried logging $responseArray before you passing it to JSON parsing/encoding function?