POST 500(内部服务器错误) - Ajax

时间:2017-11-07 20:29:40

标签: javascript php ajax

我已经检查了类似的问题,但我无法找到我的代码的问题,我尝试做的是创建产品,所以我的代码应该做什么是对每个产品进行ajax调用,即使它没有被修改。但有时它会起作用,有时它会抛出错误。

JS

for (var i = 0; i < $('.pds').length; i++) {
    var pds_items = $('.pds').length;
    var pds_counter = 0;
    var pds = $(".pds").eq(i);    

    var data = new FormData();    
    $(pds).append( "<input name='position' type='hidden'/>" );
    $(pds).append( "<input name='titulo' type='hidden'/>" );
    $(pds).append( "<input name='descripcion' type='hidden'/>" );
    $(pds).append( "<input name='pds_id' type='hidden'/>" );
    data.append("titulo", pds.find("input[name=titulo]").val());
    data.append("descripcion", pds.find("input[name=descripcion]").val());
    data.append("position", i);
    data.append("pds_id", pds.attr("id"));
    //alert(pds.attr("id"));

    if (pds.find("input[name=pdsimg]").get(0).files.length != 0) {

    $(pds).append( "<input name='img_type' type='hidden'/>" );
    $(pds).append( "<input name='img_status' type='hidden'/>" );
    data.append('img_type', 'pds');
    data.append('img_status', "1");      
    data.append('tmp_img', pds.find("input[name=pdsimg]").prop('files')[0]);

    } else { 

      $(pds).append( "<input name='img_status' type='hidden'/>" );    
      data.append('img_status', "0");   

    }        

    $.ajax({
            type: 'POST',               
            processData: false, // important
            contentType: false, // important
            data: data,
            url: "update_pds.php",

              success: function(data) {
                pds_counter++;                
                if (pds_counter == pds_items) {                 
                  pds_ready = 1;
                  allReady();
                }
              },
              error: function(jqXHR, status, error) { 
              console.log(jqXHR); 
              console.log(status); 
              console.log(error);
              //alert("Hubo un problema");
              //location.reload();
              //console.log("Error al actualizar");
              } 
          });  
  }

PHP

<?php
$conn = mysqli_connect('localhost', 'balaboxc_javier', '*****', 'balaboxc_xauty');

session_start();
    if ( isset($_SESSION['XautySV'])!="" ) {
    $user_id = $_SESSION['XautySV'];
    }

$titulo = $_POST["titulo"];
$descripcion = $_POST["descripcion"];
$pds_id = $_POST["pds_id"];
$titulo = mysqli_real_escape_string($conn,$titulo);
$descripcion = mysqli_real_escape_string($conn,$descripcion);
$position = $_POST["position"];

$img_type = $_POST["img_type"];
$img_status = $_POST["img_status"];
$filetmp = $_FILES["tmp_img"]["tmp_name"];
$filename = $_FILES["tmp_img"]["name"];
$filename = mysqli_real_escape_string($conn,$filename);
$filetype = $_FILES["tmp_img"]["type"];
$filepath = "bgs/".$filename;
$filepath = preg_replace('/\s+/', '-', $filepath);

if ($titulo == "" && $descripcion == "" && $filename == "") {
    // El producto está vacío, por lo tanto no se hace nada         
    $check = mysqli_query($conn, "SELECT * FROM `pds_svs` WHERE `user_id` = '$user_id' AND `id` = '$pds_id'") or exit(mysqli_error());
    if(mysqli_num_rows($check)){
        //$sql = "UPDATE pds_svs SET title=' ', description=' ' WHERE position='$position' AND user_id='$user_id';"     
        $sql = "DELETE FROM pds_svs WHERE user_id = '$user_id' AND id = '$pds_id';";
        $conn->multi_query($sql);
        //mysqli_query($con,"DELETE FROM `pds_svs` WHERE `user_id` = '$user_id' AND `position` = '$position'");
    }
    echo "1";
} else {

$check = mysqli_query($conn, "SELECT * FROM `pds_svs` WHERE `user_id` = '$user_id' AND `id` = '$pds_id'") or exit(mysqli_error());

if(mysqli_num_rows($check)){

    //Subir a base de datos
    $sql = "UPDATE pds_svs SET title='$titulo', description='$descripcion' WHERE id='$pds_id' AND user_id='$user_id';";

    if ($conn->multi_query($sql) === TRUE) {
        if ($img_status == 1) {

            if (move_uploaded_file($filetmp,$filepath)) {

                //Subir a base de datos
                    $sql = "UPDATE pds_svs SET img_url='$filepath' WHERE user_id='$user_id' AND id='$pds_id';";             

                    if ($conn->multi_query($sql) === TRUE) {
                        echo "1";
                    } else {
                        echo "0";       
                    }
                //Fin Subir a base de datos

            }
        }
        //echo "1";
    } else {
        echo "0";       
    }
    //Subir a base de datos

} else {    

    $result = mysqli_query($conn, "SELECT MAX(position) FROM pds_svs WHERE user_id = '$user_id'");
    $row = mysqli_fetch_row($result); 
    if ($position != 0) {
        $position = $row[0]; 
        $position++;
    }

    //Subir a base de datos
    $sql = "INSERT INTO pds_svs (user_id, title, description, position) VALUES ('$user_id', '$titulo', '$descripcion', '$position');";
    if ($conn->multi_query($sql) === TRUE) {

        if ($img_status == 1) {

            if (move_uploaded_file($filetmp,$filepath)) {               

                //Subir a base de datos
                    $sql = "UPDATE pds_svs SET img_url='$filepath' WHERE user_id='$user_id' AND position='$position';";             

                    if ($conn->multi_query($sql) === TRUE) {
                        echo "1";
                    } else {
                        echo "0";       
                    }
                //Fin Subir a base de datos

            }
        }

        //echo "1";
    } else {
        echo "0"; 
    }   
    //Fin Subir a base de datos

}

} // Esta llave cierra la sentencia que evalua si el producto tiene información o no

?>

0 个答案:

没有答案