我正在尝试提交一个表单,该表单应该上传图片并将其名称放在数据库中,由于某种原因,它不提交任何内容也不会上传任何内容,它会一无所获。每次提交时网站都会重新加载。
形式:
<form name="myForm" action="" enctype="multipart/form-data" method="post">
<div class="id-input">ID:</div>
<label class="input2 red bold">
<?php if(isset($beg_u_id)) echo($beg_u_id);?>
</label>
<input class="input" name="mon_by" value="<?php if(isset($beg_u_titel)) echo($beg_u_titel);?>" placeholder="By">
<input class="input" name="mon_title" value="<?php if(isset($mon_u_by)) echo($mon_u_by);?>" placeholder="Title">
<div class="button-area">
<input class="btn-upload" type="file" name="mon_img">
<div class="btn-upload-true">Vælg fil</div>
<div id="file_name">img/months/<?php if(isset($mon_u_img)) echo($mon_u_img);?></div>
</div>
<input class="btn" name="mon_submit" type="submit" value="<?php if ($mode == 'update') echo 'Opdater'; elseif ($mode == 'insert') echo 'Opret'; ?>">
<a href="panel.php?page=monthspicture.php">
<button class="danger-btn" type="button" href="">Annuller</button>
</a>
<input type="hidden" name="mon_id" value="<?php if(isset($beg_u_id)) echo($beg_u_id);?>">
</form>
</div>
提交代码:
<?php
$id = $_POST["mon_id"];
$mon_i_img = "";
if($_FILES['mon_img']['error'] == 0){
$target_dir = "../img/months/";
$target_file_only = basename($_FILES["mon_img"]["name"]);
$target_file = $target_dir . $target_file_only;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["mon_img"]["tmp_name"]);
if($check !== false) {
echo "<div class='success-box'>File is an image - " . $check["mime"] . ".</div>";
$uploadOk = 1;
} else {
echo "<div class='error-box'>File is not an image..</div>";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<div class='error-box'>Sorry, file already exists.</div>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["mon_img"]["size"] > 500000) {
echo "<div class='error-box'>Sorry, your file is too large.</div>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "<div class='error-box'>Sorry, only JPG, JPEG & PNG files are allowed.</div>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<div class='error-box'>Sorry, your file was not uploaded.</div>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["mon_img"]["tmp_name"], $target_file)) {
echo "<div class='success-box'>The file ". $target_file_only . " has been uploaded.</div>";
} else {
echo "<div class='error-box'>Sorry, there was an error uploading your file.</div>";
}
}
if ($uploadOk == 1) {
$mon_i_img = $target_file_only;
}
}
$mon_i_title = $_POST["mon_title"];
$mon_i_by = $_POST["mon_by"];
if (empty($mon_i_title) || empty($mon_i_by)) {
header ("Location: panel.php?page=monthspicture.php&create=empty");
exit;
}
$mon_i_id = $mysqli->real_escape_string($mob_i_id);
$mon_i_title = $mysqli->real_escape_string($mon_i_title);
$mon_i_by = $mysqli->real_escape_string($mon_i_by);
if ($mon_i_img != "") {
$sql_i = "INSERT INTO monthspicture (mon_title, mon_by, mon_img) VALUES('$mon_i_title', '$mon_i_by', '$mon_i_img')";
echo "<div class='success-box'>Your post have been created.</div>";
}else{
$sql_i = "INSERT INTO monthspicture (mon_title, mon_by) VALUES('$mon_i_title', '$mon_i_by')";
echo "<div class='success-box'>Your post have been created. (Without image)</div>";
}
if (!$mysqli->query($sql_i)) {
echo "Insert failed: (" . $mysqli->errno . ") " . $mysqli->error;
die();
}else{
}
?>
尝试添加:print_r($ _ POST);的print_r($ _ FILES);
Array ( [mon_by] => AthaxDesigns [mon_title] => The Sunrise [mon_submit] => Opret [mon_id] => ) Array ( [mon_img] => Array ( [name] => Athax---The-sunrise.jpg [type] => image/jpeg [tmp_name] => D:\wamp\tmp\phpF488.tmp [error] => 0 [size] => 673646 ) )
答案 0 :(得分:0)
问题已经发现我是个假人,在我的一个$ _POST ['名字']中输了一个错误;感谢所有试图帮助我的人。
答案 1 :(得分:-1)
尝试使用 $ _ FILES ['和您的文件字段名称']
if( $ mon_i_img !=“”){
$sql_i = "INSERT INTO monthspicture (mon_title, mon_by, mon_img) VALUES('$mon_i_title', '$mon_i_by', '$mon_i_img')";
echo "<div class='success-box'>Your post have been created.</div>";
}否则{
$sql_i = "INSERT INTO monthspicture (mon_title, mon_by) VALUES('$mon_i_title', '$mon_i_by')";
echo "<div class='success-box'>Your post have been created. (Without image)</div>";
}