doukong1391 2016-01-27 15:09
浏览 39

上传图像并将数据插入数据库问题

Trying to build this page for adding new products,for my client Page with a form - 1)To upload the image 2)Add price and name data

For some reason its not working. After i click on the submit button,nothing happens,its stayens on the saveimage.php page

<form action="saveimage.php" enctype="multipart/form-data" method="post">

<input name="name" type="text">שם:</br>

<input name="price" type="text">מחיר:</br>
 
<input name="category" value="<?php $category ?>" type="hidden">
 
<input name="uploadedimage" type="file">

<input name="new_product" type="submit" value="Upload Image">
 
</form> 

The PHP page

 <?php
include("../../dbconn.php");

    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           case 'jpeg': return '.jpg';
           case 'PNG': return '.png';
           case 'JPG': return '.jpg';
           default: return false;
       }
     }
     
     
     
if (!empty($_FILES["uploadedimage"]["name"])) {

    $file_name=$_FILES["uploadedimage"]["name"];
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    $imgtype=$_FILES["uploadedimage"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "img/".$imagename;
}

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

    if(isset($_POST['name']) && isset($_POST['price'])) 
    {
    $product_name = strip_tags($_POST['name']);
    $product_price = strip_tags($_POST['price']);
    $category = strip_tags($_POST['category']);
    }

if(move_uploaded_file($temp_name, $target_path)) {

    $query_upload="INSERT into products ('product_name','product_price','product_pic','category') VALUES ('".$product_name."', '".$product_price."', '".$image_name."', '".$category."')";


    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());  
    
}else{

   exit("Error While uploading image on the server");
} 
else{

   header('Location: upload_image_food.php?cname='.$category.');
    }
} 
?>;

</div>
  • 写回答

1条回答 默认 最新

  • doudie2693 2016-01-27 15:29
    关注

    Few changes.

    1) Change

    <input name="category" value="<?php $category ?>" type="hidden">

    to

    <input name="category" value="<?php echo $category; ?>" type="hidden">

    2) Change (Remove single quotes ' around column name)

    $query_upload="INSERT into products ('product_name','product_price','product_pic','category') VALUES ('".$product_name."', '".$product_price."', '".$image_name."', '".$category."')";
    

    To

    $query_upload = "INSERT into products (product_name,product_price,product_pic,category) VALUES ('$product_name', '$product_price', '$image_name','$category')";
    

    3) Add header("Location: upload_image_food.php?cname=$category&Message=Success"); inside move_uploaded_file because when it is success then, it need to go to desired location with some message. This is the reason why it's showing blank.

    4) Change

    }else{
    
       exit("Error While uploading image on the server");
    } 
    else{
    
       header('Location: upload_image_food.php?cname='.$category.');
        }
    } 
    ?>;
    

    to

    }else{
    
             exit("Error While uploading image on the server");
        }
    }
    else{
        header("Location: upload_image_food.php?cname=$category");
    }
    ?>
    

    Somepage.php

    <form action="saveimage.php" enctype="multipart/form-data" method="post">
        <input name="name" type="text">שם:</br>
        <input name="price" type="text">מחיר:</br>
        <input name="category" value="<?php echo $category ?>" type="hidden">
        <input name="uploadedimage" type="file">
        <input name="new_product" type="submit" value="Upload Image">
    </form> 
    

    saveimage.php

    function GetImageExtension($imagetype)
    {
        if(empty($imagetype)) return false;
        switch($imagetype)
        {
             case 'image/bmp': return '.bmp';
             case 'image/gif': return '.gif';
             case 'image/jpeg': return '.jpg';
             case 'image/png': return '.png';
             case 'jpeg': return '.jpg';
             case 'PNG': return '.png';
             case 'JPG': return '.jpg';
             default: return false;
        }
    }
    
    
    
    if (!empty($_FILES["uploadedimage"]["name"])) {
    
        $file_name=$_FILES["uploadedimage"]["name"];
        $temp_name=$_FILES["uploadedimage"]["tmp_name"];
        $imgtype=$_FILES["uploadedimage"]["type"];
        $ext= GetImageExtension($imgtype);
        $imagename=date("d-m-Y")."-".time().$ext;
        $target_path = "img/".$imagename;
    }
    
    if(isset($_POST['new_product'])) 
    {
    
      if(isset($_POST['name']) && isset($_POST['price'])) 
      {
            $product_name = strip_tags($_POST['name']);
            $product_price = strip_tags($_POST['price']);
            $category = strip_tags($_POST['category']);
        }
    
        if(move_uploaded_file($temp_name, $target_path)) 
        {
    
            $query_upload = "INSERT into products (product_name,product_price,product_pic,category) VALUES ('$product_name', '$product_price', '$image_name','$category')";
    
            mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());  
            header("Location: upload_image_food.php?cname=$category&Message=Success");
        }
        else
        {
             exit("Error While uploading image on the server");
        } 
    else
    {
            header("Location: upload_image_food.php?cname=$category");
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度