即使代码成功完成,也会出现500内部服务器错误

时间:2017-05-19 16:41:24

标签: php mysql internal-server-error

我有以下PHP脚本执行SELECT查询,将结果绑定到变量,根据绑定变量的值运行逻辑,并根据逻辑对表执行另一个UPDATE。这段代码运行成功(我可以看到在我的表中使用正确的值更新了相应的列)但是从服务器返回响应需要大约10秒钟,响应如下:

  

“服务器遇到内部错误或配置错误,而且是   无法完成您的请求。请联系服务器   ['私人联系人']的管理员告知他们这个时间   发生错误,以及您在此错误之前执行的操作。   服务器错误中可能提供有关此错误的更多信息   日志中。

     

此外,遇到500内部服务器错误错误   在尝试使用ErrorDocument处理请求时。“

以下是我的剧本:

include("../../include/session.php");

include('inc.php');

if ((isset($_POST['lotnumberinput'])) AND (isset($_POST['sgref'])) ) {


$sgref = $_POST['sgref'];
$lotnumber = $_POST['lotnumberinput'];

$conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_errno) {
        echo "There was a problem connecting to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
    }

    if (!($sqlget = $conn->prepare("SELECT lotnumber, lotnumber2, lotnumber3 FROM invoicesJan2016 WHERE id = ?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
    }

    if (!$sqlget->bind_param("s", $sgref)) {
        echo "Binding parameters failed: (" . $sqlget->errno . ") " . $sqlget->error;
    }

    if (!$sqlget->execute()) {
        echo "Execute failed: (" . $sqlget->errno . ") " . $sqlget->error;
    }


    $sqlget->bind_result($lotnumber1, $lotnumber2, $lotnumber3);

    $res = $sqlget->fetch();

    $sqlget->free_result();

if ($res) {

    while ($res) {


        if ($lotnumber1 == "") {


            if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber = ? WHERE id = ?"))) {
                echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
            }

            if (!$sql->bind_param("ss", $lotnumber, $sgref)) {
                echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
            }


            if (!$sql->execute()) {
                    echo "Execute failed: (" . $sql->errno . ") " . $sql->error;

            } else {

                echo "SG Successfully Added!";
            }


        } else if (($lotnumber1 !== "") AND ($lotnumber2 == "")) {


            if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber2 = ? WHERE id = ?"))) {
                echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
            }


            if (!$sql->bind_param("ss", $lotnumber, $sgref)) {
                echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
            }


            if (!$sql->execute()) {
                    echo "Execute failed: (" . $sql->errno . ") " . $sql->error;

            } else {

                echo "SG Successfully Added!";
            }


        } else if (($lotnumber1 !== "") AND ($lotnumber2 !== "") AND ($lotnumber3 == "")) {


            if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber3 = ? WHERE id = ?"))) {
                echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
            }


            if (!$sql->bind_param("ss", $lotnumber, $sgref)) {
                echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
            }


            if (!$sql->execute()) {
                    echo "Execute failed: (" . $sql->errno . ") " . $sql->error;

            } else {

                echo "SG Successfully Added!";
            }


        } else {

            echo "There are too many lot numbers associated with this SG. Please call IT to request additional space.";

        }

    } // End While.

}//end if


} else {

  echo "SG Number did not save. Please try again.";

}

$sqlget->close();
$sql->close();
$conn->close();

所有的帮助都表示赞赏,因为我不确定为什么要花这么长时间才能得到答复。

谢谢!

2 个答案:

答案 0 :(得分:1)

感谢您的回复。我能够确认500内部服务器错误是由响应大小4 MB引起的...... !!!这是由逻辑的while循环引起的。通过删除while并重构页面的逻辑组件,响应明显更小并且功能完美。下面是正常运行的代码的更新版本。感谢大家的帮助!!!

我的代码:

<?php

include("../../include/session.php");
include('inc.php');

