我有这个php代码,我正在尝试将数据插入到我的数据库中。它似乎不起作用,我也不知道为什么。我试图回显mysqli\u错误,但没有显示任何错误。我也响应了这个查询,当我直接在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");
}
连接.php
$conn = mysqli_connect($server, $usernameDB, $password, $usernameDB);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
附加安装
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条答案
按热度按时间jpfvwuh41#
在php文件中,将$conn变量添加到您传递的内容中。
62lalag42#
在附加安装文件中包括连接文件。举个例子: