Can anyone see whats wrong with this as some of my users are getting errors?

Ok, I have a bit of code and it works fine for most people but some are complaining that its not uploading and i can’t see what is wrong with my code so could someone else with another set of eyes let me know if im doing something wrong!

Thanks

AJAX-UPLOAD.PHP

<?php
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
require_once('../system/config-admin.php');
if(!preg_match('/^([1-9][0-9]*|0)(\.[0-9]{2})?$/', $_POST['price']))
{
  echo '<span class="text-danger">You need to put a price</span>';
  die();
}
if(empty($_POST['name']))
{
  echo '<span class="text-danger">Product Name is required!</span>';
  die();
}
if(empty($_POST['sdesc']))
{
  echo '<span class="text-danger">Short Description is required.</span>';
  die();
}
if(empty($_POST['description']))
{
  echo '<span class="text-danger">Description is required.</span>';
  die();
}
if(empty($_POST['cat_id']))
{
  echo '<span class="text-danger">Category is required.</span>';
  die();
}
if($_FILES['previewimgfile']['size'] < 1)
{
echo 'The previewimgfile file needs to be uploaded';
die();
}

if(isset($_FILES['previewimgfile'])){
  $image_name = $_FILES['previewimgfile']['name'];
  $image_name = preg_replace("/[^a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ.\']/", "", $image_name);
  $tmp_name   = $_FILES['previewimgfile']['tmp_name'];
  $image_type = $_FILES['previewimgfile']['type'];
  $image_size = $_FILES['previewimgfile']['size'];
  $allowed_image = array('image/png', 'image/PNG', 'image/jpeg', 'image/JPEG', 'image/GIF', 'image/gif');
  if(!in_array($image_type, $allowed_image)){
    echo '<span class="text-danger">Please select a jpg/png/gif for preview img!</span>';
    die();
  }
  else
  {
      if($image_size > 5767168){
          echo'file too big';
          die();
      }
      else{
    $new_image_name = time().$image_name;
    move_uploaded_file($tmp_name, '../system/assets/uploads/products/'.$new_image_name.'');
    }
  }
}
if(isset($_FILES['iconimgfile'])){
  $aimage_name = $_FILES['iconimgfile']['name'];
  $aimage_name = preg_replace("/[^a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ.\']/", "", $aimage_name);
  $tmp_name   = $_FILES['iconimgfile']['tmp_name'];
  $aimage_type = $_FILES['iconimgfile']['type'];
  $aimage_size = $_FILES['iconimgfile']['size'];
  $aallowed_image = array('image/png', 'image/PNG', 'image/jpeg', 'image/JPEG', 'image/GIF', 'image/gif');
  if(!in_array($aimage_type, $aallowed_image)){
    echo '<span class="text-danger">Please select a jpg/png/gif for icon img!</span>';
    die();
  }
  else
  {
      if($aimage_size > 5767168){
          echo'file too big';
          die();
      }
      else{
    $new_image_name1 = time().$aimage_name;
    move_uploaded_file($tmp_name, '../system/assets/uploads/products/'.$new_image_name1.'');
    }
  }
}
if(isset($_FILES['mainfile'])){
  $name = $_FILES['mainfile']['name'];
  $name = preg_replace("/[^a-zA-Z0-9áéíóúüñÁÉÍÓÚÜÑ.\']/", "", $name);
  $tmp_name   = $_FILES['mainfile']['tmp_name'];
  $file_size = $_FILES['mainfile']['size'];
if($file_size < 1){
    echo 'No file selcted try again!';
    die();
}
  $file_type = pathinfo($name);
  $file_type = $file_type['extension'];
  $allowed_file = array('zip','jpeg','txt');
  if(!in_array($file_type, $allowed_file)){
    echo '<span class="text-danger">Please select a .zip file for the main file</span>';
  }
  else
  {
    $new_file_name = time().$name;
    move_uploaded_file(''.$tmp_name.'', '../system/assets/uploads/product-files/'.$new_file_name.'');
}
  }


$name2 = $_POST['name'];
$sdesc = $_POST['sdesc'];
$description = $_POST['description'];
$cat_id = $_POST['cat_id'];
$scat_id = (isset($_POST['subcat']) ? $_POST['subcat'] : null);
$demo = $_POST['demo'];
$price = $_POST['price'];
$date = date("Y-m-d");
$featured = (isset($_POST['featured'])) ? 1 : 0;
$support = (isset($_POST['support'])) ? 1 : 0;
$stock_on = (isset($_POST['stock_on'])) ? 1 : 0;
$stock = $_POST['stock'];
$views_off = (isset($_POST['views_off'])) ? 1 : 0;
$reviews_off = (isset($_POST['reviews_off'])) ? 1 : 0;
$free = (isset($_POST['free'])) ? 1 : 0;

//$preview_img = $_FILES['previewimgfile'];
//$icon_img = $_FILES['iconimgfile'];
//$mainfile = $_FILES['mainfile']; 