if ((isset($_POST['lotnumberinput'])) AND (isset($_POST['sgref'])) ) {

  $sgref = $_POST['sgref'];
  $lotnumber = $_POST['lotnumberinput'];

  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_errno) {
        echo "There was a problem connecting to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
  }

  if (!($sqlget = $conn->prepare("SELECT lotnumber, lotnumber2, lotnumber3 FROM invoicesJan2016 WHERE id = ?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
  }

  if (!$sqlget->bind_param("s", $sgref)) {
        echo "Binding parameters failed: (" . $sqlget->errno . ") " . $sqlget->error;
  }

  if (!$sqlget->execute()) {
    echo "Execute failed: (" . $sqlget->errno . ") " . $sqlget->error;
  }

  $sqlget->bind_result($lotnumber1, $lotnumber2, $lotnumber3);

  $res = $sqlget->fetch();

  $sqlget->free_result();

  if ($res) {

    if ($lotnumber1 == "") {

      if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber = ? WHERE id = ?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
      }

    } else if (($lotnumber1 !== "") AND ($lotnumber2 == "")) {

      if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber2 = ? WHERE id = ?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
      }

    } else if (($lotnumber1 !== "") AND ($lotnumber2 !== "") AND ($lotnumber3 == "")) {

      if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber3 = ? WHERE id = ?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
      }

    } else {

      $sql = "";

    }

    if (!($sql == "")) {

      if (!$sql->bind_param("ss", $lotnumber, $sgref)) {

        echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
      }

      if (!$sql->execute()) {

        echo "Execute failed: (" . $sql->errno . ") " . $sql->error;

      } else {

        echo "SG Successfully Added!";
      }

    } else {

      echo "There are too many lot numbers associated with this SG. Please call IT to request additional space.";

    }

  }//end if

} else {

  echo "SG Number did not save. Please try again.";

}

$sqlget->close();
$sql->close();
$conn->close();

?>

答案 1 :(得分:0)

(编辑回答)在尝试阅读您的代码后,未正确缩进并使阅读变得非常困难,我建议您检查一下您的变量($sqlget$sql$conn)仍然拥有类的实例,因此您可以实际关闭任何内容。

<?php

include("../../include/session.php");

include('inc.php');

if ((isset($_POST['lotnumberinput'])) AND (isset($_POST['sgref'])) ) {

    $sgref = $_POST['sgref'];
    $lotnumber = $_POST['lotnumberinput'];
    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_errno) {
        echo "There was a problem connecting to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
    }

    if (!($sqlget = $conn->prepare("SELECT lotnumber, lotnumber2, lotnumber3 FROM invoicesJan2016 WHERE id = ?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
    }

    if (!$sqlget->bind_param("s", $sgref)) {
        echo "Binding parameters failed: (" . $sqlget->errno . ") " . $sqlget->error;
    }

    if (!$sqlget->execute()) {
        echo "Execute failed: (" . $sqlget->errno . ") " . $sqlget->error;
    }

    $sqlget->bind_result($lotnumber1, $lotnumber2, $lotnumber3);
    $res = $sqlget->fetch();
    $sqlget->free_result();

    if ($res) {

        while ($res) {

            if ($lotnumber1 == "") {

                if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber = ? WHERE id = ?"))) {
                    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
                }

                if (!$sql->bind_param("ss", $lotnumber, $sgref)) {
                    echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
                }

                if (!$sql->execute()) {
                    echo "Execute failed: (" . $sql->errno . ") " . $sql->error;
                } else {
                    echo "SG Successfully Added!";
                }

            } else if (($lotnumber1 !== "") AND ($lotnumber2 == "")) {

                if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber2 = ? WHERE id = ?"))) {
                    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
                }

                if (!$sql->bind_param("ss", $lotnumber, $sgref)) {
                    echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
                }

                if (!$sql->execute()) {
                    echo "Execute failed: (" . $sql->errno . ") " . $sql->error;
                } else {
                    echo "SG Successfully Added!";
                }

            } else if (($lotnumber1 !== "") AND ($lotnumber2 !== "") AND ($lotnumber3 == "")) {

                if (!($sql = $conn->prepare("UPDATE invoicesJan2016 SET lotnumber3 = ? WHERE id = ?"))) {
                    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
                }

                if (!$sql->bind_param("ss", $lotnumber, $sgref)) {
                    echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
                }

                if (!$sql->execute()) {
                    echo "Execute failed: (" . $sql->errno . ") " . $sql->error;
                } else {
                    echo "SG Successfully Added!";
                }

            } else {
                echo "There are too many lot numbers associated with this SG. Please call IT to request additional space.";
            }

        } // End While.

    }//end if

    if($sqlget != null){$sqlget->close();} 
    if($sql != null){$sql->close();}
    if($conn != null) {$conn->close();}

} else {
    echo "SG Number did not save. Please try again.";
}

?>