dorflv5944 2017-01-06 18:07
浏览 106
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • doushi6932 2017-01-06 18:36
    关注

    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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程