$sql_upload = $DB_con->prepare("INSERT INTO dsptesty_products (name, short_des, description, cat_id, subc_id, icon_img, preview_img, file, price, demo, created, modified, support, featured, stock_on, stock, views_off, reviews_off, free, active) VALUES (:name2, :sdesc, :description, :cat_id, :scat_id, :icon_img, :preview_img, :file, :price, :demo, :created, :modified, :support, :featured, :stock_on, :stock, :views_off, :reviews_off, :free, '1')");

$sql_upload->bindparam(":name2",$name2);
$sql_upload->bindparam(":sdesc",$sdesc);
$sql_upload->bindparam(":description",$description);
$sql_upload->bindparam(":cat_id",$cat_id);
$sql_upload->bindparam(":scat_id",$scat_id);
$sql_upload->bindparam(":icon_img",$new_image_name1);
$sql_upload->bindparam(":preview_img",$new_image_name);
$sql_upload->bindparam(":file",$new_file_name);
$sql_upload->bindparam(":price",$price);
$sql_upload->bindparam(":demo",$demo);
$sql_upload->bindparam(":created",$date);
$sql_upload->bindparam(":modified",$date);
$sql_upload->bindparam(":support",$support);
$sql_upload->bindparam(":featured",$featured);
$sql_upload->bindparam(":stock_on",$stock_on);
$sql_upload->bindparam(":stock",$stock);
$sql_upload->bindparam(":views_off",$views_off);
$sql_upload->bindparam(":reviews_off",$reviews_off);
$sql_upload->bindparam(":free",$free);

if($sql_upload->execute()){
echo '<span class="text-success">Product added for selling.</span>';
}
else{
echo "Error: " . $sql_upload->error;
}

/*$sql_upload = "INSERT INTO dsptesty_products (name, short_des, description, cat_id, icon_img, preview_img, file, price, demo, created, modified, support, featured, active) VALUES ('$name2', '$sdesc', '$description', '$cat_id', '$new_image_name1', '$new_image_name', '$new_file_name', '$price', '$demo', '$date', '$date', '$support', '$featured', '1')";
      if($DB_con->query($sql_upload) === TRUE)
      {
        echo '<span class="text-success">Product added for selling.</span>';
      }*/

}
else {
  header('location: ../index.php');
}
?>

ADD-PRODUCT.PHP

<?php 

$pageTitle = "Add Product";
require_once('../system/config-admin.php');

$category = $product->get_categories();
//$category1 = $product->get_subcategories();
require_once('includes/header1.php');
?>
      <script src="//cdnjs.cloudflare.com/ajax/libs/tinymce/4.6.5/tinymce.min.js"></script>
                        <div class="content">
                        
<nav class="navbar navbar-expand-lg navbar-dark text-white rounded bg-primary box-shadow">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample08" aria-controls="navbarsExample08" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse justify-content-md-center" id="navbarsExample08">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" href="<?php echo $setting['website_url'];?>/admin/products.php">All Products</a>
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="<?php echo $setting['website_url'];?>/admin/add-product.php">Add Product</a>
          </li>
            <li class="nav-item">
            <a class="nav-link" href="<?php echo $setting['website_url'];?>/admin/best-selling-products.php">Best Sellers</a>
          </li>
        </ul>
      </div>
    </nav>
                        
                        <div class="my-3 p-3 bg-white rounded box-shadow">
                            
                            
                            <form id="upload" class="form-horizontal">
             <div class="form-group"> <label>Name:</label> <input class="form-control" name="name" id="coupon-code" type="text"></div>
             <div class="form-group"> <label>Short Description:</label> <input class="form-control" name="sdesc" id="coupon-code" type="text"></div>
            <div class="form-group"> <label>Description:</label> <textarea type="text" class="form-control" name="description" id="coupon-code"></textarea></div><hr>
             <div class="form-group"> <label>Category:</label> <div class="input-group mb-3">
  <select class="custom-select" name="cat_id" id="cat_id" required>
    <option value="">Select Category...</option>
    <?php foreach($category as $cat) {
?>
    <option value="<?php echo $cat['id']; ?>"><?php echo $cat['name']; ?></option>
    <?php } ?>
  </select>
</div></div>
             <div class="form-group"> <label>Subcategory:</label> <div class="input-group mb-3">
  <select class="custom-select" name="subcat" id="subcat">
    <option value="">Select Subcategory...</option>

  </select>
</div></div>

<!--<select name="subcat" id="subcat">

</select>-->

<script type="text/javascript">
$(function() {
 
 $("#cat_id").bind("change", function() {
     $.ajax({
         type: "GET", 
         url: "ajax-category.php",
         data: "cat_id="+$("#cat_id").val(),
         success: function(html) {
             $("#subcat").html(html);
         }
     });
 });
            
 
});
</script>


              <div class="form-group"> <label>Item Price:</label> <div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text">$</span>
  </div>
  <input type="number" name="price" class="form-control" aria-label="Amount">
  <div class="input-group-append">
    <span class="input-group-text">.00</span>
  </div>
