douyoufan7881 2017-07-08 20: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 20:44
    关注

    You have a spelling error here:

    if (!$sucess) {
    

    Should be $success

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

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”