我现在正在尝试创建一个代码,该代码能够向数据库添加一些值以及图片名称,然后将实际图片移动到服务器中的文件夹。到目前为止,我已设法将图片保存到服务器或将图片的名称保存到数据库。
我的最终目标是将图像文件和名称分别上传到服务器和数据库后,能够检索它们并在另一页显示。
以下是我目前完成的代码
<?php
$pagetitle="News Management2";
session_start();
include_once('head.php');
include_once('stylesheets.php');
?>
</head>
<?php
include_once('navbar.php');
connectDB();
?>
<div class="container">
<h1> News Catalog Maintenance (TEST 2)</h1>
<table width="50%" align="center" class="boxbg all-round">
<form method="post" action="<?php $_SERVER['PHP_SELF']?>" enctype="multipart/form-data" name="RegistrationForm">
<tr><td>NewsID</td><td><input type="text" size"5" name="NewsID"></td></tr>
<tr><td>Headline</td><td><input type="text" size"40" name="Headline"></td></tr>
<tr><td>Main Body</td><td><textarea name="Story" rows="5" columns="200"></textarea></td></tr>
<tr><td>Date</td><td><input type="date" name="date"></td></tr>
<tr><td>Select image to upload:</td><td><input type="file" name="fileToUpload" id="fileToUpload"></td></tr>
<tr><td><input TYPE="submit" name="insert" title="Add to news feed" value="Post New"/> </td></tr>
</form>
</table>
<!-- FORM PROCESSING SECTION -->
<?php
if (isset($_POST['insert'])) {
//This gets all the other information from the form
$NewsID = $_POST['NewsID'];
$Headline = $_POST['Headline'];
$Story = $_POST['Story'];
$date = $_POST['date'];
//This gets the image information
$target = "images/news/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$fileToUpload = ($_FILES['fileToUpload']['name']);
//This checks for any unfilled data form
if ($NewsID =='' || $Headline =='' || $Story =='' || $date ==''){
echo "Required information is missing... fill-in all fields first... ";
}// end of unfilled data check 1
else { //Writes the information to the database
$addNewsSqlQuery = "INSERT INTO news ( NewsID, Headline, Story, date, fileToUpload )
VALUES ('$NewsID','$Headline','$Story','$date','$fileToUpload')";
$insertResult = mysqli_query($mydb, $addNewsSqlQuery) or
die ($addNewsSqlQuery . " " . mysqli_error($mydb));
echo "News Added \n ";
unset($NewsID); unset($Headline); unset($Story); unset($date); unset($fileToUpload);
}
//Writes the photo to the server
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "\n File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if(file_exists($target_file)){
echo "\n Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if($_FILES["fileToUpload"]["size"] > 5000000){
echo "\n 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.";
}
}
} //end of if INSERT button was clicked
if (isset($_POST['delete'])) {
$NewsID = $_POST['NewsID'];
$deleteNewsSqlQuery = "delete from news where NewsID= $NewsID";
$deleteResult = mysqli_query($mydb, $deleteNewsSqlQuery)
or die ($deleteNewsSqlQuery . " " . mysqli_error($mydb));
echo "Book with NewsID: " . $NewsID . " deleted!";
} //end of if DELETE button was clicked
if (isset($_POST['update'])) {
//This gets all the other information from the form
$NewsID = $_POST['NewsID'];
$Headline = $_POST['Headline'];
$Story = $_POST['Story'];
$date = $_POST['date'];
$fileToUpload = $_POST['fileToUpload'];;
$updateNewsSqlQuery = "update news set Headline='$Headline',Story='$Story', date='$date', fileToUpload='$fileToUpload' where NewsID = $NewsID";
$updateResult = mysqli_query($mydb, $updateNewsSqlQuery) or die ($updateNewsSqlQuery . " " . mysqli_error($mydb));
echo "News with NewsID: " . $NewsID . " was updated successfully!";
} //end of if update button was clicked
?>
<br>
<?php //DISPLAY A LIST OF ALL Books
$sql = "select * from news";
$result = mysqli_query($mydb, $sql) or die(mysqli_error($mydb));
if(mysqli_num_rows($result)>0){
?>
<table class="table table-striped" >
<thead>
<td><b>NewsID</b></td>
<td><b>Headline</b></td>
<td><b>Story</b></td>
<td><b>date</b></td>
<td><b>fileToUpload</b></td>
<td><b>Actions</b></td>
</thead>
<tbody>
<?php while($rows=mysqli_fetch_array($result)){
$NewsID = $rows['NewsID'];
$Headline = $rows['Headline'];
$Story = $rows['Story'];
$date = $rows['date'];
$fileToUpload = $rows['fileToUpload'];
?>
<tr>
<form method="post" id="action" action="<?php $_SERVER['PHP_SELF']?>">
<?php //if update button was pressed change all plain text to textfields to enable inline editing
if (isset($_POST['preUpdate']) && $_POST['NewsID']==$NewsID) { ?>
<td><?php echo $NewsID ?></td>
<td><input type="text" name="Headline" value="<?php echo $Headline?>"></td>
<td><input type="text" name="Story" value="<?php echo $Story?>"></td>
<td><input type="date" name="date" value="<?php echo $date?>"></td>
<td><input type="text" name="fileToUpload" value="<?php echo $fileToUpload?>"></td>
<td><input type="submit" name="update" value="Save"/></td>
<?php }
else { //These book will be displayed in plain text ?>
<td><?php echo $NewsID; ?></a></td>
<td><?php echo $Headline; ?></td>
<td><?php echo $Story; ?></td>
<td><?php echo $date; ?></td>
<td><div id="tti<?php echo $NewsID ?>" class="tti"><?php echo $fileToUpload; ?>
<img id="img<?php echo $NewsID ?>" class="image" style="display: none; position: relative;" src="<?php echo "images/news/".$fileToUpload; ?>" />
</div>
</td>
<td><input type="submit" name="preUpdate" value="Update"/>
<input type="submit" name="delete" value="delete"/></td>
<?php } // end of if update button was pressed ?>
<input type="hidden" name="NewsID" value="<?php echo $NewsID?>">
</form>
</tr>
<?php } //end of while loop that displays courses ?>
</tbody>
</table>
<?php } //end of if mysqli_num_rows >0 ?>
<?php include_once('footer.php'); ?>