如何插入复选框& php中数据库中的文件字段值

时间:2014-04-11 09:45:42

标签: php mysql

我在php文件中有以下代码:

include_once('connection.php');
$prod_imgs='';
if(isset($_REQUEST['submit']))
{
    $category_id=$_REQUEST['category']; 
    $product_id=$_REQUEST['product'];
    $relation_id=$_REQUEST['relation'];
    $occasion_id=$_REQUEST['occasion'];
    $prod_image = $_FILES['prod_image']['name'];

    $tot=count($prod_image);
    $tott=count($occasion_id);
    //$tott=count($occasion);
    foreach($prod_image as $key=>$prod_imgs)
    {
        if($prod_imgs==="")
        {
            unset($prod_image[$key]);
            $prod_image[] = $prod_imgs;
            echo $key.$prod_imgs;
        }

    }

        for($i=0;$i<$tott;$i++)
        {

        if($prod_image!='')
        {
             $prod_images=$prod_image[$i];
            $occasion=$occasion_id[$i];

            //$image=$prod_images[$i];  
            //echo $prod_images=$prod_image[$i];

        if(mysql_query("insert into tbl_add_product_occasion                            values('','$category_id','$product_id','$relation_id','$occasion','product_images/$prod_images')")or die(mysql_error()))
    {
        $msg="<script>alert('your product info added successfully')</script>";
    }
    else
    {
        echo "<script>alert('an error occured!!!')</script>";
    }

    }
    }
echo $msg;
}

& the html is:





<script type="text/javascript">
function get_product(cat){      
                                  // alert(cat);
                                   $.ajax({
                                      type : "POST",
                                      url : 'ajax_get_product.php',
                                      data : 'cat=' +cat,
                                      success : function(data){
                                        //alert(data);
                                        document.getElementById('prod').innerHTML=data;
                                            //society_code').innerH''TML=data.d;
                                         }
                                      });

        };


</script>
<form action="" method="post"  enctype="multipart/form-data">

                <fieldset>
                 <dl>
                        <dt><label for="gender">Select category:</label></dt>
                        <dd>
                            <select  name="category" id="category" onchange="get_product(this.value)" >
                                <option>select category</option>
                                <?php
                                $sel=mysql_query("select * from tbl_category");
                                while($arr=mysql_fetch_array($sel))
                                {
                                    $id=$arr['id'];
                                    echo "<option value='".$id."'>".$arr['category']."</option>";
                                }
                                ?>
                            </select>
                        </dd>
                    </dl>
                    <dl>
                    <dt><label for="product">Select Product:</label></dt>
                        <dd>
                            <select name="product" id="prod">
                                <option value=''>select product</option>

                            </select>
                        </dd>
                    </dl>

                    <dl>
                    <dt><label for="option">choose relation:</label></dt>
                    <dd>
                    <select name="relation">
                    <?php
                    $query=mysql_query("select * from tbl_relation");
                    while($result=mysql_fetch_array($query))
                    {
                        $relation=$result['relation'];
                        $relation_id=$result['id'];
                        echo "<option value='".$relation_id."'>".$relation."</option>";         

                    }?>
                    s</select>
                    </dd>
                    </dl>
                    <dl>
                    <dt><label for="option">choose Occasion:</label></dt>
                    <div style="float:left;width: 400px;margin-top: 9px;">
                    <?php
                        $select=mysql_query('select * from tbl_occasion');
                        while($array=mysql_fetch_array($select))
                        {
                            $occasion_id=$array['id'];
?>


<dd style="width:auto;">
<span id="" style="width:115px; float:left;"><input type="checkbox" name="occasion[]" value="<?php echo $occasion_id;?>" /><?php echo $array['occasion'];?></span>
<input type="file" name="prod_image[]"/>

</dd>

<?php
}
?>
                    </div>
                    </dl>
                    <dl class="submit">
                    <input type="submit" name="submit" id="submit" value="Enter" />
                     </dl>
                </fieldset>

             </form>

当我将数据插入到我的表图像中而不是插入数据库时​​,其他工作正常,但图像不能插入。请帮我解决这个问题。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您应该放弃使用已弃用的mysql_ *函数。请改用mysqli或PDO。不要连接字符串,而是使用带参数的预准备语句。将输入值绑定到参数。那说:

PHP-manual说(http://www.php.net/manual/en/features.file-upload.post-method.php):

默认情况下,文件将存储在服务器的默认临时目录中,除非在php.ini中使用upload_tmp_dir指令给出了另一个位置。

[...]

无论逻辑如何,您都应该从临时目录中删除该文件或将其移动到其他位置。


在使用之前,您应该使用move_uploaded_file()移动上传的文件。

答案 1 :(得分:0)

export default {
    data: function () {
        return {
            dialog: false
        }
    },
    props: {
            notifications: false,
            sound: false,
            widgets: false
        },
    methods: {
        open: function () {
            var vm = this;

            vm.dialog = true;
        }
    }
}