列'image'不能为null

时间:2014-09-06 19:06:25

标签: php mysql image post blogs

我设置了数据库,我的php表单将图像上传到我想要的文件夹就好了。但是,在数据库中它只是对图像说null,如果我将其更改为不为null,我会收到一条错误消息: 注意:未定义的索引:第45行的C:\ xampp \ htdocs \ sws_blog \ admin \ add_image_post.php中的图像 列'图像'不能为空

我正在尝试构建一个自定义的PHP博客,我只会发布/上传到。我有不同的帖子类型,如“图像帖子”和“视频帖子”。

我无法显示该帖子的图片我上传了图片..

这是我的add_image_post.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>

Manage_Posts.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;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_getIposts = 10;
$pageNum_getIposts = 0;
if (isset($_GET['pageNum_getIposts'])) {
  $pageNum_getIposts = $_GET['pageNum_getIposts'];
}
$startRow_getIposts = $pageNum_getIposts * $maxRows_getIposts;

mysql_select_db($database_blog, $blog);
$query_getIposts = "SELECT post_id, title, updated FROM iposts ORDER BY updated DESC";
$query_limit_getIposts = sprintf("%s LIMIT %d, %d", $query_getIposts, $startRow_getIposts, $maxRows_getIposts);
$getIposts = mysql_query($query_limit_getIposts, $blog) or die(mysql_error());
$row_getIposts = mysql_fetch_assoc($getIposts);

if (isset($_GET['totalRows_getIposts'])) {
  $totalRows_getIposts = $_GET['totalRows_getIposts'];
} else {
  $all_getIposts = mysql_query($query_getIposts);
  $totalRows_getIposts = mysql_num_rows($all_getIposts);
}
$totalPages_getIposts = ceil($totalRows_getIposts/$maxRows_getIposts)-1;$maxRows_getIposts = 10;
$pageNum_getIposts = 0;
if (isset($_GET['pageNum_getIposts'])) {
  $pageNum_getIposts = $_GET['pageNum_getIposts'];
}
$startRow_getIposts = $pageNum_getIposts * $maxRows_getIposts;

mysql_select_db($database_blog, $blog);
$query_getIposts = "SELECT post_id, title, image, updated FROM iposts ORDER BY updated DESC";
$query_limit_getIposts = sprintf("%s LIMIT %d, %d", $query_getIposts, $startRow_getIposts, $maxRows_getIposts);
$getIposts = mysql_query($query_limit_getIposts, $blog) or die(mysql_error());
$row_getIposts = mysql_fetch_assoc($getIposts);

if (isset($_GET['totalRows_getIposts'])) {
  $totalRows_getIposts = $_GET['totalRows_getIposts'];
} else {
  $all_getIposts = mysql_query($query_getIposts);
  $totalRows_getIposts = mysql_num_rows($all_getIposts);
}
$totalPages_getIposts = ceil($totalRows_getIposts/$maxRows_getIposts)-1;

$maxRows_getVposts = 10;
$pageNum_getVposts = 0;
if (isset($_GET['pageNum_getVposts'])) {
  $pageNum_getVposts = $_GET['pageNum_getVposts'];
}
$startRow_getVposts = $pageNum_getVposts * $maxRows_getVposts;

mysql_select_db($database_blog, $blog);
$query_getVposts = "SELECT post_id, title, updated FROM vposts ORDER BY updated DESC";
$query_limit_getVposts = sprintf("%s LIMIT %d, %d", $query_getVposts, $startRow_getVposts, $maxRows_getVposts);
$getVposts = mysql_query($query_limit_getVposts, $blog) or die(mysql_error());
$row_getVposts = mysql_fetch_assoc($getVposts);

if (isset($_GET['totalRows_getVposts'])) {
  $totalRows_getVposts = $_GET['totalRows_getVposts'];
} else {
  $all_getVposts = mysql_query($query_getVposts);
  $totalRows_getVposts = mysql_num_rows($all_getVposts);
}
$totalPages_getVposts = ceil($totalRows_getVposts/$maxRows_getVposts)-1;

$queryString_getIposts = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_getIposts") == false && 
        stristr($param, "totalRows_getIposts") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_getIposts = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_getIposts = sprintf("&totalRows_getIposts=%d%s", $totalRows_getIposts, $queryString_getIposts);

$queryString_getVposts = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_getVposts") == false && 
        stristr($param, "totalRows_getVposts") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_getVposts = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_getVposts = sprintf("&totalRows_getVposts=%d%s", $totalRows_getVposts, $queryString_getVposts);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Manage Posts</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>Manage Posts
</h1>
<p><a href="index.php">Admin menu</a></p>
<p><a href="add_image_post.php">Add Image Post</a></p>
<p><a href="add_video_post.php">Add Video Post</a></p>
<p>&nbsp;</p>
<h3>Image Posts</h3>
<table width="800">
  <tr>
    <th scope="col">Image</th>
    <th scope="col">Date</th>
    <th scope="col">Title</th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
  </tr>
  <?php do { ?>
    <tr>
      <td><img src="uploads/<?php echo $_FILES["image"] ["name"]?>" width="100" border="0"></a></td>
      <td><?php echo $row_getIposts['updated']; ?></td>
      <td><?php echo $row_getIposts['title']; ?></td>
      <td><a href="update_image_post.php?post_id=<?php echo $row_getIposts['post_id']; ?>">EDIT</a></td>
      <td><a href="delete_post.php?post_id=<?php echo $row_getIposts['post_id']; ?>">DELETE</a></td>
    </tr>
    <?php } while ($row_getIposts = mysql_fetch_assoc($getIposts)); ?>
</table>
<table border="0">
  <tr>
    <td><?php if ($pageNum_getIposts > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_getIposts=%d%s", $currentPage, 0, $queryString_getIposts); ?>">First</a>
    <?php } // Show if not first page ?></td>
    <td><?php if ($pageNum_getIposts > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_getIposts=%d%s", $currentPage, max(0, $pageNum_getIposts - 1), $queryString_getIposts); ?>">Previous</a>
    <?php } // Show if not first page ?></td>
    <td><?php if ($pageNum_getIposts < $totalPages_getIposts) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_getIposts=%d%s", $currentPage, min($totalPages_getIposts, $pageNum_getIposts + 1), $queryString_getIposts); ?>">Next</a>
    <?php } // Show if not last page ?></td>
    <td><?php if ($pageNum_getIposts < $totalPages_getIposts) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_getIposts=%d%s", $currentPage, $totalPages_getIposts, $queryString_getIposts); ?>">Last</a>
    <?php } // Show if not last page ?></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($getIposts);

1 个答案:

答案 0 :(得分:0)

GetSQLValueString($_POST['image'], "text"));

应该是

GetSQLValueString($_FILES['image']['name'], "text"));

可能

GetSQLValueString($_FILES["image"], "name"));

也应该

GetSQLValueString($_FILES['image']['name'], "text"));

name个案例中,您$theType没有switch