Jump to content

Profile Picture missing when intending to edit profile


genzedu777

Recommended Posts

Hi all,

 

I am currently facing a problem, if you look at 'viewprofile.jpg' attachment, you can see that there is an uploaded profile picture.

 

However when I click to edit the profile, the picture is missing (editprofile.jpg), I am just wondering what went wrong? Can someone guide me in troubleshooting this problem?

 

 

<?php
if (isset($_POST['submit'])) {
  
    // Validate and move the uploaded picture file, if necessary
if (!empty($new_picture)) {
      if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
        ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= CT_MAXFILESIZE)) {
        //0 indicates a success, other values indicate failure
	if ($_FILES['file']['error'] == 0) {
          // Move the file to the target upload folder
          $target = CT_UPLOADPATH . basename($new_picture);
          if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {
            // The new picture file move was successful, now make sure any old picture is deleted
            if (!empty($old_picture) && ($old_picture != $new_picture)) {
              @unlink(CT_UPLOADPATH . $old_picture);
            }
          }
          else {
            // The new picture file move failed, so delete the temporary file and set the error flag
            @unlink($_FILES['new_picture']['tmp_name']);
            $error = true;
            echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
          }
        }
      }
      else {
        // The new picture file is not valid, so delete the temporary file and set the error flag
        @unlink($_FILES['new_picture']['tmp_name']);
        $error = true;
        echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (CT_MAXFILESIZE / 1024). '</p>';
      }
    }


// Grab the profile data from the POST
    $name = mysqli_real_escape_string($dbc, trim($_POST['name']));
    $nric = mysqli_real_escape_string($dbc, trim($_POST['nric']));
    $gender = mysqli_real_escape_string($dbc, trim($_POST['gender']));
    

    $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture']));
    $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
    $new_picture_type = $_FILES['new_picture']['type'];
    $new_picture_size = $_FILES['new_picture']['size']; 
    list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);


    $error = false;

    

    // Update the profile data in the database
    if (!$error) {
      if (!empty($name) && !empty($nric) && !empty($gender)) {
        
	$query = "UPDATE tutor_profile SET name = '$name', nric = '$nric', gender = '$gender' WHERE tutor_id = '" . $_GET['tutor_id'] . "'";

	mysqli_query($dbc, $query)
	or die(mysqli_error($dbc));

        // Confirm success with the user
        echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile.php?tutor_id=' . $_GET['tutor_id'] . '">view your profile</a>?</p>';

        mysqli_close($dbc);
        exit();
      }
      else {
        echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
      }
    }
  } // End of check for form submission
  else {
    // Grab the profile data from the database
    $query = "SELECT name, nric, gender FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
    
$data = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));

// The user row was found so display the user data
if (mysqli_num_rows($data) == 1) {
	$row = mysqli_fetch_array($data);

    if ($row != NULL) {
      $name = $row['name'];
      $nric = $row['nric'];
      $gender = $row['gender'];
    }
    else {
      echo '<p class="error">There was a problem accessing your profile.</p>';
    }
  }
  }

  mysqli_close($dbc);
?>

  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo CT_MAXFILESIZE; ?>" />

<ul id="tabSet_ep">
	<li><a href="#panel1">Personal Profile</a></li>
	<li><a href="#panel2">Qualifications</a></li>
	<li><a href="#panel3">Tutor\'s Comments/Commitment</a></li>
	<li><a href="#panel4">Tutoring Levels/Subjects</a></li>
</ul>

<!--Personal Profile-->
<div id="panel1">
      <label for="new_picture">Picture:</label>
      <input type="file" id="new_picture" name="new_picture" />
      <?php if (!empty($old_picture)) {
        echo '<img class="profile" src="' . CT_UPLOADPATH . $old_picture . '" alt="Profile Picture" />';
      } ?><br />
  <label for="firstname">First name:</label>
      <input type="text" id="firstname" name="firstname" value="<?php if (!empty($name)) echo $name; ?>" /><br />
      <label for="lastname">Last name:</label>
      <input type="text" id="lastname" name="lastname" value="<?php if (!empty($nric)) echo $nric; ?>" /><br />
      <label for="gender">Gender:</label>
      <select id="gender" name="gender">
        <option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; ?>>Male</option>
        <option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option>
      </select><br />
    </div>

    <input type="submit" value="Save Profile" name="submit" />
  </form>

 

