我有这个代码,但我无法解决它的问题。任何帮助将不胜感激。请不要太快将它标记为副本而不给我一个解决方案,因为我已经完成了所有相似的问题并且他们没有回答我的问题,因为我已经检查了文件是否上传是为了处理它仍然得到这个错误UNDEFINED INDEX:
Html代码:
<!DOCTYPE html>
<html>
<body>
<form role="form" method="post" action="test2.php">
<div class="form-group">
<div class="row">
<div class="col-xs-8">
<label class="control-label">Call Sign</label>
<input type="text" class="form-control" name="call_sign" />
</div>
<div class="col-md-4">
<label class="control-label">Picture</label>
<img src="Koala.jpg" alt="with responsive image feature" class="img-responsive img-circle">
</div>
</div>
<div class="row">
<div class="col-xs-8">
<label class="control-label">Driver First Name</label>
<input type="text" class="form-control" name="first_Name" />
</div>
</div>
<div class="row">
<div class="col-xs-8">
<label class="control-label">Driver Last Name</label>
<input type="text" class="form-control" name="last_Name" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-8">
<label for = "fileToUpload">File input</label>
<input type="file" name="fileToUpload" id="fileToUpload" >
</div>
</div>
<div class="row">
<div class="col-xs-8">
<label class="control-label">Location</label>
<input type="text" class="form-control" name="Location" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-4 selectContainer">
<label class="control-label">Company Shirt</label>
<select class="form-control" name="CShirt">
<option value="">Choose </option>
<option value="Good">Good</option>
<option value="Poor">Poor</option>
<option value="UniformNotWorn">Uniform Not Worn</option>
<option value="Untidy">Untidy</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-xs-4 selectContainer">
<label class="control-label">Trousers</label>
<select class="form-control" name="Trousers">
<option value="">Choose </option>
<option value="Clean/Smart">Clean/Smart</option>
<option value="Acceptable">Acceptable</option>
<option value="Jeans/Casual">Jeans/ Casual</option>
<option value="Untidy">Untidy</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-xs-4 selectContainer">
<label class="control-label">General Appearance</label>
<select class="form-control" name="Appearance">
<option value="">Choose </option>
<option value="Good">Good</option>
<option value="Fair">Fair</option>
<option value="Poor">Poor</option>
</select>
</div>
</div>
<div class="form-group">
<div class="row">
<label class="control-label">In the Vehicle</label>
<div class = "checkbox">
<label><input type = "checkbox" value="Tea/Coffe" name="In_the_Vehicle"> Tea/Coffe</label>
</div>
<div class = "checkbox">
<label><input type = "checkbox" value="Eating" name="In_the_Vehicle"> Eating</label>
</div>
<div class = "checkbox">
<label><input type = "checkbox" value="Smoking" name="In_the_Vehicle"> Smoking</label>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label">Assessor Comments</label>
<div>
<textarea class="controlline" name="Assessor_Comments" rows="8"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label">Driver Comments</label>
<div>
<textarea class="controlline" name="Driver_Comments" rows="8"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label">Kit Required</label>
<div>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="DMN Shirts" /> DMN Shirts
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="DMN Jacket" /> DMN Jacket
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="BMW Jacket" /> BMW Jacket
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="Hi-Viz-Jacket" />Hi-Viz-Jacket
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="ID Badge" /> ID Badge
</label>
</div>
</div>
<div class="form-group">
<div>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="Tyre_Guage" /> Tyre Guage
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="Trade_Plate_Bands" /> Trade Plate Bands
</label>
<label class="radio-inline">
<input type="radio" name="Kit_Required" value="MiVIS_Ruler/Kit" /> MiVIS Ruler/Kit
</label>
</div>
</div>
<div class="form-group">
<label class="control-label">Training Wanted</label>
<div>
<label class="radio-inline">
<input type="radio" name="Training_Wanted" value="Yes" /> Yes
</label>
<label class="radio-inline">
<input type="radio" name="Training_Wanted" value="No" /> No
</label>
</div>
</div>
<button type="submit" class="button" name="submit">Submit</button>
</form>
</body>
</html>
php代码:
if (isset($_POST['submit'])) {
$query = "INSERT INTO spot_checkdetails(location,CShirt,trouser,appearance,kitReq,Training,fk1_callSIgn)
VALUES('$_POST[Location]','$_POST[CShirt]' ,'$_POST[Trousers]' ,'$_POST[Appearance]','$_POST[Kit_Required]' ,'$_POST[Training_Wanted]','$_POST[call_sign]')";
$result = mysqli_query($connection, $query);
if (!$result) die("database acess failed".$connection->error);
$foreignk = "select id from spot_checkdetails order by id desc limit 1";
$result = mysqli_query($connection, $foreignk);
while ($row = $result->fetch_assoc()) {
$reqry = $row["id"];
}
$query = "INSERT INTO spotcheckcomment(assCom,driCom,spot_checkdetails_id)VALUES('$_POST[Assessor_Comments]','$_POST[Driver_Comments]','".$reqry."')";
$result = mysqli_query($connection, $query);
$foreign2 = "select id from spotcheckcomment order by id desc limit 1";
$result = mysqli_query($connection, $foreign2);
while ($row = $result->fetch_assoc()) {
$reqry2 = $row["id"];
}
$foreign3 = "select fk1_callSIgn from spot_checkdetails order by id desc limit 1";
$result = mysqli_query($connection, $foreign3);
while ($row = $result->fetch_assoc()) {
$reqry3 = $row["fk1_callSIgn"];
}
if (!empty($_POST)) { // **check weather the form is submitted or not**
// Where the file is going to be placed
$target_path = "uploads/";
$random_digit = rand(0000, 9999)."_";
$jobnumber = $reqry3."_".$random_digit;
//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables
$new_file_name = $jobnumber.basename($_FILES["fileToUpload"]["name"]);
//$target_file = $target_dir .$new_file_name;
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path.$new_file_name; // basename( $_FILES['uploadedfile']['name']);
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
echo "The file ".basename($_FILES['fileToUpload']['name']).
" has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
}
$query = "INSERT INTO spotcheckimagepath(imgname,imagepath,spot_check_comment_id)VALUES('".$new_file_name."','".$target_path."','".$reqry2."')";
$result = mysqli_query($connection, $query);
}
?>
ERROR :)注意:未定义的索引:
中的fileToUpload答案 0 :(得分:1)
更改以下内容:
<form role="form" method="post" action="test2.php">
到
<form role="form" method="post" action="test2.php" enctype="multipart/form-data">
在尝试访问var之前还会检查var是否存在,可以使用
if(isset($_FILES['fileToUpload']){do something}else{error!}
另外建议您在html之前移动PHP
代码。
答案 1 :(得分:1)
正如w3schools.com所说,您需要向表单添加enctype="multipart/form-data"
。
multipart / form-data
不编码任何字符。使用具有文件上载控件的表单时,此值是必需的
所以改变这一行
<form role="form" method="post" action="test2.php">
到这个
<form role="form" method="post" action="test2.php" enctype="multipart/form-data">
另外,您可以看到here如何检查图片是否已上传