Insert image path into database

Logged in users only

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 15755 Views - Last Post: 20 October 2009 - 02:29 PM Rate Topic: -----

#1 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Insert image path into database

Posted 14 October 2009 - 04:52 PM

Hi all

I have a members area of my site, where once logged in, there is an option to upload a photo via a form.
<?php 
 session_start(); 
// Check if we have established an authenticated 
if (!isset($_SESSION["authenticatedUser"])) 
{ 
	 $_SESSION["message"] = "Please Login"; 
	 header("Location: players.php"); //Go back and login 
} 
//If this page hasn't been redirected (we are allowed in) then we can display 
?>


<form enctype="multipart/form-data" action="players_upload_photo_action.php" method="post">
	<input type="hidden" name="MAX_FILE_SIZE" value="200000">
	<fieldset>
	<legend>Upload Photo</legend>
	<table>
		<tr>
			<td><label for="player_photo_file">File:</label></td>
			<td><input name="player_photo_file" type="file" /></td>
		</tr>
	</table>
	<p class="submit"><input type="submit" value="Upload" /></p>
	</fieldset>
	</form>



I have it working so that once a file is uploaded, it gets stored exactly where I want it, but I want to be able to put the path into a field in my database, so that I can select to display it on a different page.

This is what my players_upload_photo_action.php page looks like.
<?php
	require 'connection.php';
	
	//Not entirely sure this next line is the right way of going about things!!
		$playerid = $_SESSION['authenticatedUser'];
	
	
	//Check uploaded file is gif/jpg
	if 	 ((($_FILES["player_photo_file"]["type"] == "image/gif")
		|| ($_FILES["player_photo_file"]["type"] == "image/jpeg")
		|| ($_FILES["player_photo_file"]["type"] == "image/pjpeg"))
		&& ($_FILES["player_photo_file"]["size"] < 200000))
	  
	{
		if ($_FILES["player_photo_file"]["error"] > 0)
		{
			echo "Return Code: " . $_FILES["player_photo_file"]["error"] . "<br />";
		}
		else
		{
			if (file_exists("upload/" . $_FILES["player_photo_file"]["name"]))
			{
				echo $_FILES["player_photo_file"]["name"] . " already exists. ";
			}
			else
			{
				move_uploaded_file($_FILES["player_photo_file"]["tmp_name"], "images/players/" . $_FILES["player_photo_file"]["name"]);
				header("location: players_upload_photo.php");
			}
		}
	  }
	else
	  {
		  echo "Invalid file";
	  }
	
	
	$photo = 'images/players/' . $_FILES["player_photo_file"]["name"];
	
	
	
	
	//Insert new article into database
	$query = "INSERT INTO players_personal (player_photo)
			  VALUES ('$photo')
			  WHERE player_id = '$playerid'";
			  
	// Run query through connection
	$result = mysql_query ($query, $connection);
	
	// Redirect if $query is successful
	header("Location: players_account.php");
	
	// Close Connection
	mysql_close($connection);
  
?>



Anyone able to help me achieve this?

If I leave empty the WHERE part of the 'INSERT INTO players_personal', then the image path gets entered correctly, but does so with a new id, which is no good. I need it to be assigned to the user uploading it.

Thanks for any help :)

Is This A Good Question/Topic? 0
  • +

Replies To: Insert image path into database

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Insert image path into database

Posted 14 October 2009 - 09:51 PM

problem is in your query, your query must be something like this:
... $query = "INSERT INTO players_personal (`player_photo`,`player_id`) VALUES ('$photo',$playerid)";...
Was This Post Helpful? 0
  • +
  • -

#3 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Insert image path into database

Posted 15 October 2009 - 09:09 AM

Sorry, I maybe should have made this a bit clearer...the player_id is already set in the database, from the user signing up using a registration form earlier on.

It is only optional adding a photo later on at any time, that is why it is on a form of it's own, once the player is logged in.

By using
$query = "INSERT INTO players_personal (`player_photo`,`player_id`) VALUES ('$photo',$playerid)";


this is just creating a brand new id number with the image path, same as I was doing earlier.

I need the image path put into the player_photo field, even though the rest of the fields are already populated during the initial registration.

Hope this is a bit clearer
Was This Post Helpful? 0
  • +
  • -

#4 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Insert image path into database

Posted 15 October 2009 - 09:31 AM

It almost sounds like you have that field set up to be autoincrement. If that is the case, you can't do what you want. The field value cannot be set in an autoincrement field.

If it is not an autoincrement field the next question is, does a record already exist for this player in the players_personal table? If so you need to do an UPDATE instead of an INSERT.

Without knowing more I'm kinda guessing here. Hope it helps.
Was This Post Helpful? 0
  • +
  • -

#5 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Insert image path into database

Posted 15 October 2009 - 10:14 AM

Thanks for that, I didn't even think about it needing to be an UPDATE rather than INSERT.

You are correct in thinking that the player_id is already set (there are a number of registered users already in the db) and yes, the player_id is autoincremental.

That being the case, I have changed my code to the following:
$query = "UPDATE players_personal
			SET player_photo = $photo
		WHERE player_id = '$playerid'";



Now, I gotta be honest here...I'm not entirely sure that I've set $playerid correctly. This is what may be causing the trouble.

From my first post, you'll see that I have set it like so:
$playerid = $_SESSION['authenticatedUser'];



I'm not convinced this is the way to do it, but I didn't know how else to achieve this.

The players_upload_photo_action.php, (which contains the $query), is only PHP, no HTML and so I do NOT have the page set up as follows:
<?php 
 session_start(); 
