PHP将文本和图像插入数据库并检索它

时间:2017-08-08 01:42:34

标签: php html

AddStdRecord.php 这是我需要访问addoutput.php的AddStd记录表单,将其插入数据库中     

添加学生信息

<form action="AddOutput.php" method="POST" enctype="multipart/form-data" 
style="border:1px solid #ccc">
<div class="container">

<b>Select file to upload:</b></br></br>
<input type="file" name="UploadImage" id="UploadImage">
</br></br>
<label><b>Student ID</b></label>
<input type="text" placeholder="Enter Student Number" name="txtID" required>

<label><b>Last Name</b></label>
<input type="text" placeholder="Enter Last Name" name="txtLname" required>
<label><b>First name</b></label>
<input type="text" placeholder="Enter First Name" name="txtFname" required>

<label><b>Middle Name</b></label>
<input type="text" placeholder="Enter Middle Name" name="txtMname" required>

<label><b>Birthday</b></label>
<input type="text" placeholder="Enter Birthday" name="txtBday" required>

<label><b>Gender</b></label><br><br>
<input type="radio" name="rbgender" value="Male" checked="checked"><b>Male</b>
<input type="radio" name="rbgender" value="Female"><b>Female </b><br><br>

<label><b>Address</b></label>
<input type="text" placeholder="Enter Address" name="txtAdd" required>

<label><b>Father's Name</b></label>
<input type="text" placeholder="Enter Father's Name" name="txtfather" required>

<label><b>Mother's Name</b></label>
<input type="text" placeholder="Enter Mother's Name" name="txtmother" required>

<label><b>Contact</b></label>
<input type="text" placeholder="Enter Contact Number" name="txtcontact" required>

<label><b>Previous School</b></label>
<input type="text" placeholder="Enter Previous School Attended" name="txtPrev" required>

<div class="clearfix">
<button type="button" class="cancelbtn">Cancel</button>
<button type="submit" name="submit" class="signupbtn">Add Student</button>


</form></br></br>
</div>
</div>
</form>

AddOutput.php 输入数据库的代码,这有什么问题?     

include_once('connection.php');


$StdID = $_REQUEST['txtID'];
$Lname = $_REQUEST['txtLname'];
$Fname = $_REQUEST['txtFname'];
$Mname = $_REQUEST['txtMname'];
$Bday = $_REQUEST['txtBday'];
$Gender = $_REQUEST['rbgender'];
$Add = $_REQUEST['txtAdd'];
$Father = $_REQUEST['txtfather'];
$Mother = $_REQUEST['txtmother'];
$Contact = $_REQUEST['txtcontact'];
$PrevSch = $_REQUEST['txtPrev'];



$target_dir = "StdImg/";
$target_file = $target_dir . basename($_FILES["UploadImage"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
 } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
 }
// Check if file already exists
 if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["UploadImage"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}


$sql = "INSERT INTO tblstdpro VALUES ('".$_SERVER['DOCUMENT_ROOT']."\\StdImg\\".$fileToUpload."', '$StdID', '$Lname', '$Fname', '$Mname', '$Bday', '$Gender', '$Add', '$Father', '$Mother', '$Contact', '$PrevSch')";

   if ($conn->query($sql) === TRUE) {
header("Location: AddStdRec.php?");
  } else {
   echo "Error: " . $sql . "<br>" . $conn->error;
 }

$conn->close();
?>

我输入的是什么错误?它进入主文件夹,但没有去数据库,我一直在寻找这个功能的日子,但似乎无法找到一个,请评论我的错误提前谢谢你

0 个答案:

没有答案