[attachment deleted by admin]

where you query...

$query = "SELECT name, nric, gender FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
$data = mysqli_query($dbc, $query)

you aren't including the pic name field are you?

Litebearer! hey Hi!

 

What a mistake. Thanks

 

Do you mind if I ask you another question. Currently I am trying to 'save' the profile after editing, and after hitting the submit buttonw, I am prompted with 1 warning message, an another error message from {else} statement.

 

In short, I didnt mange to save the profile due to some reasons, and my database is not able to update the new records.

 

Warning message - Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in D:\inetpub\vhosts\championtutor.com\httpdocs\editprofile.php on line 39

 

{else} error message - You must enter all of the profile data (the picture is optional).

 

Another thing which I noticed is my original URL is www.abc.com/editprofile.php?tutor_id=Tcolez5, after hitting the submit button, it became www.abc.com/editprofile.php. Does it contribute to any of the problem?

 

 

<?php
if (isset($_POST['submit'])) {
  
// Grab the profile data from the POST
    $name = mysqli_real_escape_string($dbc, trim($_POST['name']));
    $nric = mysqli_real_escape_string($dbc, trim($_POST['nric']));
    $gender = mysqli_real_escape_string($dbc, trim($_POST['gender']));
    
    $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture']));
    $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
    $new_picture_type = $_FILES['new_picture']['type'];
    $new_picture_size = $_FILES['new_picture']['size']; 
    list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);


    $error = false;
  
  
    // Validate and move the uploaded picture file, if necessary
if (!empty($new_picture)) {
      if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
        ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= CT_MAXFILESIZE)) {
        //0 indicates a success, other values indicate failure
	if ($_FILES['file']['error'] == 0) {
          // Move the file to the target upload folder
          $target = CT_UPLOADPATH . basename($new_picture);
          if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {
            // The new picture file move was successful, now make sure any old picture is deleted
            if (!empty($old_picture) && ($old_picture != $new_picture)) {
              @unlink(CT_UPLOADPATH . $old_picture);
            }
          }
          else {
            // The new picture file move failed, so delete the temporary file and set the error flag
            @unlink($_FILES['new_picture']['tmp_name']);
            $error = true;
            echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
          }
        }
      }
      else {
        // The new picture file is not valid, so delete the temporary file and set the error flag
        @unlink($_FILES['new_picture']['tmp_name']);
        $error = true;
        echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (CT_MAXFILESIZE / 1024). '</p>';
      }
    }


    // Update the profile data in the database
    if (!$error) {
      if (!empty($name) && !empty($nric) && !empty($gender) && !empty($picture)) {
        
	$query = "UPDATE tutor_profile SET name = '$name', nric = '$nric', gender = '$gender', picture = '$picture' WHERE tutor_id = '" . $_GET['tutor_id'] . "'";


	mysqli_query($dbc, $query)
	or die(mysqli_error($dbc));

        // Confirm success with the user
        echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile.php?tutor_id=' . $_GET['tutor_id'] . '">view your profile</a>?</p>';

        mysqli_close($dbc);
        exit();
      }
      else {
        echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
      }
    }
  } // End of check for form submission
  else {
    // Grab the profile data from the database
    $query = "SELECT name, nric, gender, picture FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
    
$data = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));

// The user row was found so display the user data
if (mysqli_num_rows($data) == 1) {
	$row = mysqli_fetch_array($data);

    if ($row != NULL) {
      $name = $row['name'];
      $nric = $row['nric'];
      $gender = $row['gender'];
  $old_picture = $row['picture'];
    }
    else {
      echo '<p class="error">There was a problem accessing your profile.</p>';
    }
  }
  }

  mysqli_close($dbc);
