Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Currently I have a profile page that displays the information from the profile database table and this works fine.

I also have an edit profile page where, if the user provides an about me, location and a profile image, the profile table in the database is updated and is then displayed on their profile page.

The problem I am having is, if the user wanted to only edit, say, their about me and leave their location and profile image the same, it sets the profile table to blank for the inputs that were not filled in by the user (the profile image and then location in this case).

this is my code:

PHP
$db = Database::getConnection();
		$query = "SELECT * FROM profile WHERE username=:username";
		$output = $db->prepare($query);
		$output->bindParam(':username', $s_username);
		$success = $output->execute();
		if ($success){
			$row = $output->fetch();
				//this part is to get the current info for each column
                $db_about = $row['about'];
				$db_location = $row['location'];
				$db_filePath = $row['image'];
				$rest = substr("".$db_filePath."", 8);  // removes "uploads/"
		}}
		
		$uploadDir = 'uploads/'; //Image Upload Folder
		if(isset($_POST['submit']))
		{
			$about    = $_POST['about'];
			$location = $_POST['location'];
			$fileName = $_FILES['Photo']['name'];
			$tmpName  = $_FILES['Photo']['tmp_name'];
			$fileSize = $_FILES['Photo']['size'];
			$fileType = $_FILES['Photo']['type'];		
			$filePath = $uploadDir . $fileName;
			$result = move_uploaded_file($tmpName, $filePath);

			if (!$result) {
				echo' An error occurred';
				exit();
			}
			if(!get_magic_quotes_gpc())
			{

				$fileName = addslashes($fileName);
				$filePath = addslashes($filePath);
			}
			$db = Database::getConnection();
			$query = "UPDATE profile SET image='$filePath',about='$about',location='$location' WHERE username=:username";
			$output = $db->prepare($query);
			$output->bindParam(':username', $s_username);
			$success = $output->execute();
				if($success){
					echo 'Upload complete<br><br>';
				} else {
					echo 'error occurred';
				}
			}
		
		?>

		<form name="editProfile" enctype="multipart/form-data" action="editProfile.php" method="POST">
			<textarea id="editProfile" name="about" placeholder="About Me"></textarea><br/>
			<input type="text" id="editProfile" name="location" placeholder="Location"><br/>
			<div id="profileUpload">
				<input type="file" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"><br/>
			</div>
			<!-- password to confirm -->
			<INPUT type="submit" class="button" name="submit" value="Submit"> 
		</form>


I want to be able to update the profile table when only changing one of the inputs. the blank inputs that havent been changed should either:

1. Not update in the SQL,
2. Or get the current info from the profile table and for each input that wasnt updated in editprofile, set the variable as what was in the table before the update, meaning that it is being updated with the same information.
Posted
Comments
ZurdoDev 26-Mar-15 14:21pm    
Where are you stuck? There is no magic to do what you want, you just need to write the code to check if it's blank, don't send it.
jba1991 26-Mar-15 14:37pm    
I tried to check if the blank inputs were blank using isset, and if they werent i would make $about the value of $db_about (for example) to be put into the DB but it didnt work.
jba1991 26-Mar-15 14:39pm    
Well I presume I did it wrong, rather than it not working.
Mohibur Rashid 26-Mar-15 20:58pm    
Two approach can be followed:
keeping all the original value in hidden file and and only the changed value to server to update.

the other one would be, sending all the data to server, run a select statement, create the update statement accordingly and update

the second method is plain stupid though.
jba1991 27-Mar-15 7:13am    
I like the sound of the first approach. What do you mean by hidden file? How could I do that?


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900