如何将图像上传到数据库和根文件夹?

时间:2017-04-27 19:32:03

标签: php mysqli

我正在尝试将产品添加到我的网站,我想在每个产品中添加图片。我创建了图像应该去的文件夹,并在表格中为图像创建了一行。我正在使用Dreamweaver并且我尝试插入记录但是我收到错误(列'图像'不能为空)所以我一直在寻找代码从这里和那里解决它现在我不会得到错误但是所有的东西我提交的不是去数据库,只有图像转到根文件夹。

消息框未显示已添加产品。我已经读过mysqli和mysql不能混合,可能是问题吗?我还是个初学者,所以我希望这个问题很清楚。感谢。

<?php require_once('Connections/MJ_conn.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "1";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "Login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?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;
}
}

mysql_select_db($database_MJ_conn, $MJ_conn);
$query_AddProduct = "SELECT * FROM products";
$AddProduct = mysql_query($query_AddProduct, $MJ_conn) or die(mysql_error());
$row_AddProduct = mysql_fetch_assoc($AddProduct);
$totalRows_AddProduct = mysql_num_rows($AddProduct);
?>

<!DOCTYPE html>
<?php include("db.php"); ?>
<html>
<head>
    <title>Product List</title>
    <link rel="stylesheet" href="IndexStyle.css"/>
    <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">
    <link href="SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css">
    <link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css">
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
</head>
<body>
<body class="body">
    <header class="MainHeader">
    <img src="images/header-image-5.jpg">
    <nav><ul>
         <li class="active"><a href="index.html">Home</a></li>
         <li><a href="Product.php">Products</a></li>
         <li><a href="AboutUs.php">About Us</a></li>
         <li><a href="ContactUs.php">Contact Us</a></li>
        </ul>
    </header>
    <div class="MainBody">
      <form method="POST" enctype="multipart/form-data" name="insert_product" id="insert_product">
        <table width="800" border="0">
              <tr>
                <td width="144">Product Name:</td>
                <td width="646"><span id="sprytextfield1">
                  <label for="ProductName"></label>
                  <input type="text" name="product_name" id="ProductName">
                <span class="textfieldRequiredMsg">A value is required.</span></span></td>
              </tr>
              <tr>
                <td>Price:</td>
                <td>RM <span id="sprytextfield2">
                  <label for="ProductPrice"></label>
                  <input type="text" name="price" id="ProductPrice">
                <span class="textfieldRequiredMsg">A value is required.</span></span></td>
              </tr>
              <tr>
                <td>Category:</td>
                <td><span id="spryselect1">
                  <label for="ProductCategory"></label>
                  <select name="category" id="ProductCategory">
                    <option value="Shirts">Shirts</option>
                    <option value="Pants">Pants</option>
                    <option value="Shoes">Shoes</option>
                  </select>
                <span class="selectRequiredMsg">Please select an item.</span></span></td>
              </tr>
              <tr>
                <td>Product Details:</td>
                <td><span id="sprytextarea1">
                  <label for="ProductDetails"></label>
                  <textarea name="details" id="ProductDetails" cols="45" rows="5"></textarea>
                <span class="textareaRequiredMsg">A value is required.</span></span></td>
              </tr>
              <tr>
                <td>Product Image:</td>
                <td><label for="ProductImage"></label>
                <input type="file" name="image" id="ProductImage"></td>
              </tr>
              <tr>
                <td><input name="id" type="hidden" id="id" value="<?php echo $row_AddProduct['id']; ?>"></td>
                <td><input type="submit" name="ProductSubmit" id="ProductSubmit" value="Add Product"></td>
              </tr>
        </table>
      </form>
    </div>
    <aside class="SideBar">
        <p>MySideBar 
        </p>
    </aside>
    <Footer class="Footer"> 
        <p>Copyrights reserved 
        </p>
    </Footer>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1");
var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1");
</script>
</body>
</html>
<?php 
mysql_free_result($AddProduct);


    if(isset($_POST['ProductSubmit'])){

        //getting the text data from the fields
        $id = $_POST['id'];

        $product_name = $_POST['product_name'];
        $price = $_POST['price'];
        $details = $_POST['details'];
        $category = $_POST['category'];

        //getting the image from the field
        $image = $_FILES['image']['name'];
        $image_tmp = $_FILES['image']['tmp_name'];

        move_uploaded_file($image_tmp,"ProductImages/$image");

         $AddProduct = "insert into products (id,product_name,price,details, category, image) values ('$id','$product_name','$price','$details', '$category','$image')";

         $AddProduct = mysqli_query($con, $AddProduct);

         if($AddProduct){

         echo "<script>alert('Product Has been inserted!')</script>";
         echo "<script>window.open('AddProduct.php','_self')</script>";

         }
    }
?>

这是db.php文件夹:

<?php 
                // After uploading to online server, change this connection accordingly

                $con = mysqli_connect("localhost","root","","mjwebsite");

                if (mysqli_connect_errno())
                  {
                  echo "Failed to connect to MySQL: " . mysqli_connect_error();
                  }

                ?>

0 个答案:

没有答案
相关问题