如何将上传图片的位置路径插入数据库中用户的pic_location?
所以我设法通过php上传图片到一个文件夹但是我的问题现在就是那个,如果我想实现那个,我不知道查询会是什么样的......
会员在上传图片时登录,以便SESSION正在运行。
我会给你我的代码看看,如果你能发现该怎么做......
由于
我的picUpload.php (我在哪里上传图片以及我想将位置路径插入到用户的pic_loaction列中的用户)
<?php
include 'connect.php';
include 'header.php';
if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)
{
//This is the directory where images will be saved
$target=$_SERVER['DOCUMENT_ROOT'] . "/avatars/" . basename( $_FILES['file']['name']);
//This gets all the other information from the form
$pic_location=($_FILES['file']['name']);
//Writes the information to the database
$sql = "UPDATE users SET pic_location='$target' WHERE user_id=" . $_SESSION['user_id'];
//Writes the photo to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
else
{
//nothing
}
?>
我的connect.php
<?php
session_start();
//connect.php
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'mydatabase';
if(!mysql_connect('localhost', 'root', ''))
{
exit('Error: could not establish database connection');
}
if(!mysql_select_db($database))
{
exit('Error: could not select the database');
}
?>
my header.php
<!DOCTYPE HTML>
<head>
<title> ShareLink </title>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="jquery-1.6.4.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="script.js"></script>
<!-- Back to Top easing jQuery -->
<link rel="stylesheet" type="text/css" media="screen,projection" href="css/ui.totop.css" />
<!-- jquery -->
<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
<!-- easing plugin ( optional ) -->
<script src="js/easing.js" type="text/javascript"></script>
<!-- UItoTop plugin -->
<script src="js/jquery.ui.totop.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
/*
var defaults = {
containerID: 'moccaUItoTop', // fading element id
containerHoverClass: 'moccaUIhover', // fading element hover class
scrollSpeed: 1200,
easingType: 'linear'
};
*/
$().UItoTop({ easingType: 'easeOutQuart' });
});
</script>
</head>
<body>
<h1> ShareLink </h1>
<div id="wrapper">
<div id="menu">
<a class="item" href="/index.php">Home</a> -
<a class="item" href="/create_topic.php">Create a topic</a> -
<a class="item" href="/create_cat.php">Create a category</a> -
<a class="item" href="/members.php"> Members </a> -
<a class="item" href="/search_form.php"> Search </a> -
<a class="item" href="/profile.php"> Profile </a>
<div id="userbar">
<?php
if($_SESSION['signed_in'])
{
echo 'Hello <b>' . htmlentities($_SESSION['user_name']) . '</b>. <a class="item" href="signout.php">Log out</a>';
}
else
{
print'<a class="item" href="signin.php">Log in</a> or <a class="item" href="signup.php">Register</a>';
}
?></div>
</div>
<div id="content">
,这是我的database.sql文件中的用户表
CREATE TABLE users (
user_id INT(8) NOT NULL AUTO_INCREMENT,
user_name VARCHAR(30) NOT NULL,
user_pass VARCHAR(255) NOT NULL,
user_email VARCHAR(255) NOT NULL,
user_date DATETIME NOT NULL,
user_level INT(8) NOT NULL,
pic_location VARCHAR(255) NOT NULL,
UNIQUE INDEX user_name_unique (user_name),
PRIMARY KEY (user_id)
);
答案 0 :(得分:1)
将变量$target
存储为pic_location,查询不插入,应该是更新查询。
答案 1 :(得分:1)
正如punit所写,使用变量$ target来更新用户的记录。在查询中,使用UPDATE而不是INSERT,确保定义用户的ID(您现在拥有的INSERT只是添加一个新行,但是您想要修改现有行,因此您需要一个WHERE子句并且你应该在查询中使用它之前清理$ target,否则你会盲目地注入SQL。