PHP - 在控制台中发送空值并获取未定义索引

时间:2017-01-06 18:07:55

标签: php

我向php代码发送一个json字符串named-student

$scope.student = {name: "Joe", grades: "85", info: ""};

现在php代码很简单 -

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

事情是,有时“信息”可能是空的,我想把它传递给服务器 - 但我得到了 -

 Undefined index: info

现在我尝试将代码添加到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')";

但似乎没有做这项工作。

那么我做错了什么? 如何将空值传递到表中?

正如你所看到的那样,对于PHP来说我是新手所以任何帮助都会很好

2 个答案:

答案 0 :(得分:1)

redirectTo : String -> Attribute msg redirectTo destinationUrl = attribute "onclick" ("window.location.href = ' ++ destinationURL ++ "'") 不是从帖子

访问数组的正确方法

button 
  [ class "mdl-button", redirectTo "https://google.com" ]
  [ text "Click to leave" ]

$_POST('info')

答案 1 :(得分:1)

希望它会对您有所帮助(请仔细阅读评论(//...)并查看更改): -

$scope.student = [name: "Joe", grades: "85", info: ""]; // `.` from variable name eed to be removed in any manner

现在我假设它: -

$scope = [name: "Joe", grades: "85", info: ""];

现在php代码很简单 -

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