?>

  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo CT_MAXFILESIZE; ?>" />

<ul id="tabSet_ep">
	<li><a href="#panel1">Personal Profile</a></li>
	<li><a href="#panel2">Qualifications</a></li>
	<li><a href="#panel3">Tutor\'s Comments/Commitment</a></li>
	<li><a href="#panel4">Tutoring Levels/Subjects</a></li>
</ul>

<!--Personal Profile-->
<div id="panel1">
      <label for="new_picture">Picture:</label>
      <input type="file" id="new_picture" name="new_picture" />
      <?php if (!empty($old_picture)) {
        echo '<img class="profile" src="' . CT_UPLOADPATH . $old_picture . '" alt="Profile Picture" />';
      } ?><br />
  <label for="firstname">First name:</label>
      <input type="text" id="firstname" name="firstname" value="<?php if (!empty($name)) echo $name; ?>" /><br />
      <label for="lastname">Last name:</label>
      <input type="text" id="lastname" name="lastname" value="<?php if (!empty($nric)) echo $nric; ?>" /><br />
      <label for="gender">Gender:</label>
      <select id="gender" name="gender">
        <option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; ?>>Male</option>
        <option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option>
      </select><br />
    </div>

    <input type="submit" value="Save Profile" name="submit" />
  </form>
?>

 

 

[attachment deleted by admin]

1. remove the @ from your unlink's

2. no need to unlink the temp image

3. re: the url - when you first come to the page there is a value in the 'get'; however, when you are on the page and hit submit you haven't set a get value - suggest you use session variable to pass tutor_id from page to page

 

make sense?

Hi litebearer,

 

I don't quite understand. Could you please enlighten me? :)

 

For your info, I am logging in as an 'administrator', therefore my $_SESSION is tagged to my admin's username and password, not to the individual tutor. That's the reason I am using $_GET to retrieve tutor's information.

 

Not too sure if you get what I mean :)

 

As for the code, I have indicated $_GET['tutor_id'], why is it not able to update? Any idea? Thanks

// Update the profile data in the database
    if (!$error) {
      if (!empty($name) && !empty($nric) && !empty($gender) && !empty($picture)) {
        
	$query = "UPDATE tutor_profile SET name = '$name', nric = '$nric', gender = '$gender', picture = '$picture', dob_day = '$dob_day' WHERE tutor_id = '" . $_GET['tutor_id'] . "'";

Hi Litebearer,

 

I am still lost in the 'hidden' and '$_SESSION' thingy, could you further advice me what to do?

 

If I don't use $_GET, should I use $_SESSION instead to update my query? I have tried, but it doesn't work.

 

$query = "UPDATE tutor_profile SET name = '$name', nric = '$nric', gender = '$gender', picture = '$picture', dob_day = '$dob_day' WHERE tutor_id = '" . $_SESSION['tutor_id'] . "'";

 

And if I were to use 'hidden', where and how should I place in my code? Really appreciate any help from you, I am really helpless now, still stuck in this area. Thank you so much

 

 

This is my code

<?php
  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  if (isset($_POST['submit'])) {
  
// Grab the profile data from the POST
    $name = mysqli_real_escape_string($dbc, trim($_POST['name']));
    $nric = mysqli_real_escape_string($dbc, trim($_POST['nric']));
    $gender = mysqli_real_escape_string($dbc, trim($_POST['gender']));
    
    $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture']));
    $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
    $new_picture_type = $_FILES['new_picture']['type'];
    $new_picture_size = $_FILES['new_picture']['size']; 
    list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);


    $error = false;
  
  
    // Validate and move the uploaded picture file, if necessary
if (!empty($new_picture)) {
      if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
        ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= CT_MAXFILESIZE)) {
        //0 indicates a success, other values indicate failure
	if ($_FILES['file']['error'] == 0) {
          // Move the file to the target upload folder
          $target = CT_UPLOADPATH . basename($new_picture);
          if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {
            // The new picture file move was successful, now make sure any old picture is deleted
            if (!empty($old_picture) && ($old_picture != $new_picture)) {
              @unlink(CT_UPLOADPATH . $old_picture);
            }
          }
          else {
            // The new picture file move failed, so delete the temporary file and set the error flag
            @unlink($_FILES['new_picture']['tmp_name']);
            $error = true;
            echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
          }
        }
      }
      else {
        // The new picture file is not valid, so delete the temporary file and set the error flag
        @unlink($_FILES['new_picture']['tmp_name']);
        $error = true;
        echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (CT_MAXFILESIZE / 1024). '</p>';
      }
    }


    // Update the profile data in the database
    if (!$error) {
      if (!empty($name) && !empty($nric) && !empty($gender) && !empty($picture)) {
        
	$query = "UPDATE tutor_profile SET name = '$name', nric = '$nric', gender = '$gender', picture = '$picture', dob_day = '$dob_day' WHERE tutor_id = '" . $_GET['tutor_id'] . "'";


	mysqli_query($dbc, $query)
	or die(mysqli_error($dbc));

        // Confirm success with the user
        echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile.php?tutor_id=' . $_GET['tutor_id'] . '">view your profile</a>?</p>';

        mysqli_close($dbc);
        exit();
      }
      else {
        echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
      }
    }
  } // End of check for form submission
  else {
    // Grab the profile data from the database
    $query = "SELECT name, nric, gender, picture, dob_day, dob_mth, dob_year FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'";
    
$data = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));

