这个POST方法怎么了?

时间:2019-10-16 07:43:58

标签: php post methods

我试图在php中修复此上传代码:

<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>  
<?php 
$nama=$_SESSION["username"];
if (($nama)!='Admin'){
echo "<script>
     window.location = '/home'
     </script>";
}
?>

<h2>Upload Item Panel</h2>
<p><span class="error">*Semua harus diisi</span></p>
<form method="post" action="listingupload.php/" enctype="multipart/form-data">  
  Nama Barang: <input type="text" name="name" value="">
  <br><br>
  Stock    :<input type="text" name="stock" value="">
  <br><br>
  Warna: <input type="text" name="warna" value="">
  <br><br>
  RAM: <input type="text" name="ram" value="">
  <br><br>
  kondisi: <input type="text" name="kondisi" value="">
  <br><br>
  Harga: <input type="text" name="harga" value="">
  <br>
 <b>Harap mengisi kolom harga tanpa titik (Dua juta rupiah = 2000000)</b>
  <br><br>
  Harga Sebelum diskon: <input type="text" name="hargabefore" value="">
  <br><br>
  Deskripsi: <textarea name="deskripsi" rows="5" cols="40"></textarea>
  <br><br>
    Gambar Listingan:<br><br>
    <input type="file" name="pic"><br><br>
    Gambar Detail 1:<br><br>
    <input type="file" name="pic2"><br><br>
    Gambar Detail 2:<br><br>
    <input type="file" name="pic3"><br><br>
    Gambar Detail 3:<br><br>
    <input type="file" name="pic4"><br><br>
    Gambar Detail 4:<br><br>
    <input type="file" name="pic5"><br><br>
    Gambar Detail 5:<br><br>
    <input type="file" name="pic6"><br><br>
    <input type="submit" value="POST" name="submit">
</form>

</body>
</html>

这就是表单页面的代码 这是POST方法的代码:

<?php
$namafile = $_FILES['pic']['name'];
$namafile2 = $_FILES['pic2']['name']; 
$namafile3 = $_FILES['pic3']['name'];  
$namafile4 = $_FILES['pic4']['name'];  
$namafile5 = $_FILES['pic5']['name'];  
$namafile6 = $_FILES['pic6']['name'];  
$ukuran = $_FILES['pic']['size'];  
$error = $_FILES['pic']['error']; 


$ukuran2 = $_FILES['pic2']['size'];  
$error2 = $_FILES['pic2']['error']; 

$ukuran3 = $_FILES['pic3']['size'];  
$error3 = $_FILES['pic3']['error']; 

$ukuran4 = $_FILES['pic4']['size'];  
$error4 = $_FILES['pic4']['error']; 

$ukuran5 = $_FILES['pic5']['size'];  
$error5 = $_FILES['pic5']['error']; 

$ukuran6 = $_FILES['pic6']['size'];  
$error6 = $_FILES['pic6']['error']; 

include 'mysqldata.php';

$item=$_POST['name'];
$deskripsi=$_POST['deskripsi'];
$price=$_POST['harga'];
$stock=$_POST['stock'];
$ram=$_POST['ram'];
$warna=$_POST['warna'];
$kondisi=$_POST['kondisi'];
$hargabefore=$_POST['hargabefore'];


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn= new mysqli($host,$dbUsername,$dbPassword,$dbname);
if (mysqli_connect_error()){
    echo mysqli_errno($this->db_link);
    die('Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
} else{
echo "Redirecting....\n";
if (move_uploaded_file($_FILES['pic']['tmp_name'],'listing/'.$namafile)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic2']['tmp_name'],'listing/'.$namafile2)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic3']['tmp_name'],'listing/'.$namafile3)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic4']['tmp_name'],'listing/'.$namafile4)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic5']['tmp_name'],'listing/'.$namafile5)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic6']['tmp_name'],'listing/'.$namafile6)){
        echo "Success";
}else{
echo "Failed";
}
$sql = "INSERT INTO $dbname.itemlist(Itemname, Description,Harga,Image,stock,picture,picture2,picture3,picture4,picture5,warna,ram,kondisi,pseudoprice) VALUES ('$item','$deskripsi','$price','$namafile','$stock','$namafile2','$namafile3','$namafile4','$namafile5','$namafile6','$warna','$ram','$kondisi','$hargabefore')";
$conn->query($sql);

}
?>

