I"m sending to a php code a json string named- student
$scope.student = {name: "Joe", grades: "85", info: ""};
Now the php code is simple -
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//read the json file contents
$jsondata = file_get_contents("php://input");
//convert json object to php associative array
$data = json_decode($jsondata, true);
$studentname = $data['studentname'];
$stuedentgrades = $data['stuedentgrades'];
$studentinfo = $data['studentinfo'];
$sql = "INSERT INTO students (name, grades, info)
VALUES ('$studentname', '$stuedentgrades', '$studentinfo')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
The thing is that sometimes the "info" can be empty and i would like to pass it empty to the server - but I'm getting -
Undefined index: info
Now i tried to add to the code the isset -
`if(isset($_POST('info'))){
$info= $data['info'];
}else{
echo "NOOOOOOOOOO";
} `
$studentname = $data['studentname'];
$stuedentgrades = $data['stuedentgrades'];
$studentinfo = $data['studentinfo'];
$sql = "INSERT INTO students (name, grades, info)
VALUES ('$studentname', '$stuedentgrades', '$studentinfo')";
but it didn't seems to do the work.
So what am I doing wrong? How can I pass empty value into the table?
As you can see I'm novice when it comes to PHP so any help would be nice