PHP / mysqli:无缘无故跳过行

时间:2017-07-08 20:38:10

标签: php mysql mysqli lines skip

我已经获得了以下代码 - 它将图像上传到服务器(实际上可以正常工作),然后将图像链接和其他一些信息插入到数据库中。不知何故,它每次都跳过mysqli 中的数据库插入部分

我做错了什么?即使我在mysqli-part行之间插入echo,它也不会回显它们。这段代码是否会被严重删除,或者我错过了一些非常明显的内容?

<?php
    //Handle IMG Upload - copyied from w3schools
  //THIS WORKS!
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
        // Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["fileToUpload"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            exit("Sorry, your file was not uploaded.");
            // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                echo "File branche: ". $_FILES["fileToUpload"]["name"];
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }

    //Database Upload

    //It starts to skip right after the mysqli_connect command - with skipping i mean that it doesn´t even execute the echos i have inserted in between.
    //The vars $_POST["txtpost"] and $target_file are completly fine. I checked it, no reason to question this
    //The password for connection is completly correct. No reason to question.
    //This code is actually one phrase in german, cause i´m german. verbindung is translated in connection.

        $verbindung = mysqli_connect('placeholder','placeholder','placeholder','placeholder');
        if(mysqli_connect_errno()) {
            die('Connect Error');
        }
        $verbindung->set_charset("utf8");
        $txtpost = $_POST["txtpost"];
        $success = $connection->query("INSERT INTO XXX (Txt, ImageURL) VALUES('$txtpost','$target_file')");
        if (!$sucess) {
            $connection->close();
            die("Query did not succeed");
        }
        $connection->close();
        echo "Query did succeed";
    }
  //This above database action snippet is just being skipped
?>
  <html>

  <head>
    <title>Example Title</title>
  </head>

  <body>
    <form action="thisdocument.php" method="post" enctype="multipart/form-data">
      Text Data: <input type="text" name="txtpost"> <br /> Image Data: <input type="file" name="fileToUpload" id="fileToUpload"> <br /> Date: (USING CURRENT_TIMESTAMP) <br /> Indexing ID Number: (USING AI) <br />
      <input type="submit" value="Submit Data" name="submit">
    </form>
  </body>
  </html>

图片上传功能完全正常,并从w3schools.com复制。所有私人信息等都被明显的词语所取代。我已在代码中插入注释,以澄清其他任何内容。

WOOPS :我刚刚意识到Stack Overflow的代码片段编辑器整理了#34;此片段为混乱。对于那个很抱歉。为了澄清,if(isset($_POST["submit"])) {部分围绕着所有代码,也围绕着破碎的mysqli部分。

1 个答案:

答案 0 :(得分:0)

这里有拼写错误:

    var data = { "description": "string", "id": 10, "username": "barubanget"}
    $.ajax({
        data: JSON.stringify(data),
        dataType: 'json',
        url : "http://localhost:8085/TaspenNCBSpring/insertuserlogin",
        type : 'POST',
        success : function(data) {
            console.log("success post!");
        },
        error : function(e) {
            console.log("error: " + e);
        }
    });     

应该是$ success