phpmyadmin 未定义的数组键,并且不知道该键的来源

e7arh2l6  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(146)

我在跟踪这个,https://www.androidhire.com/insert-data-from-app-to-mysql-android/
并且在步骤“在您的服务器上上传php脚本”中有一个错误,“get_data. php文件的代码”

<?php

include 'DatabaseConfig.php' ;

 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

     $room = $_POST['room'];
     $time = $_POST['time'];

 $Sql_Query = "insert into GetDataTable (room,time) values ('$room','$time')";

 if(mysqli_query($con,$Sql_Query)){

 echo 'Data Submit Successfully';

 }
 else{

 echo 'Try Again';

 }
 mysqli_close($con);
?>

这是我代码,我的数据库表是enter image description here
错误消息为警告:第8行的C:\xampp\htdocs\get_data. php中未定义的数组键“room”
所以我试着

if(isset($_POST['room']){
        $room = $_POST['room'];
    }

但这给了我警告:未定义的变量

kyks70gy

kyks70gy1#

您需要将if()放在使用$room的整个代码块周围:

<?php

include 'DatabaseConfig.php' ;

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

if ( isset( $_POST['room'] ) ) {
   $room = $_POST['room'];
   $time = $_POST['time'];

   $Sql_Query = "insert into GetDataTable (room,time) values ('$room','$time')";

   if(mysqli_query($con,$Sql_Query)){
       echo 'Data Submit Successfully';
   } else{
       echo 'Try Again';
   }
   mysqli_close($con);
}
?>

相关问题