Jump to content

Uploading image then setting image path in a database


mhawkes

Recommended Posts

Hi, I am new here so sorry if my post lacks coherency.

 

I am attempting to upload an image to a directory then take the path of that image and insert it in to a database.

I am making an upload form that adds an event to an sql database and wish to include an image when it is displayed on the site. Currently I have attempted to set a session using the name of the new event when you press upload, where it takes you to another page where you add the image. It checks the session and then uploads the image. The image is uploading fine however I can't get the path to post to the database.

session_start();
if (isset($_SESSION['Name'])){
$eventname = $_SESSION['Name'];
}

$file_upload="true";
$file_up_size=$_FILES['file_up'][size];
echo $_FILES[file_up][name];
if ($_FILES[file_up][size]>250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$file_upload="false";}

if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";}

$file_name=$_FILES[file_up][name];
$add="../upload/events/$file_name"; // the path with the file name where the file will be stored, upload is the directory name.
if($file_upload=="true"){

if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
DATABASE CONNECTION WOULD GO HERE
      	
      	$query = "UPDATE Events SET image='$add' WHERE Name='$eventname'";
      	mysql_query($query) or die('Error, query failed');
      	echo "<br>File $file_name uploaded<br>";
  }
  
   mysql_close($con)
	else{echo "Failed to upload file Contact Site admin to fix the problem";
	}

}
else{echo $msg;}

unset($_SESSION['Name']);
session_destroy();

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

1. try simply echoing out $add to see what value it contains.

 

2. We are PRESUMING that you are actually making the db connection and that the words "DATABASE CONNECTION WOULD GO HERE"  are simply for example here.

 

3. check brace location here "}

mysql_close($con)

else{

echo "Failed to upload file Contact Site admin to fix the problem";

}"

 

 

Link to comment
Share on other sites

I just have and it echo'd nothing, I'm guessing that's the problem.

I need to use the information stored in that session to ensure that the image is inserted in to the same row of the table as the information placed in it on the page before. Is there any way around this?

Link to comment
Share on other sites

It takes the information from a form and puts it through:

 

 mysql_select_db("mhawkes", $con);
   $errors = "";  
   if (!isset($_POST['Name']))  
     $errors .= "Please provide a name for the Event <br/>";  
   if (!isset($_POST['Date']))  
     $errors .= "Please provide a date. <br/>";
   if (!isset($_POST['Venue']))  
     $errors .= "Please provide a venue. <br/>";  
   if ($errors == "") {
     mysql_query("INSERT INTO Events(Name, Genre, Date, Venue, Price, Tickets, EventID, postcode) 
 			 VALUES( 
					 '".addslashes($_POST['Name'])."',
                         '".addslashes($_POST['Genre'])."', 
					 '".addslashes($_POST['Date'])."', 
                         '".addslashes($_POST['Venue'])."',  
					 '".addslashes($_POST['Price'])."',
					 '".addslashes($_POST['Tickets'])."',
					 '',
					 '".addslashes($_POST['postcode'])."'
                         )") 
					 or die(mysql_error());  
session_start();
$_SESSION['Name'] = $_POST['Name'];
     header("location:../eventpic.php");
  	} 
  else {  
      echo $errors."Registration Unsuccessful, Please try again.";  
  		} 

 

Setting the session at the bottom of the page.

Link to comment
Share on other sites

Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

 

You might have to use non-cookie  based sessions if you don't start your page with session_start();

 

 

echo '<br /><a href="page2.php?' . SID . '">page 2</a>';

 

http://php.net/manual/en/function.session-start.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.