图像未保存在PHP中的数据库中

时间:2018-07-19 05:14:07

标签: php mysqli file-upload

用户登录到他的个人资料后,我正在显示他的用户个人资料。我在那里让他添加他的个人资料照片。用户添加个人资料图片后,我想将图像路径插入数据库。

登录时的数据库 -id,用户名,密码,电子邮件

个人资料图片保存后的数据库-ID,用户名,密码,电子邮件,名称1(保存img路径)

以下代码将图片保存在给定的文件夹中,但没有将图片路径保存在数据库中。有人可以帮我吗?

<?php include('header.php');?>
<?php include('config.php');?>
<?php
    // index.php
    session_start();
    if(!isset($_SESSION['username']))

     {
        header("Location: login.php");
    }
    $username = $_SESSION['username'];
    $sql = "SELECT *  FROM services WHERE user_name = '".$_SESSION['username']."'";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        ?>
    <?php

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

     $name = $_FILES['file']['name'];
    $target_dir = "upload/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);

    // Select file type
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // Valid file extensions
    $extensions_arr = array("jpg","jpeg","png","gif");

    // Check extension
    if( in_array($imageFileType,$extensions_arr) ){

  // Insert record
  //$query = "insert into services(name1) values('".$name."') where user_name = '".$_SESSION['username']."'";
  $query = " UPDATE services SET name1='$name' where user_name = '".$_SESSION['username']."";
  mysqli_query($con,$query);

  // Upload file
  move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);

 }

}
?>




<div class="container">
    <div class="row">
        <div class="col-md-7 ">
            <div class="panel panel-default">
                <div class="panel-heading"> <center> <h4 >User Profile</h4></center></div>
                    <div class="panel-body">

                        <div class="box box-info">

                            <div class="box-body">
                                <div class="col-sm-6">
                                    <div  align="center"> 
                                    <form method="post" action="welcome.php" enctype='multipart/form-data'>
                                            <div class="imageupload panel panel-default">

                                                <div class="file-tab panel-body">
                                                    <label class="btn btn-default btn-file">
                                                    <span>Browse</span>
                        <!-- The file is stored here. -->
                                                    <input type="file" name="file">
                                                    </label>
                                                    <button type="button" class="btn btn-default">Remove</button>
                                                </div>
                                    <input type='submit' value='Save name' name='but_upload'>
                                            </div>
                                    </div>

              <br>

              <!-- /input-group -->
            </div>
            <div class="col-sm-6">
            <h4 style="color:#00b1b1;"><?php echo $row ['name']; ?></h4></span>
              <span><p><?php echo $row ['service']; ?></p></span>            
            </div>
            <div class="clearfix"></div>
            <hr style="margin:5px 0 5px 0;">

1 个答案:

答案 0 :(得分:-1)

更改

$query = " UPDATE services SET name1='$name' where user_name = '".$_SESSION['username']."";

 $query = ("UPDATE services SET name1='$name' where user_name = '".$_SESSION['username']."") or die(mysqli_error($con));

我添加了mysqli_error。它将在查询和括号中打印错误