使用php和bootstrapformvalidation将图像上传到mysql数据库

时间:2015-04-18 04:41:18

标签: php mysql ajax image jqbootstrapvalidation

<?php

    session_start();
    if(isset($_SESSION['user']))
    {

?>

<head>
    <link rel="stylesheet" href="css/bootstrap.css" />
    <link rel="stylesheet" href="vali/css/bootstrapValidator.css"/>
    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bootswatch.js"></script>
    <script type="text/javascript" src="vali/js/bootstrapValidator.js">     </script>
    <script src="../SnapClient/gui.js"></script>
    <script src="../SnapClient/cloud.js"></script>
    <script src="../SnapClient/index.html"></script>
</head>
<body>
    <br><br><br><br>
    <!-- Fixed navbar -->
    <?php include("header.html");?>
    <div class="container">

        <div class="panel panel-default">
            <div class="panel-heading">
                <h4> <img  alt="" height="50" width="80"/> Account Settings </h4>  
            </div>
            <div class="panel-body">


               <div class="col-ms-8"><hr><center><h5>Pick Your Profile Picture</h5></center>
                  <div class="well">
                     <form class="form-horizontal" id="fileForm" method="post" action="registrationupdate3.php" enctype="multipart/form-data" >

                         <fieldset>

                             <div class="form-group">
                                   <label for="image" class="col-lg-3 control-label">Profile Picture<span style="color:red;">*</label>
                                   <div class="col-lg-6">
                                      <input type="file" class="form-control" name="image" />                      
                                   </div>

                             </div>

                            <div class="form-group">
                                  <div class="col-lg-10 col-lg-offset-3">
                                       <button  class="btn btn-primary" id="imageupload" name="imageupload" >Update</button>
                           </div>                                                                                                             

                          </fieldset>  
                      </form>
              </div>
             </div>

         </div>
        </div>
    </div>
    </div>

<script type="text/javascript">

    $(document).ready(function() {

    $('#fileForm')
        .bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },


            fields: {
            image: {
                validators: {
                                 file: {
                                           extension: 'jpeg,png',
                                           type: 'image/jpeg,image/png',
                                           maxSize: 100 * 100,   
                                           message: 'The selected file is not valid'
                                      }
                            }
                   }
                 }

        })
        .on('success.form.bv', function(e) {
            // Prevent form submission
            e.preventDefault();

            // Get the form instance
            var $form = $(e.target);


            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');

            // Use Ajax to submit form data
            $.post($form.attr('action'), $form.serialize(), function(result) {

                  if(result=="success")
              {
                alert(result);

              }
              else
              {
                alert(result);

              }

            }, 'json');


        });



       });

 </script>


<?php include("footer.html");?>   
</body>
</html>

 <?php
     } else {
     header("Location:index.html");
     }
 ?>

 php file

<?php 

    session_start();
    include 'db_connection.php';

    if(is_uploaded_file($_FILES['image']['tmp_name'])) {

        $prfimage= addslashes(file_get_contents($_FILES['userImage'] ['tmp_name']));
        $username = $_SESSION['user'];

        $query= mysql_query("UPDATE userdetails set profileimage='{$prfimage}'  WHERE username='".$username."'");

        echo $query;

    }
    else
    {
        echo "the image file is empty";
    }

?>

始终显示为空(所选图像文件为空)。有谁知道为什么?如果有人知道,请帮助我。我需要在mysql中存储用户选择的图像文件。我使用bootstrapvalidator验证图像输入。我检查没有bootstrapvalidator图像存储在mysql中,一切正常,但如果我使用bootstrapvalidator它显示未定义的索引

0 个答案:

没有答案