【问题标题】:image does not save in the database in PHP图像不保存在 PHP 的数据库中
【发布时间】:2018-07-19 05:14:06
【问题描述】:

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

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

头像保存后的数据库 - id, username,password,email,name1(img path save)

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

<?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;">

【问题讨论】:

  • "但是它没有将图片路径保存在数据库中"那么它是做什么的呢?
  • 嘿,回显你的 $name 并更新 $query 并粘贴到这里
  • 这可能是因为您的查询因末尾缺少' 而失败。如果您检查了mysqli_query($con,$query) 的返回值,您就会知道这一点。如果您使用prepared statements with bound parameters,您将完全避免这个问题(和其他问题)
  • 查询不安全。使用带有占位符和绑定的准备好的语句。我希望在包含之前看到会话开始。如果您正在调用 while 循环,则无需检查 num_rows。
  • 投票以错别字结束

标签: php mysqli file-upload


【解决方案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。它将在查询和括号中打印错误

【讨论】:

  • @Lanka 非常感谢你,现在它显示了这个 Undefined variable: name
  • 如果我的回答解决了您的问题,请将我的回答标记为已接受的解决方案。谢谢@sadee
  • 表示$name变量中没有任何内容。
  • @Lanka 我做到了。你来自斯里兰卡,对吧?我不知道为什么一旦我们问一些我们不知道的事情,别人总是得到负分
  • @sadee - 是的,我来自斯里兰卡。我也无法理解为什么人们会无缘无故地否决我们的答案。
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 2020-06-01
  • 2021-06-08
  • 2021-06-09
  • 1970-01-01
相关资源
最近更新 更多