douyoufan7881 2017-07-08 12:38
浏览 35
已采纳

PHP / mysqli:无缘无故跳过行

I've got the code below - it uploads an image to the server (which actually works) and then inserts the link to the image and some other information into a database. Somehow it skips the database-insert part in mysqli every single time.

What am I doing wrong? Even when I do insert echo's between the mysqli-part lines, it doesn't echo them. Is this code seriously getting skipped or am I missing something pretty obvious?

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

The Picture Upload does work completely and is copied from w3schools.com. All private information etc. is replaced by obvious words. I've inserted comments in the code that clarify anything else.

WOOPS: I just realized that Stack Overflow's snippet editor "tidied" this snippet into total mess. Sorry about that. For clarification, the if(isset($_POST["submit"])) { part is around all of the code, also around the broken mysqli-part.

展开全部

  • 写回答

1条回答 默认 最新

  • donglan9517 2017-07-08 12:44
    关注

    You have a spelling error here:

    if (!$sucess) {
    

    Should be $success

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部