【发布时间】: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