// The user row was found so display the user data
if (mysqli_num_rows($data) == 1) {
	$row = mysqli_fetch_array($data);

print_r($row);
    if ($row != NULL) {
      $name = $row['name'];
      $nric = $row['nric'];
      $gender = $row['gender'];
  $old_picture = $row['picture'];
  $dob_day = $row['dob_day'];
  $dob_mth = $row['dob_mth'];
  $dob_year = $row['dob_year'];
    }
    else {
      echo '<p class="error">There was a problem accessing your profile.</p>';
    }
  }
  }

  mysqli_close($dbc);
?>

<?php
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo CT_MAXFILESIZE; ?>" />

<ul id="tabSet_ep">
	<li><a href="#panel1">Personal Profile</a></li>
	<li><a href="#panel2">Qualifications</a></li>
	<li><a href="#panel3">Tutor\'s Comments/Commitment</a></li>
	<li><a href="#panel4">Tutoring Levels/Subjects</a></li>
</ul>

<!--Personal Profile-->
<div id="panel1">

<div class="panel1_pic">
  <?php if (!empty($old_picture)) {
        echo '<img class="profile" src="' . CT_UPLOADPATH . $old_picture . '" alt="Profile Picture" height="140" width="120" style="display: block; margin-left: 0; margin-right: 0; padding:8px; border:solid; border-color: #dddddd #aaaaaa #aaaaaa #dddddd; border-width: 1px 2px 2px 1px;"/>';
      } ?>
</div> 
  
  
<!--Table for Panel 1(Personal Profile) Content-->
<table>

<!--Picture-->	
<tr>
  <td class="label">Picture:</td>
      <td><input type="file" id="new_picture" name="new_picture" width="10px" /></td>
    </tr> 

<!--Name-->		
<tr>
  <td class="label">Name:</td>
      <td><input name="name" type="text" id="name" value="<?php if (!empty($name)) echo $name; ?>" /></td>
</tr>

<!--NRIC-->	
<tr>
      <td class="label">NRIC:</td>
      <td><input name="nric" type="text" id="nric" value="<?php if (!empty($nric)) echo $nric; ?>" /></td>
</tr>  