有时这可以正常工作,所以我将所有数据和文件上传到了mysql,但有时它只是给我有关所有数据(包括价格,图像,库存)的PHP未定义索引的PHP通知等等

我完全不知道为什么会发生这种情况,有人可以帮助我吗?

EDIT:我确实填写了所有表单数据,但是当我发布它时,有时它仍然显示未定义的索引,问题是,如果它显示UNDEFINED INDEX并不意味着我的所有数据都没有发送到mysql?因为确实确实填写了我的表单中的所有数据,所以问题在于在POST脚本中它告诉我所有数据都是NULL(未定义索引)

如果我只上传了2-3张图片,则代码有效,但是如果我上传了5张图片,则会显示未定义的索引错误

2 个答案:

答案 0 :(得分:0)

我想您必须检查表格是否被送达。

if(!empty($_POST)  && !empty($_FILES))
{
$namafile = $_FILES['pic']['name'];
$namafile2 = $_FILES['pic2']['name']; 
$namafile3 = $_FILES['pic3']['name'];  
$namafile4 = $_FILES['pic4']['name'];  
$namafile5 = $_FILES['pic5']['name'];  
$namafile6 = $_FILES['pic6']['name'];  
$ukuran = $_FILES['pic']['size'];  
$error = $_FILES['pic']['error']; 


$ukuran2 = $_FILES['pic2']['size'];  
$error2 = $_FILES['pic2']['error']; 

$ukuran3 = $_FILES['pic3']['size'];  
$error3 = $_FILES['pic3']['error']; 

$ukuran4 = $_FILES['pic4']['size'];  
$error4 = $_FILES['pic4']['error']; 

$ukuran5 = $_FILES['pic5']['size'];  
$error5 = $_FILES['pic5']['error']; 

$ukuran6 = $_FILES['pic6']['size'];  
$error6 = $_FILES['pic6']['error']; 

include 'mysqldata.php';

$item=$_POST['name'];
$deskripsi=$_POST['deskripsi'];
$price=$_POST['harga'];
$stock=$_POST['stock'];
$ram=$_POST['ram'];
$warna=$_POST['warna'];
$kondisi=$_POST['kondisi'];
$hargabefore=$_POST['hargabefore'];


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn= new mysqli($host,$dbUsername,$dbPassword,$dbname);
if (mysqli_connect_error()){
    echo mysqli_errno($this->db_link);
    die('Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
} else{
echo "Redirecting....\n";
if (move_uploaded_file($_FILES['pic']['tmp_name'],'listing/'.$namafile)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic2']['tmp_name'],'listing/'.$namafile2)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic3']['tmp_name'],'listing/'.$namafile3)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic4']['tmp_name'],'listing/'.$namafile4)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic5']['tmp_name'],'listing/'.$namafile5)){
        echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic6']['tmp_name'],'listing/'.$namafile6)){
        echo "Success";
}else{
echo "Failed";
}
$sql = "INSERT INTO $dbname.itemlist(Itemname, Description,Harga,Image,stock,picture,picture2,picture3,picture4,picture5,warna,ram,kondisi,pseudoprice) VALUES ('$item','$deskripsi','$price','$namafile','$stock','$namafile2','$namafile3','$namafile4','$namafile5','$namafile6','$warna','$ram','$kondisi','$hargabefore')";
$conn->query($sql);

}
}

答案 1 :(得分:0)

采用这种格式检查变量是否已设置。

if (isset($_FILES['pic']['name']))
{
   $namafile = $_FILES['pic']['name'];
}

if (isset($_POST['name']))
{
    $item = $_POST['name'])
}
// do the rest