简体   繁体   English

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

[英]how to insert checkbox & file field value in database in php

i have the following code in php file:我在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>

when i am inserting the data into my table image not inserting in database other is fine working well but image not inserting .当我将数据插入到我的表中时,图像没有插入数据库中,其他工作正常,但图像没有插入。 please help me to solve this problem.请帮我解决这个问题。 thanks in advance.提前致谢。

You should abandon the use of the deprecated mysql_* functions.您应该放弃使用已弃用的 mysql_* 函数。 Use mysqli or PDO instead.请改用 mysqli 或 PDO。 Don't concatenate strings, use prepared statements with parameters instead.不要连接字符串,而是使用带参数的准备好的语句。 Bind your input values to the parameters.将您的输入值绑定到参数。 That said:那说:

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

Files will, by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini.默认情况下,文件将存储在服务器的默认临时目录中,除非在 php.ini 中使用 upload_tmp_dir 指令指定了另一个位置。

[...] [...]

Whatever the logic, you should either delete the file from the temporary directory or move it elsewhere.无论逻辑如何,您都应该从临时目录中删除该文件或将其移至别处。


You should move the uploaded file with move_uploaded_file() , before you're working with it.在使用它之前,您应该使用move_uploaded_file()移动上传的文件。

add this code in form tag enctype="multipart/form-data"在表单标签中添加此代码enctype="multipart/form-data"

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

checkbox value : <input type="checkbox" name="chk[]" value="play"> play
<input type="checkbox" name="chk[]" value="game" required>game

file name : <input type="file" name="img" required>

<input type="submit" name="submit" value="submit">

</form>
 

<?php

if(isset($_POST['submit']))

{
# $_FILES — HTTP File Upload variables
$img = $_FILES['img']['name'];

$checkbox = $_POST['chk'];
# implode() returns a string from the elements of an array.
$chk = implode(",",$checkbox);

$link = mysqli_connect("localhost","root","","db_name");

$q ="insert into table (image,checkbox) values ('$img','$chk')";
mysqli_query($link,$q);
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM