dorflv5944 2017-01-06 10: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 10: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部