PHP MySQL插入无效,我找不到任何错误

时间:2018-07-02 11:38:50

标签: php mysql mysqli

我有这个PHP代码,正在尝试将数据插入数据库。它似乎不起作用,我不确定为什么。我试图回显mysqli_error,但没有显示任何错误。我还回显了该查询,当我直接在phpmyadmin中运行它时,它运行良好。

表格

<form method="POST" action="images.php?#werk" enctype="multipart/form-data">
                    <h6>Voeg nieuwe afbeelding toe</h6>
                    <div class="file-field input-field">
                        <div class="btn">
                            <span>Bestand</span>
                            <input name="install" type="file">
                        </div>
                        <div class="file-path-wrapper">
                            <input class="file-path validate" type="text">
                        </div>
                    </div>

                    <div class="input-field">
                        <textarea id="textarea1" name="caption" class="materialize-textarea"></textarea>
                        <label for="textarea1">Beschrijving</label>
                    </div>
                    <div class="row">
                        <button class="btn waves-effect waves-light" type="submit" name="submit2">Voeg toe
                            <i class="material-icons right">add</i>
                        </button>
                    </div>
                </form>

PHP

include "includes/connect.php";
if(isset($_POST['submit2'])){
addInstallation($_POST['caption'], "install");
}

connect.php

$conn = mysqli_connect($server, $usernameDB, $password, $usernameDB);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

addInstallation

function addInstallation($caption, $name){
$target_dir = "../in-progress.johnkok.com/img/installations/";
if($_FILES[$name]["size"] > 0){
    $target_file = $target_dir . basename($_FILES[$name]["name"]);
    $uploadOK = 1;
    $image_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
    $check = getimagesize($_FILES[$name]["tmp_name"]);

    if($check !== false){
        $uploadOK = 1;
    }
    else{
        $feedback2 = "Bestand is geen afbeelding.";
        $uploadOK = 0;
    }

    if($uploadOK !== 0){
        move_uploaded_file($_FILES[$name]["tmp_name"], $target_file);
        $target_file = "img/installations/" . basename($_FILES[$name]["name"]);
        $query = mysqli_query($conn, "INSERT INTO `Installations`(`caption`, `image_path`) VALUES ('$caption','$target_file')");
    }
}
mysqli_close($conn);

}

2 个答案:

答案 0 :(得分:1)

在您的php文件中,将$ conn变量添加到您传递的内容中。

include "includes/connect.php";
if(isset($_POST['submit2'])){
addInstallation($_POST['caption'], "install",$conn);
}

答案 1 :(得分:0)

在addInstallation文件中包含连接文件。 只是一个例子:-

include "includes/connect.php";
function addInstallation($caption, $name){
// some code
}