Php 列';图像';不能为空
我已经建立了数据库,我的php表单将图像上传到我希望它正常运行的文件夹中。但是,在数据库中,它只是表示图像为null,如果我将其更改为not null,则会出现一个错误,即: 注意:未定义的索引:第45行C:\xampp\htdocs\sws\u blog\admin\add\u image\u post.php中的图像 列“image”不能为空 我试图建立一个定制的PHP博客,只有我会发布/上传到它。我有不同的帖子类型,比如“图片帖子”和“视频帖子” 我无法为我上载图像的帖子显示图像 这是我的add_image_post.php:Php 列';图像';不能为空,php,mysql,image,post,blogs,Php,Mysql,Image,Post,Blogs,我已经建立了数据库,我的php表单将图像上传到我希望它正常运行的文件夹中。但是,在数据库中,它只是表示图像为null,如果我将其更改为not null,则会出现一个错误,即: 注意:未定义的索引:第45行C:\xampp\htdocs\sws\u blog\admin\add\u image\u post.php中的图像 列“image”不能为空 我试图建立一个定制的PHP博客,只有我会发布/上传到它。我有不同的帖子类型,比如“图片帖子”和“视频帖子” 我无法为我上载图像的帖子显示图像 这是我的
<?php require_once('../Connections/blog.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "imageform")) {
$insertSQL = sprintf("INSERT INTO iposts (title, tags, teaser, ipost, image) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['tags'], "text"),
GetSQLValueString($_POST['teaser'], "text"),
GetSQLValueString($_POST['ipost'], "text"),
GetSQLValueString($_POST['image'], "text"));
mysql_select_db($database_blog, $blog);
$Result1 = mysql_query($insertSQL, $blog) or die(mysql_error());
$insertGoTo = "manage_posts.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "imageform")) {
$insertSQL = sprintf("INSERT INTO iposts (title, tags, teaser, ipost, image) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['tags'], "text"),
GetSQLValueString($_POST['teaser'], "text"),
GetSQLValueString($_POST['ipost'], "text"),
GetSQLValueString($_FILES["image"], "name"));
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["image"]["name"]);
$extension = end($temp);
if ((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/jpg")
|| ($_FILES["image"]["type"] == "image/pjpeg")
|| ($_FILES["image"]["type"] == "image/x-png")
|| ($_FILES["image"]["type"] == "image/png"))
&& ($_FILES["image"]["size"] < 200000) // <= you may have to increase this value
&& in_array($extension, $allowedExts))
{
if ($_FILES["image"]["error"] > 0)
{
echo "Return Code: " . $_FILES["image"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["image"]["name"] . "<br>";
echo "Type: " . $_FILES["image"]["type"] . "<br>";
echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>";
if (file_exists("uploads/" . $_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],
"uploads/" . $_FILES["image"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["image"]["name"];
}
}
}
else
{
echo "Invalid file";
}
mysql_select_db($database_blog, $blog);
$Result1 = mysql_query($insertSQL, $blog) or die(mysql_error());
$insertGoTo = "manage_posts.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Image Post</title>
<link href="../css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="../css/bootstrap-responsive.css" rel="stylesheet" type="text/css">
<link href="../css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Add Image Post </h1>
<p><a href="index.php">Admin Menu</a></p>
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="imageform" id="imageform">
<p>
<label for="title">Title:</label>
<input name="title" type="text" id="title" maxlength="30">
</p>
<p>
<label for="tags">Tags:</label>
<input name="tags" type="text" id="tags" maxlength="30">
</p>
<p>
<label for="teaser">Teaser:</label>
<textarea name="teaser" id="teaser"></textarea>
</p>
<p>
<label for="ipost">Post:</label>
<textarea name="ipost" id="ipost"></textarea>
</p>
<p>
<label for="image">Filename:</label>
<input type="file" name="image" id="image">
</p>
<p>
<input name="insert" type="submit" class="submit" id="insert" value="Post">
</p>
<input type="hidden" name="MM_insert" value="imageform">
</form>
</body>
</html>
添加图像帖子
添加图像帖子
应该是
GetSQLValueString($_FILES['image']['name'], "text"));
很可能
GetSQLValueString($_FILES["image"], "name"));
也应该
GetSQLValueString($_FILES['image']['name'], "text"));
您的开关
案例中没有名称
作为$类型
。在一个插入
中,您使用的是$\u POST['image']
,在另一个案例中,您使用的是$\u文件['image']
。这是故意的吗?还有,哪一行是45?发生错误是因为您尝试为图像插入的值为空。。。现在我们必须弄清楚为什么它是空的。是不是传错了?你指的是错误的东西吗?正确的方法不正确吗?还有,该函数,GetSQLValueString
。。。当您将其用于图像时,在第二次插入中将name
作为第二个参数传递。它没有处理这一问题的理由。归还什么?$\u文件['image']
和$\u POST['image']
是否都存在并具有您期望的值?最初使用$POST['image']将图像路径保存到数据库中。但我并不完全理解这是如何实现的。我将如何重命名上传的图像以与该帖子相关联?
GetSQLValueString($_FILES["image"], "name"));
GetSQLValueString($_FILES['image']['name'], "text"));