简体   繁体   English

尝试通过 php 将照片上传到 myphpadmin 但失败

[英]Trying to upload a photo through php to myphpadmin but fails

So I am just learning PHP and I have the below code but it does not send my photos to my database.所以我只是在学习 PHP 并且我有以下代码,但它不会将我的照片发送到我的数据库。 My database file works as I use if for other pages and the data gets sent to the database.如果用于其他页面并且数据被发送到数据库,我的数据库文件就像我使用的那样工作。 My code is basic as this is how I know how to write it at the moment but there is something I am missing.我的代码是基本的,因为这就是我目前知道如何编写它的方式,但是我缺少一些东西。 The action page comes back blank.操作页面返回空白。 Not even one error mentioned and the photo does not show in the database table that they are supposed to go.甚至没有提到一个错误,并且照片没有在数据库表中显示它们应该是 go。 Any advice would be apperciated.任何建议都会受到重视。 Again just learning so I know my code is basic.再次只是学习,所以我知道我的代码是基本的。

//Form
                    
<form enctype="multipart/form-data" action="Upload.php" method="post">
<input type="file" name="photo" id="fileSelect">
<textarea align="center" name="Hashtag" cols="40" placeholder ="Describe your photo"></textarea>
<input value="Upload" class="special big" type="submit">
</form>


//Action file Upload.php

<?php
require "DB.PHP";

if (isset($_POST["submit"]))
{
$tag = $_POST["Hashtag"];
$photo = rand(1000,10000)."-".$_FILES["photo"]["name"]; //Create unique file name

$sql = "INSERT INTO photos (hashtag,Image) VALUES('$tag','$photo')";
if ($conn ->query ($sql)){
echo '<script type="text/javascript">';
echo 'alert("Your Photo was submitted.")';  
echo '</script>';
echo "<script>setTimeout(\"location.href = 'Photo_Submit.php';\",0000);</script>";
   }

else "Error: ".$sql. "<br>". $conn->error;     
$conn->close();
}
?>

//table in database

 `Image` varchar(255) NOT NULL,
  `hashtag` varchar(255) NOT NULL,
  `user_id` varchar(50) CHARACTER SET utf8 NOT NULL,
  `Time Stamp` int(11) NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`user_id`)
  1. I don't know if it is a mistake when typing the question but I can see that in your else you are not echoing the error.我不知道输入问题时是否有错误,但我可以看到在您的else中您没有回显错误。 Place echo before the error在错误之前放置echo
if () {
    //
} else {
    echo "Error: ".$sql. "<br>". $conn->error;
}
  1. Per your table definition, user_id is NOT NULL means the user_id field cannot be null.根据您的表定义, user_id NOT NULL意味着user_id字段不能是 null。 It has to be set on every request unless you set a default value (like for the timestamp)!除非您设置默认值(例如时间戳),否则必须在每个请求上设置它!

  2. Consider the advice about Dharman on SQL injections and others.考虑有关 SQL 注射和其他方面的 Dharman 建议。

Use longtext for your image in your database, not varchar(255)对数据库中的图像使用longtext ,而不是varchar(255)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM