douliang2087 2014-11-25 13:02
浏览 40
已采纳

PHP / MySQL上传图片并注释并打印[关闭]

I'm doing a website, really really simple btw that its pretty much done, but some problems got me! here are the 3 pages:

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 prints messages / images uploaded via admin.php
admin.php is for submiting data / images so that way can be shown on index.php
upload.php is called when the submit button is pressed on admin.php

Question: I need to modify upload.php to somehow put an ID on every uploaded image so it can be printed on index.php using its ID, How should it be?

  • 写回答

1条回答 默认 最新

  • duanche8554 2014-11-25 13:03
    关注

    question 1 : The main problem is that on admin.php after filling the forms and clicking "submit" it won't Insert data into my database table, what am I missing?:

    You are sending wrong SQL. Chanage value to values

    Change

    mysql_query("INSERT INTO BlogData ('title', 'category', 'content') VALUE('$title', '$category', '$content')");
    

    to

    mysql_query("INSERT INTO BlogData (`title`, `category`, `content`) VALUES('$title', '$category', '$content')");
    

    question 2 : I want to add a "image upload" option on the form, how do I do it? http://www.w3schools.com/php/php_file_upload.asp

    question 3 : On index.php all the entries are being "printed", how do I show the image that I've uploaded with the form too?

    Fetch the image paths from database and add <img/>s for it.

    Also mysql_ functions are deprecated. Use mysqli_ instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么