我正在做一个网站,真的非常简单顺便说一下它已经完成了,但是有些问题让我感到高兴!这是3页:
index.php http://pastebin.com/whQfhPEy
admin.php http://pastebin.com/pQCsphCa
upload.php:
<?php
mysql_connect("localhost", "xxxx", "xxxx");
mysql_select_db("xxxx");
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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["fileToUpload"]["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.";
}
}
?>
index.php打印通过admin.php上传的消息/图像 admin.php用于提交数据/图像,以便在index.php上显示方式 在admin.php上按下提交按钮时调用upload.php
问题:我需要修改upload.php,以某种方式在每个上传的图像上放置一个ID,这样就可以使用它的ID在index.php上打印,应该怎么做?
答案 0 :(得分:1)
问题1:主要问题是在填写表单并单击“提交”后在admin.php上,它不会将数据插入到我的数据库表中,我缺少什么?:
您发送错误的SQL。 Chanage value
至values
更改
mysql_query("INSERT INTO BlogData ('title', 'category', 'content') VALUE('$title', '$category', '$content')");
到
mysql_query("INSERT INTO BlogData (`title`, `category`, `content`) VALUES('$title', '$category', '$content')");
问题2:我想在表单上添加“图片上传”选项,我该怎么办? http://www.w3schools.com/php/php_file_upload.asp
问题3:在index.php上所有条目都被“打印”了,如何显示我用表格上传的图片?
从数据库中获取图像路径并为其添加<img/>
。
也弃用了mysql_
个函数。
请改用mysqli_
。