php图像上传-从表单上传的图像,但未存储路径
我已经一年多没写代码了,所以请温柔一点 我编写了一个表单,使用sendmail脚本向管理员发送电子邮件,并将任何图像上载到网站中的文件夹,并将路径记录到数据库表中。sendmail脚本有效,图像存储在“我的网站”的“上载”文件夹中,但未记录路径,插入查询记录以下错误:php图像上传-从表单上传的图像,但未存储路径,php,image,path,Php,Image,Path,我已经一年多没写代码了,所以请温柔一点 我编写了一个表单,使用sendmail脚本向管理员发送电子邮件,并将任何图像上载到网站中的文件夹,并将路径记录到数据库表中。sendmail脚本有效,图像存储在“我的网站”的“上载”文件夹中,但未记录路径,插入查询记录以下错误: Query was: INSERT INTO englisharabicimages VALUES ('','uploads/milkshake.png'). Error: 我知道我可能错过了一些非常基本的东西,但我已经很久没有
Query was: INSERT INTO englisharabicimages VALUES ('','uploads/milkshake.png'). Error:
我知道我可能错过了一些非常基本的东西,但我已经很久没有接触过php了。我不知道我做错了什么
<?php
//connect to database, check login credentials, declare message variables
include "connect.php";
error_reporting(E_ERROR);
require_once('sendmail.php');
$message = $_GET['message'];
//function to check for valid image formats
function upload($file_upload, $dir){
$url ='';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = finfo_file($finfo, $file_upload["tmp_name"]);
$allowedExts = array("gif", "jpeg", "jpg", "png", "pdf", "PDF", "doc", "DOC", "docx", "DOCX", "JPG", "JPEG", "PNG", "GIF");
$temp = explode(".", $file_upload["name"]);
$extension = end($temp);
if ((($file == "image/gif")
|| ($file == "image/jpeg")
|| ($file == "image/jpg")
|| ($file == "image/pjpeg")
|| ($file == "image/x-png")
|| ($file == "image/png"))
|| ($file == "application/pdf")
|| ($file == "application/msword")
|| ($file == "application/vnd.openxmlformats- officedocument.wordprocessingml.document")
&& ($file_upload["size"] < 7000000)
&& in_array($extension, $allowedExts))
{
if ($file_upload["error"] > 0){
$message = "An error occurred: " . $file_upload["error"] . "<br>";
}
else{
$path = $dir . $file_upload["name"];
move_uploaded_file($file_upload["tmp_name"],$path);
}
}
else
{
$message = "Wrong format";
}
return $path;
}
$projectdetailsErr = $emailErr = $dateErr = "";
$projectdetails = $email = $date = "";
function validate_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['Submit']))
{
$has_errors = false;
if (!empty($_POST["projectdetails"])) {$projectdetails = validate_input($_POST["projectdetails"]);}
if (!empty($_POST["email"])) {$email = validate_input($_POST["email"]);}
if (!empty($_POST["date"])) {$date = validate_input($_POST["date"]);}
if (!$has_errors)
{
$Link = mysql_connect($Host, $User, $Password);
$path = upload($dir);
if(!empty($_FILES) && is_array($_FILES)){
$path = upload($_FILES["image"], "uploads/");
}
$Query = "INSERT INTO englisharabicimages VALUES ('','".mysql_escape_string($path)."')";
} else {
die("Query was: $Query. Error: ".mysql_error($Link));
}
if($sql = mysql_db_query ($DBName, $Query, $Link)) {
$message = "Thank you for your enquiry.";
header("Location: index.php?message=".urlencode($message));
} else {
die("Query was: $Query. Error: ".mysql_error($Link));
}
}
?>
0){
$message=“出现错误:“.$file_upload[“error”]。”
”;
}
否则{
$path=$dir.$file_upload[“name”];
移动上传的文件($file\u upload[“tmp\u name”],$path);
}
}
其他的
{
$message=“格式错误”;
}
返回$path;
}
$projectdetailsErr=$emailErr=$dateErr=“”;
$projectdetails=$email=$date=“”;
函数验证输入($data)
{
$data=修剪($data);
$data=条带斜杠($data);
$data=htmlspecialchars($data);
返回$data;
}
如果(isset($_POST['Submit']))
{
$has_errors=false;
如果(!empty($_POST[“projectdetails”]){$projectdetails=validate_输入($_POST[“projectdetails”]);}
如果(!empty($_POST[“email”]){$email=validate_输入($_POST[“email”]);}
如果(!empty($_POST[“date”]){$date=validate_输入($_POST[“date”]);}
如果(!$有错误)
{
$Link=mysql\u connect($Host、$User、$Password);
$path=上传($dir);
if(!empty($\u文件)&&is\u数组($\u文件)){
$path=upload($_文件[“图像”],“uploads/”);
}
$Query=“插入到englisharabicimages值(“”,“.mysql_escape_字符串($path)。”)”;
}否则{
die(“querywas:$Query.Error:.mysql_Error($Link));
}
if($sql=mysql\u db\u query($DBName,$query,$Link)){
$message=“谢谢您的查询。”;
标题(“Location:index.php?message=“.urlencode($message));
}否则{
die(“querywas:$Query.Error:.mysql_Error($Link));
}
}
?>
请发布完整的错误消息。另外,不要使用mysql.*
函数,它们从PHP5.5开始就被弃用,在PHP7.0中被完全删除。使用或代替。注意。谢谢这是完整的消息:查询是:插入到englisharabicimages值(“”,'uploads/milkshake.png')。错误:我可以知道为什么在第一列中插入空白字符串'
?确保它可以接受该值。第一列是主键。我以前使用过这个脚本,它工作正常,但这是一个新的数据库和一个新的连接脚本。这就是问题所在。您不应该在主键列中插入空字符串,根据定义,主键唯一地标识特定行。因此,要么使该列自动递增,要么为插入的每一行设置唯一值。