Reputation: 759
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
Upvotes: 1
Views: 367
Reputation: 72269
Hope it will help you (please read comments (//...
) carefully and check changes):-
$scope.student = [name: "Joe", grades: "85", info: ""]; // `.` from variable name eed to be removed in any manner
Now i assume it's:-
$scope = [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);
// here i assume that $data is exactly equals what you shown above that means $data = [name: "Joe", grades: "85", info: ""];
//Now change here:-
$studentname = (!empty($data['name']))? $data['name'] : ""; //check change here
$stuedentgrades = (!empty($data['grades']))? $data['grades'] : "";
$studentinfo = (!empty($data['info']))? $data['info'] : "Nooooo";
$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();
?>
Upvotes: 1
Reputation: 12885
$_POST('info')
is not the correct way to access an array from post
try
if(isset($_POST['info'])){
$info= $_POST['info'];
}else{
echo "NOOOOOOOOOO";
}
or
if(isset($data['student']['info'])){
$info= $data['student']['info'];
}else{
echo "NOOOOOOOOOO";
}
Upvotes: 1