// Check if we have established an authenticated 
if (!isset($_SESSION["authenticatedUser"])) 
{ 
	 $_SESSION["message"] = "Please Login"; 
	 header("Location: players.php"); //Go back and login 
}  
?>



This checking the authenticatedUser is on the player_upload_photo.php file, where the form lies.

Hope this helps give a better idea of where I'm getting all mixed up.
Was This Post Helpful? 0
  • +
  • -

#6 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Insert image path into database

Posted 15 October 2009 - 01:32 PM

if you have set the session variable "authenticatedUser" to the playerid then the way you are getting the variable should be fine. If however you are not setting $_SESSION["authenticatedUser"=(player id number) then it will not.

It all depends on what your log in script is doing.
Was This Post Helpful? 0
  • +
  • -

#7 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Insert image path into database

Posted 15 October 2009 - 02:07 PM

Right, I've gone back and double checked my login script, and it seems you are correct in that it is NOT the player_id which is set in the $_SESSION['authenticatedUser'];, but rather it is the players_username.

HOWEVER, realising this mistake, I altered my UPDATE, but I'm still having no luck.

I'm including my login_action.php which is what happens once a user logs in.
Maybe this will help clear things up :blink:

<?php
include 'connection.php';

// Main ----------
session_start();

   	// Get the data collected from the user
   	$appUsername = trim(stripslashes(htmlspecialchars($_POST["player_username"])));
   	$appPassword = trim(stripslashes(htmlspecialchars($_POST["player_password"])));
   	$query = "SELECT * FROM players_personal
   			 WHERE player_username = '$appUsername'
			 AND  player_password = '$appPassword'
			 AND player_account_active = 1
			 ORDER BY players_personal.player_id ";
			 
	$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
	// see if any rows were returned
	if (mysql_num_rows($result) > 0) 
	{
		$row = mysql_fetch_array($result); 
	   	$id = $row["player_id"]; 
	   	$_SESSION["authenticatedUser"] = $appUsername;
		// Relocate to the logged-in page
		header("Location: players_account.php?player_id=$id");
	}
	else
	{
	   $_SESSION["message"] = "Could not connect to Players section as $appUsername ";
	   header("Location: players.php");
	}
		
mysql_free_result($result);
mysql_close($connection);

?>


Was This Post Helpful? 0
  • +
  • -

#8 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Insert image path into database

Posted 17 October 2009 - 09:40 AM

So now you have this as your query?

$query = "UPDATE players_personal
			SET player_photo = $photo
		WHERE player_username = '$playerid'";



Based on the rest of your code, I believe that will update the correct record with what you are looking for.

If it doesn't are there any error codes?
Was This Post Helpful? 0
  • +
  • -

#9 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Insert image path into database

Posted 17 October 2009 - 09:51 AM

Yep, that is my query as it stands at the moment.

The image is being uploaded to where it should be, the page is heading back to the players_account.php as it should be, but no image path is being inserted into the database.

No error messages anywhere. :crazy:

I'm starting to think it might just be easier to put the photo upload facility in the original registration form, just have everything being inserted at the same time.
This is getting way frustrating as it stands.
Was This Post Helpful? 0
  • +
  • -

#10 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Insert image path into database

Posted 17 October 2009 - 09:59 AM

OK, I just had a thought. Are you using backslashes for the path? As in trying to save them would be escaping characters?

Does anything end up in the database at all?
Was This Post Helpful? 0
  • +
  • -

#11 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Insert image path into database

Posted 17 October 2009 - 10:04 AM

This is the slashes I have included in the path:
$photo = 'images/players/' . $_FILES["player_photo_file"]["name"];



This surely must work, as when I was previously using a INSERT rather than UPDATE, the full path was being written to the DB.

Now, unless rules change concerning slashes depending on what kind of query you are using, this shouldn't be a problem. At least, I wouldn't have thought so... ?
Was This Post Helpful? 0
  • +
  • -

#12 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Insert image path into database

Posted 17 October 2009 - 10:10 AM

Nope the rules don't change. And there is nothing that is in the database at all?

I would echo out the query variable so that you can see exactly what is being sent. Maybe there's something not happening the way that we are expecting it. Actually I would echo out all relevant variables so I could see everything making the query as well.
Was This Post Helpful? 0
  • +
  • -

#13 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Insert image path into database

Posted 17 October 2009 - 10:18 AM

Good thinking re: echoing out the variables.

However, trying to echo out the $playerid, the $photo and the $query have all resulted in when a file is uploaded and the submit button is pressed, rather than anything being echoed out, or the players_account page being displayed (as previously), what is happening now is the file upload form is being displayed again.

Nothing at all is being entered into the DB field
Was This Post Helpful? 0
  • +
  • -

#14 TTechGuy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 24-April 09

Re: Insert image path into database

Posted 19 October 2009 - 12:06 PM

Hrm....now I'm perplexed. Setup the update page so that it doesn't redirect anywhere and dump the variables followed by a die command. That way hopefully you will see the variable info.
Was This Post Helpful? 0
  • +
  • -

#15 Lydon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 22-May 08

Re: Insert image path into database

Posted 20 October 2009 - 11:28 AM

Cheers for all your help with this.

It's way annoying how nothing has worked, so I've ended up scrapping the separate ability to upload a photo, and have just put the file upload box in with the original registration form.

Hey presto, everything works!
DB is populated with the correct pathname just as it should be.

Now just have to sort out the stupid error in getting the photo displayed on the members account page.
*Shakes head in disbelief*

Cheers mate :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2