我使用此表单在我的数据库中插入数据:
<form action="demandes-de-tatouage.html" method="post" enctype="multipart/form-data">
<div id="name"><label for="name">Nom:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="name" value="" /></div>
<div id="name"><label for="name">Email:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="email" value="" /></div>
<div id="description"><label for="description">Description:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="description" value="" /></div>
<div id="name"><label for="name">Partie du corps:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="partie" value="" /></div>
<div id="image"><label for="image">Envoyez vos images</label></div>
<div class="controls"><input id="image" name="image" type="file" value="" maxlength="100000" /> </div></br>
<div class="controls"><input id="image2" name="image2" type="file" value="" maxlength="100000" /> </div></br>
<input class="btn2" name="btn2" type="submit" value="Envoyer" /></form>
和此:
<?php
$host = "";
$databasename = "";
$user = "";
$pass = "";
/**********MYSQL Settings****************/
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
die('Can\'t use foo : '.mysql_error());
}
} else {
die('Not connected : '.mysql_error());
}
$name = $_POST["name"];
//email:
$email = $_POST["email"];
//description:
$description = $_POST["description"];
//partie:
$partie = $_POST["partie"];
function GetImageExtension($imagetype) {
if (empty($imagetype)) {
return false;
}
switch ($imagetype) {
case 'image/bmp':
return '.bmp';
case 'image/gif':
return '.gif';
case 'image/jpeg':
return '.jpg';
case 'image/png':
return '.png';
default:
return false;
}
}
if (!empty($_FILES["image"]["name"])) {
$file_name = $_FILES["image"]["name"];
$temp_name = $_FILES["image"]["tmp_name"];
$imgtype = $_FILES["image"]["type"];
$ext = GetImageExtension($imgtype);
$imagename = $file_name."-".date("d-m-Y")."-".time().$ext;
$target_path = "images/assets/syltattoo/Emails/".$imagename;
if (!empty($_FILES["image2"]["name"])) {
$file_name2 = $_FILES["image2"]["name"];
$temp_name2 = $_FILES["image2"]["tmp_name"];
$imgtype2 = $_FILES["image2"]["type"];
$ext2 = GetImageExtension($imgtype2);
$imagename2 = $file_name2."-".date("d-m-Y")."-".time().$ext2;
$target_path2 = "images/assets/syltattoo/Emails/".$imagename2;
if (move_uploaded_file($temp_name, $target_path) && move_uploaded_file($temp_name2, $target_path2)) {
$query_upload = "INSERT INTO `modx_demandes`(image, image2, name, email, description, partie) VALUES
('".$target_path."','".$target_path2."','$name', '$email', '$description', '$partie')";
mysql_query(
$query_upload) or die("error in $query_upload == ----> ".mysql_error());
} else {
exit("Error While uploading image on the server");
}
}
}
我的问题是如果用户填写了除iamge&amp;之外的所有字段。 image2没有插入数据库。如果每个字段都填满,一切都还可以。所以我有两个问题: 1-如何使其适用于空白字段? 2-如何重定向aftr表单成功? 谢谢
答案 0 :(得分:0)
这是因为您插入数据库的唯一时间是:
if (!empty($_FILES["image2"]["name"])) { /* ... */ }
阻止。
因此,如果用户在使用表单时未使用image2
字段,程序将不会执行其中包含的代码块。
在php中重定向使用以下
header('Location: someplace.php');