PHP / mysqli: Lines are skipped for no reason

↘锁芯ラ 提交于 2019-12-14 03:23:59

问题


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:


You have a spelling error here:

if (!$sucess) {

Should be $success



来源:https://stackoverflow.com/questions/44990544/php-mysqli-lines-are-skipped-for-no-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!