0

i have tried to fix my this upload code in my 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>

so that is the code for the form page and here is the code for the POST method :

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

}
?>

sometimes this works normally, so i got all of my data and my files uploaded to the mysql, BUT sometimes it just give me the PHP Notice of PHP Undefined Index for ALL OF my data including Price, Image, Stock etc.

I have no idea at all why could this happen, can anyone help me with this one?

EDIT :i did fill in all of the form data, but when i POST it, sometimes it still shows undefined index , the problem is that, if it shows UNDEFINED INDEX doesn't it mean that all of my data is not sent to the mysql? Because is did fill all of the data in my form, the problem is that in the POST script it told me that all of the data is NULL (Undefined index)

if i only uploaded 2-3 pictures the code is working, but if i uploaded 5 pictures then the undefined index error shows up

Jagitsha
  • 1
  • 2
  • Possible duplicate [notice-undefined-variable-notice-undefined-index-and-notice-undefined](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – lovelace Oct 16 '19 at 07:45
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Carl Binalla Oct 16 '19 at 07:46
  • Well, if you open your **POST method** page, a lot of that error will be thrown, as all of them are not set yet. Try putting it inside a condition, like `if(isset($_POST['submit'])){}` – Carl Binalla Oct 16 '19 at 07:47
  • This is the common mistake use `isset()` in if condition –  Oct 16 '19 at 07:47
  • it's different, i did fill in all of the form data, but when i POST it, sometimes it still shows undefined index – Jagitsha Oct 16 '19 at 07:47
  • where do i need to put isset()? – Jagitsha Oct 16 '19 at 07:48
  • Before start your code –  Oct 16 '19 at 07:48
  • the problem is that, if it shows UNDEFINED INDEX doesn't it mean that all of my data is not sent to the mysql? Because is did fill all of the data in my form, the problem is that in the POST script it told me that all of the data is NULL (Undefined index) – Jagitsha Oct 16 '19 at 07:51
  • if i only uploaded 2-3 pictures the code is working, but if i uploaded 5 pictures then the undefined index error shows up – Jagitsha Oct 16 '19 at 08:04

2 Answers2

0

I am supposing that you have to check if form is submiited or not.

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

}
}
Jaymin
  • 1,643
  • 1
  • 18
  • 39
0

Do this format to check if the variables have been set.

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

if (isset($_POST['name']))
{
    $item = $_POST['name'])
}
// do the rest
Two
  • 512
  • 4
  • 17