</div></div>
               <div class="form-group"> <label>Live Preview:</label> <input class="form-control" name="demo" id="coupon-code" type="url"></div>
               
              <!-- <div class="form-group"> <label>Tags (max 15):</label> <input class="form-control" name="tags[]" id="coupon-code" type="text"></div>-->
               
               <hr>
       
         <div class="form-check">
    <input type="checkbox" name="featured" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Featured Item</label>
  </div>
           <div class="form-check">
    <input type="checkbox" name="free" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Make Free Item (Only download limited times)</label>
  </div>
    <div class="form-check">
    <input type="checkbox" name="support" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Support item</label>
  </div>
                                  <div class="form-check">
    <input type="checkbox" name="reviews_off" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Disable Reviews</label>
  </div>
                                  <div class="form-check">
    <input type="checkbox" name="views_off" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Hide View Counter</label>
  </div>
      <div class="form-check">
    <input type="checkbox" name="stock_on" id="exampleCheck1" data-toggle="collapse" data-target="#stockcoll" aria-expanded="false" aria-controls="stockcoll">
    <label class="form-check-label" for="exampleCheck1">Use Stock Limits</label>
  </div>
                              
                              <div class="collapse" id="stockcoll">
                                <br>
  <div class="card card-body">
   
    <div class="form-group"> <label>Stock Available:</label> <div class="input-group mb-0"><input class="form-control" name="stock" type="number"></div></div>
    
  </div>
</div>
       
       <hr>
                    <div class="form-group"> <label>Main File (.ZIP):</label> 
                                   <div class="input-group">
  <div class="custom-file">
    <input type="file" name="mainfile" class="custom-file-input" id="inputGroupFile04">
    <label class="custom-file-label" for="inputGroupFile04">Choose File</label>
  </div>
</div>
                    </div>

                    <div class="form-group"> <label>Icon Image File (.PNG,.JPG):</label> 
                                   <div class="input-group">
  <div class="custom-file">
    <input type="file" name="iconimgfile" class="custom-file-input" id="inputGroupFile04">
    <label class="custom-file-label" for="inputGroupFile04">Choose File</label>
  </div>
</div>
                    </div>
                    
                                        <div class="form-group"> <label>Preview Image File (.PNG,.JPG):</label> 
                                   <div class="input-group">
  <div class="custom-file">
    <input type="file" name="previewimgfile" class="custom-file-input" id="inputGroupFile04">
    <label class="custom-file-label" for="inputGroupFile04">Choose File</label>
  </div>
</div>
                    </div>

<hr>
<button type="submit" id="btn" class="btn btn-primary w-100">Upload</button>

        <div class="progress mt-3" style="display:none;">
              <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%;">

              </div>
            </div>

<script type="text/javascript">
  $("#upload").on("submit",(function(e) {
      
      tinyMCE.triggerSave();
      
    e.preventDefault();

            e.stopImmediatePropagation();
            var formData = new FormData($(this)[0]);
            var file = $('input[type=file]')[0].files[0];
            formData.append('upload_file',file);
            $('.progress').show();
    
    $.ajax({
        
         xhr: function() {
                    var xhr = new window.XMLHttpRequest();
                    xhr.upload.addEventListener("progress", function(evt) {
                        if (evt.lengthComputable) {
                            var percentComplete = evt.loaded / evt.total;
                            percentComplete = parseInt(percentComplete * 100);
                            $('.progress-bar').css('width',percentComplete+"%");
                            $('.progress-bar').html(percentComplete+"%");
                            if (percentComplete === 100) {

                        }
                      }
                    }, false);
                    return xhr;
                  },
        
          url: "<?php echo $setting['website_url'];?>/admin/ajax-upload.php",
      type: "POST",
      data:  new FormData(this),
      contentType: false,
          cache: false,
      processData:false,
      beforeSend: function() 
        {
            $("#res").html('Uploading..Please wait!');
        },  
        success: function(response)
        {
            $("#res").html(response);
        }        
     });
  }));
</script>
<div id="res"></div>
</form>
                        

     			<script type="text/javascript">
	tinymce.init({
		selector: "textarea",
		themes: "modern",
		branding: false,
		  plugins: [
    'advlist autolink lists link image charmap preview',
    'visualblocks code',
    'insertdatetime media contextmenu paste code'
  ],
  toolbar: 'bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image code'
	});
	</script>   
 <?php 
require_once('includes/footer.php');
?>

MySQL Database

UPDATE:
When i add an error handler to the ajax bit it returns with 500! So i’m unsure how to remove it or know why im getting a 500 error!

That seems self explanatory, but I don’t see any field named ‘purchases’ in your table. :thinking:

You didn’t post that code.

Sorry thats for a different table which i fixed by setting NULL to true for all of them but i just placed that error incase its the reason why the code is not working but thats fixed now so you can ignore the error files as they are not important!

I’m asking is there something wrong with my code above or how i have done the database in the picture above?

Please read the updated first message ^^^

500 is the code for a general server error, so you will have to look in the error log from your hoster.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.