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

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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料