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

图片转代码服务由CSDN问答提供 功能建议

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

  $ scope.student = {name:“Joe”,成绩:“85”,信息:“”}; 
   
 
 

现在php代码是 simple -

 &lt;?php 
 $ servername =“localhost”; 
 $ username =“username”; 
 $ password =“password”; 
 $  dbname =“myDB”; 
 
 //创建连接
 $ conn = new mysqli($ servername,$ username,$ password,$ dbname); 
 //检查连接
if($ conn-&gt; connect_error  ){
 die(“连接失败:”。$ conn-&gt; connect_error); 
} 
 //读取json文件内容
 $ jsondata = file_get_contents(“php:// input”); 
  
 //将json对象转换为php关联数组
 $ data = json_decode($ jsondata,true); 
 
 $ studentname = $ data ['studentname']; 
 $ stuedentgrades = $ data ['stuedentgrades'  ]; 
 $ studentinfo = $ data ['studentinfo']; 
 
 $ sql =“INSERT INTO学生(姓名,成绩,信息)
VALUES('$ studentname','$ stuedentgrades','$ studentinfo'  )“; 
 
如果 ($ conn-&gt; query($ sql)=== TRUE){
 echo“新记录创建成功”; 
} else {
 echo“错误:”。  $ sql。  “&LT峰; br&gt;” 中 。  $ conn-&gt;错误; 
} 
 
 $ conn-&gt; close(); 
?&gt; 
   
 
 

事情有时候 “info”可以为空,我想把它传递给服务器 - 但我得到了 -

 未定义的索引:info 
  <  / pre> 
 
 

现在我尝试在代码中添加isset -

 `if(isset($ _ POST('info'))){  
 
 $ info = $ data ['info']; 
} else {
 echo“NOOOOOOOOOO”; 
}`
 
 
 $ studentname = $ data ['studentname']; 
  $ stuedentgrades = $ data ['stuedentgrades']; 
 $ studentinfo = $ data ['studentinfo']; 
 
 $ sql =“INSERT INTO学生(姓名,成绩,信息)
VALUES('$ studentname',  '$ stuedentgrades','$ studentinfo')“; 
   
 
 

但它似乎没有完成这项工作。

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

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题