Jump to content

Bit of help with sessions....


blackdogupya

Recommended Posts

Hey Guys,

 

Me again!  Still working on this bloody database! :)

 

Okay, so I have a site that people can add a record to a database.  This record is filled using a form and the form contains an image that can be uploaded.

 

This works fine.

 

Then there's the ability to search a record based on a boolean search which displays a table with the record data and displays a thumbnail of the photo.

 

This also works fine.

 

Then I have a script that (once it's working) will allow you to edit the record.

 

This is where I'm having issues.  Here's my process for the form:

 

[*]User searches for the record by using a boolean search

[*]Search finds the record and displays a form containing the original values in the database

[*]User changes some parts of the original record using the form

[*]Form then updates the database with the new values

 

The problem I'm having is with the photo function.  If there's no photo attached, I was getting an error saying that the photo field could not be empty.  So I used the following process:

 

[*]User searches for the record using edit.php

[*]Form is displayed using edit_process.php

[*]edit_process.php is posted to update.php that has conditions to check if the file upload field is empty or not

[*]If the field is empty, then it requires updatenophoto.php

[*]If the field has a new image, it uses updatephoto.php

 

When I submit the form to the update.php script, it does nothing and gives me a blank page.

 

Here's my code for each of the parts (hit the character limit, code in comments):

 

 

Link to comment
Share on other sites

i would put all the select names i think they are cities into a table and query all of them to make your code shorter and cleaner.

<?
session_start();
$_SESSION['oldID'] = $_POST['oldID'];
$_SESSION['animalID'] = $_POST['animalID'];
$_SESSION['status'] = $_POST['status'];
$_SESSION['date'] = $_POST['date'];
$_SESSION['species'] = $_POST['species'];
$_SESSION['breed'] = $_POST['breed'];
$_SESSION['sex'] = $_POST['sex'];
$_SESSION['primary_colour'] = $_POST['primary_colour'];;
$_SESSION['colour'] = $_POST['colour'];
$_SESSION['distinctive_traits'] = $_POST['distinctive_traits'];
$_SESSION['fur_length'] = $_POST['fur_length'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['desexed'] = $_POST['desexed'];
$_SESSION['microchipped'] = $_POST['microchipped'];
$_SESSION['suburb'] = $_POST['suburb'];
$_SESSION['pound_area'] = $_POST['pound_area'];
$_SESSION['contact'] = $_POST['contact'];
$_SESSION['link'] = $_POST['link'];
$_SESSION['notes'] = $_POST['notes'];
$_SESSION['uploaded_file'] = $_FILES['uploaded_file'];
$_SESSION['imageFile'] = $_FILES['uploaded_file']['name'];
$_SESSION['imageMime'] = $_FILES['uploaded_file']['type'];
$_SESSION['imgData'] = $_FILES['uploaded_file']['tmp_name'];
$_SESSION['imgSize'] = $_FILES['uploaded_file']['size'];

//If the photo field is empty, we call the no photo stuff
if($_SESSION['imgSize'] === 0){
header("Location: nophoto.php");
}else{
header("Location: Photo.php");
}
?>

 

Link to comment
Share on other sites

Thanks for that!  I'm almost there!

 

It seems to update the database with the form values, but I'm thinking I'm going to have to write the image file to a temp directory and store that in the session, unless there's a way to carry the file across in a session without uploading to a temp directory?

 

Would anyone be able to help me out with a bit of code to take the file, put it in the images/tmp directory and store it in a session?

 

I'm looking at articles online, but it's not making a huge amount of sense to me!  I'm thinking it will go in this script that handles the POST from the form:

 

<?
session_start();
$_SESSION['oldID'] = $_POST['oldID'];
$_SESSION['animalID'] = $_POST['animalID'];
$_SESSION['status'] = $_POST['status'];
$_SESSION['date'] = $_POST['date'];
$_SESSION['species'] = $_POST['species'];
$_SESSION['breed'] = $_POST['breed'];
$_SESSION['sex'] = $_POST['sex'];
$_SESSION['primary_colour'] = $_POST['primary_colour'];;
$_SESSION['colour'] = $_POST['colour'];
$_SESSION['distinctive_traits'] = $_POST['distinctive_traits'];
$_SESSION['fur_length'] = $_POST['fur_length'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['desexed'] = $_POST['desexed'];
$_SESSION['microchipped'] = $_POST['microchipped'];
$_SESSION['suburb'] = $_POST['suburb'];
$_SESSION['pound_area'] = $_POST['pound_area'];
$_SESSION['contact'] = $_POST['contact'];
$_SESSION['link'] = $_POST['link'];
$_SESSION['notes'] = $_POST['notes'];
$_SESSION['uploaded_file'] = $_FILES['uploaded_file'];
$_SESSION['imageFile'] = $_FILES['uploaded_file']['name'];
$_SESSION['imageMime'] = $_FILES['uploaded_file']['type'];
$_SESSION['imgData'] = $_FILES['uploaded_file']['tmp_name'];
$_SESSION['imgSize'] = $_FILES['uploaded_file']['size'];

//If the photo field is empty, we call the no photo stuff
if($_SESSION['imgSize'] === 0){
header("Location: updatenophoto.php");
}else{
header("Location: updatephoto.php");
}
?>

Thanks again for your help so far!

 

Cheers,

 

Dave

 

Link to comment
Share on other sites

You look like you are already putting the image into the $_SESSION variable

$_SESSION['uploaded_file'] = $_FILES['uploaded_file'];
$_SESSION['imageFile'] = $_FILES['uploaded_file']['name'];
$_SESSION['imageMime'] = $_FILES['uploaded_file']['type'];
$_SESSION['imgData'] = $_FILES['uploaded_file']['tmp_name'];
$_SESSION['imgSize'] = $_FILES['uploaded_file']['size'];

The trick is getting your head around the fact that to a computer an image is just a bigger word.  It can store the binary data that makes up an image just as easily as it can the binary data that makes up your text (it just needs a bit more memory to do it).

Link to comment
Share on other sites

That's what I thought, but when that script calls the following:

 

<?php
session_start();
//Require the formatting code
require ('format.php');
// This is how we connect to the database.  Please don't modify this.
$dbms = 'mysqli';
$hostname = 'localhost';
$db_user = 'l******';
$database = 'l******';
$db_password = '******';
$load_extensions = '';
//Connect to the database using the details above
$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db); 
//Make sure we're posting the form

// Now we convert the HTML form field values to variables so we can use the details to populate the database.
$oldID = $_SESSION['oldID'];
$animalID = $_SESSION['animalID'];
$status = $_SESSION['status'];
$date = $_SESSION['date'];
$species = $_SESSION['species'];
$breed = $_SESSION['breed'];
$sex = $_SESSION['sex'];
$primary_colour = $_SESSION['primary_colour'];
$colour = $_SESSION['colour'];
$distinctive_traits = $_SESSION['distinctive_traits'];
$fur_length = $_SESSION['fur_length'];
$age = $_SESSION['age'];
$desexed = $_SESSION['desexed'];
$microchipped = $_SESSION['microchipped'];
$suburb = $_SESSION['suburb'];
$pound_area = $_SESSION['pound_area'];
$contact = $_SESSION['contact'];
$link = $_SESSION['link'];
$notes = $_SESSION['notes'];
$imgfile = $_SESSION['imgFile'];
$imgmime = $_SESSION['imgMime'];
$imgdata = $_SESSION['imgData'];
$imgsize = $_SESSION['imgSize'];
$name = mysql_real_escape_string($_SESSION['imgfile']);
$mime = mysql_real_escape_string($_SESSION['imgmime']);
$data = mysql_real_escape_string(file_get_contents($_SESSION['imgdata']));
$size = intval($imgsize);
$when = 'now()';
//Now we have connected, we're going to update the record that matches what was entered into the Animal ID field
$sql = "UPDATE animal_info SET status='$status', date='$date', species='$species', breed='$breed', sex='$sex', primary_colour='$primary_colour', colour='$colour', distinctive_traits='$distinctive_traits', fur_length='$fur_length', age='$age', desexed='$desexed', microchipped='$microchipped', suburb='$suburb', pound_area='$pound_area', contact='$contact', link='$link', notes='$notes', img_name='$name', img_mime='$mime', img_size='$size', img_data='$data', img_created='$when' WHERE `animalID` = '$oldID'";
//Execute the SQL statement to update the record
$execute = mysql_query($sql) or die(mysql_error());
//This is the query that grabs the new vaules of the record
$result = "SELECT * FROM `animal_info` WHERE `animalID` LIKE '$oldID' LIMIT 0,1";
//Now we run another query to grab the new details from the record that has been updated
$results = mysql_query($result) or die(mysql_error());
//Build the results of the new record
while($row = mysql_fetch_array($results))
{ 
//Build the HTML for the results in the table
echo "$resultsHTMLabove";
echo "Here's your new record:";
echo "<br><BR>";
//Here's the table
echo "<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"2\">";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Animal ID</th>";
echo "<td>".$row['animalID']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Status</th>";
echo "<td>".$row['status']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Date</th>";
echo "<td>".$row['date']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Species</th>";
echo "<td>".$row['species']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Breed</th>";
echo "<td>".$row['breed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Sex</th>";
echo "<td>".$row['sex']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Primary Colour</th>";
echo "<td>".$row['primary_colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Colour</th>";
echo "<td>".$row['colour']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Distinctive Traits</th>";
echo "<td>".$row['distinctive_traits']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Fur Length</th>";
echo "<td>".$row['fur_length']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Age</th>";
echo "<td>".$row['age']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Desexed?</th>";
echo "<td>".$row['desexed']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Microchipped?</th>";
echo "<td>".$row['microchipped']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Suburb</th>";
echo "<td>".$row['suburb']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Pound Area</th>";
echo "<td>".$row['pound_area']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Contact</th>";
echo "<td>".$row['contact']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Link</th>";
echo "<td><a href='".$row['link']."' target=_blank >Link to Facebook</a></td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Notes</th>";
echo "<td>".$row['notes']."</td>";
echo "</tr>";
echo "<tr>";
echo "<th scope=\"row\" align=\"left\">Photo</th>";
echo "<td>";
echo "<img src=\"getimage.php?id=".$row['ID']."\" width=\"200px\" height=\"150px\" />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<BR><BR>";
//Build the footer part of the page
echo "$resultsHTMLbelow";
} 

?>

 

I get the following error:

 

Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /home/loveani/public_html/poplaf/updatephoto.php on line 43

 

Which corresponds to this line:

 

$data = mysql_real_escape_string(file_get_contents($_SESSION['imgdata']));

Link to comment
Share on other sites

$_FILES['uploaded_file']['tmp_name'] contains the location of the uploaded image, which will be destroyed once the script on your page ends. You would need to read the image data and store it into a session variable -

 

$_SESSION['raw_image_data'] = file_get_contents($_FILES['uploaded_file']['tmp_name']);

Link to comment
Share on other sites

You would need to read the image data and store it into a session variable -

 

$_SESSION['raw_image_data'] = file_get_contents($_FILES['uploaded_file']['tmp_name']);

 

Where would I put this?  In the script that handles the post or in the script that updates the record?

 

Would I then use

 

$rawData = $_SESSION['raw_image_data'];

 

And use that in the update query for the database?

 

Sorry if I'm sounding a little dense! :)

 

Cheers,

 

Dave

Link to comment
Share on other sites

Okay, so my code now looks like this:

 

<?
session_start();
$_SESSION['oldID'] = $_POST['oldID'];
$_SESSION['animalID'] = $_POST['animalID'];
$_SESSION['status'] = $_POST['status'];
$_SESSION['date'] = $_POST['date'];
$_SESSION['species'] = $_POST['species'];
$_SESSION['breed'] = $_POST['breed'];
$_SESSION['sex'] = $_POST['sex'];
$_SESSION['primary_colour'] = $_POST['primary_colour'];;
$_SESSION['colour'] = $_POST['colour'];
$_SESSION['distinctive_traits'] = $_POST['distinctive_traits'];
$_SESSION['fur_length'] = $_POST['fur_length'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['desexed'] = $_POST['desexed'];
$_SESSION['microchipped'] = $_POST['microchipped'];
$_SESSION['suburb'] = $_POST['suburb'];
$_SESSION['pound_area'] = $_POST['pound_area'];
$_SESSION['contact'] = $_POST['contact'];
$_SESSION['link'] = $_POST['link'];
$_SESSION['notes'] = $_POST['notes'];
$_SESSION['uploaded_file'] = $_FILES['uploaded_file'];
$_SESSION['imageFile'] = $_FILES['uploaded_file']['name'];
$_SESSION['imageMime'] = $_FILES['uploaded_file']['type'];
$_SESSION['imgSize'] = $_FILES['uploaded_file']['size'];
$_SESSION['imgData'] = file_get_contents($_FILES['uploaded_file']['tmp_name']);
//If the photo field is empty, we call the no photo stuff
if($_SESSION['imgSize'] === 0){
header("Location: updatenophoto.php");
}else{
header("Location: updatephoto.php");
}
?>

 

And I get the following error when submitting:

 

Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /home/loveani/public_html/poplaf/update.php on line 26

Warning: Cannot modify header information - headers already sent by (output started at /home/loveani/public_html/poplaf/update.php:26) in /home/loveani/public_html/poplaf/update.php on line 31

Link to comment
Share on other sites

The warning is because your code is not testing if the file uploaded before accessing the uploaded file information.

 

To avoid that warning, you would need to put that line of code inside the else { ... } logic, right before the header() redirect.

 

The warning also means that the upload isn't working or you didn't select a file to upload.

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.