dongshi6844 2018-06-22 07:53
浏览 348

使用var_dump()在php中显示null

I trying to print array value in php.Here i share my codes

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));
        }
    }

}

when I tried this, I got null in view page,at the same time in console network(Response) it shows :

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

How to print testname in view page

Full Code of upload.php file

<?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条回答 默认 最新

  • dsbfbz75185 2018-06-22 08:17
    关注

    When you say "view page", your test is not clear enough.
    How are you posting the data to the script?

    I've attached a code that displays your post when posting from html form:

    <?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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误