How do I INSERT the location path of the uploaded picture to the user's pic_location in the database?
So I managed to upload the picture via php to a folder BUT my problem now is that and I don't know how the query will look if I want to achieve that...
The member is logged in when he uploads the pic so the SESSION is running.
I will give you my code to have a look see and if you can spot what to do...
Thanks
My picUpload.php (Where I uploaded the picture and where I want to INSERT the location path into the user's pic_loaction column in the table users)
<?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
}
?>
my 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">
and here is my users table in my database.sql file
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)
);
store the variable $target
as pic_location and the query is not insert it should be update query.
As punit wrote, use the variable $target to update the record of the user. In the query, use an UPDATE instead of an INSERT, make sure you define the ID of the user (the INSERT that you have right now simply adds a new row, but you want to modify an existing row, so you need a WHERE clause) and you should definitely sanitize $target before using it in the query, otherwise you have a blind SQL injection.