<!--DOB-->
<tr>
      <td class="label">Date Of Birth:</td>
  <td><select id="dob_day" name="dob_day" value="<?php if (!empty($dob_day)) echo $dob_day; ?>">
			<option value=''></option>
			<option>01</option>
			<option>02</option>
			<option>03</option>
			<option>04</option>
			<option>05</option>
			<option>06</option>
			<option>07</option>
			<option>08</option>
			<option>09</option>
			<option>10</option>
			<option>11</option>
			<option>12</option>
			<option>13</option>
			<option>14</option>
			<option>15</option>
			<option>16</option>
			<option>17</option>
			<option>18</option>
			<option>19</option>
			<option>20</option>
			<option>21</option>
			<option>22</option>
			<option>23</option>
			<option>24</option>
			<option>25</option>
			<option>26</option>
			<option>27</option>
			<option>28</option>
			<option>29</option>
			<option>30</option>
			<option>31</option>
  </select>
  
<!--DOB_MTH-->
	  <select id="dob_mth" name="dob_mth">
			<option value='00' ></option><option value='01' >Jan</option><option value='02' >Feb</option><option value='03' >Mar</option><option value='04' >Apr</option><option value='05' >May</option><option value='06' >Jun</option><option value='07' >Jul</option><option value='08' >Aug</option><option value='09' >Sep</option><option value='10' >Oct</option><option value='11' >Nov</option><option value='12' >Dec</option>							
	  </select>

<!--DOB_YEAR-->
      <select id="dob_year" name="dob_year" >
			<option value=''></option>
				<option value='1995' >1995</option><option value='1994' >1994</option><option value='1993' >1993</option><option value='1992' >1992</option><option value='1991' >1991</option><option value='1990' >1990</option><option value='1989' >1989</option><option value='1988' >1988</option><option value='1987' >1987</option><option value='1986' >1986</option><option value='1985' >1985</option><option value='1984' >1984</option><option value='1983' >1983</option><option value='1982' >1982</option><option value='1981' >1981</option><option value='1980' >1980</option><option value='1979' >1979</option><option value='1978' >1978</option><option value='1977' >1977</option><option value='1976' >1976</option><option value='1975' >1975</option><option value='1974' >1974</option><option value='1973' >1973</option><option value='1972' >1972</option><option value='1971' >1971</option><option value='1970' >1970</option><option value='1969' >1969</option><option value='1968' >1968</option><option value='1967' >1967</option><option value='1966' >1966</option><option value='1965' >1965</option><option value='1964' >1964</option><option value='1963' >1963</option><option value='1962' >1962</option><option value='1961' >1961</option><option value='1960' >1960</option><option value='1959' >1959</option><option value='1958' >1958</option><option value='1957' >1957</option><option value='1956' >1956</option><option value='1955' >1955</option><option value='1954' >1954</option><option value='1953' >1953</option><option value='1952' >1952</option><option value='1951' >1951</option><option value='1950' >1950</option><option value='1949' >1949</option><option value='1948' >1948</option><option value='1947' >1947</option><option value='1946' >1946</option><option value='1945' >1945</option><option value='1944' >1944</option><option value='1943' >1943</option><option value='1942' >1942</option><option value='1941' >1941</option><option value='1940' >1940</option><option value='1939' >1939</option><option value='1938' >1938</option><option value='1937' >1937</option><option value='1936' >1936</option><option value='1935' >1935</option><option value='1934' >1934</option><option value='1933' >1933</option><option value='1932' >1932</option><option value='1931' >1931</option><option value='1930' >1930</option>							
	  </select>
	  
	</td> <!--End of <TD> DOB-->
  </tr> <!--End of <TR> DOB-->
  
<!--Gender-->	  
  <tr>
      <td class="label">Gender:</td>
	<td>
	<select id="gender" name="gender">
		<option value="M" <?php if (!empty($gender) && $gender == 'M') echo 'selected = "selected"'; ?>>Male</option>
		<option value="F" <?php if (!empty($gender) && $gender == 'F') echo 'selected = "selected"'; ?>>Female</option>
	</select>
	</td>
  </tr>

<!--Submit Button-->	
  <tr><td><br /><input type="submit" value="Save Profile" name="submit" /></td></tr>
    </div>
  </form>
  
  </table> <!--End of Table for Panel 1(Personal Profile) Content-->

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.