使用var_dump()在PHP中显示null

时间:2018-06-22 07:53:24

标签: php

我试图在php中打印数组值。在这里我分享我的代码

upload.php

$postdata = file_get_contents("php://input");

if(isset($postdata)){

    $request = json_decode($postdata, true);

    var_dump($request);

    $artist_name = $request['testname'];

    if($artist_name != ""){
        $con = mysqli_connect("localhost", "root", "upload", "ampache");

        // Check connection
        if(mysqli_connect_errno()){
            // echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        else{
            $sql = "INSERT INTO album_details(artist_name) VALUES('$artist_name')";
            $stmt = mysqli_query($con, $sql) or die ("MySQL Error:".mysqli_error($con));
        }
    }

}

尝试此操作时,我在视图页面上看到了null,同时在控制台网络(响应)中显示:

array(1) {
    ["testname"]=>
    string(7) "vignesh"
   }

如何在视图页面中打印testname

upload.php文件的完整代码

<?php
    header("Access-Control-Allow-Origin: *");

    $connection = mysqli_connect("localhost", "root", "upload", "ampache"); // Establishing

    $postdata = file_get_contents("php://input");

    if(isset($postdata)){
      $request = json_decode($postdata, true);

      echo $postdata;

      $artist_name = $request['testname'];

      if($artist_name != ""){
          $con = mysqli_connect("localhost", "root", "upload", "ampache");

          // Check connection
          if(mysqli_connect_errno()){
              // echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
          else{

              $sql = "INSERT INTO album_details(artist_name) VALUES('$artist_name')";
              $stmt = mysqli_query($con, $sql) or die ("MySQL Error:".mysqli_error($con));
          }
      }

    }

    $catalog = 1;

    $enabled = 1;

    $channels = 2;

    $update_time = 0;

    $user_upload = 1;

    $bitrate = 192000;

    $rate = 44100;

    $mode = "cbr";

    echo $postdata;

    $use = 12;

    if(isset($_POST['submit'])){ // Fetching variables of the form which  travels in URL
        $name = $_POST['name'];

        $query1 = mysqli_query($connection, "insert into album(name) values ('$name')");
        $lastid = mysqli_insert_id($connection);
        $album_id = $lastid;
    }

    if(isset($_FILES['files'])){
        $i = 0;
        foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
            $i++;

            $a = 'C:\\\xampp\\\htdocs\\\ampache-3.8.6_all\\\songs\\\admin\\\ ';

            $b = $name;

            $mp3 = $_FILES['files']['name'][$key];

            $path = "C:/xampp/htdocs/ampache-3.8.6_all/songs/"."/".$name."/";

            $song = $path.$mp3;

            mkdir($path);

            if(move_uploaded_file($tmp_name, "$path/{$_FILES['files']['name'][$key]}")){
                echo "file moved";
            }
            else{
                echo "file not moved";
            }

            $filename = $song;
            $size = filesize($filename);

            $c = "\\\ ".$mp3;

            $file = $a.$b.$c;

            $query = mysqli_query($connection, "insert into song(title, catalog,
               enabled, channels, update_time, user_upload, mode, bitrate, rate, file,
               album, size) values ('$mp3', '$catalog', '$enabled', '$channels',
               '$update_time', '$user_upload', '$mode', '$bitrate', '$rate', '$file',
               '$album_id', '$size')");
        }
    }

   mysqli_close($connection);  // Closing Connection with Server
?>

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <div class="signup-form" style="text-align:center;">
            <form method="post" enctype="multipart/form-data">
                <h2>Upload Songs</h2>
                <p class="hint-text">Please upload your album</p>
                <div class="form-group">
                    <label for='name' class="color">Album Name</label>
                    <input type="text" name="name" class="form-control">
                </div>

                <label for='file' class="color">upload</label>
                <input type="file" name="files[]" multiple="multiple">
                <input type="submit" name="submit" value="submit" style="color:
               black;position: relative;left: 137px;bottom: 24px;">
            </form>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

当您说“查看页面”时,您的测试不够清晰。
您如何将数据发布到脚本中?

我附上了一个代码,用于从html表单发布时显示您的帖子:

<?php
$postdata = !empty($_POST['json']) ? $_POST['json'] : null;
if(!empty($postdata)) {
    $request = json_decode($postdata, true);
    var_dump($request);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form method='POST'>
        <input name='json' value='{"foo": "bar"}'>
        <input type='submit'>
    </form>
</body>
</html>