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>