Jump to content

wrong images!


Doug

Recommended Posts

Hello,

 

I have been wrestling with this forth past couple of days and cannot work out what is wrong.

The code below works ok except when I wish to change the picture. Instead of changing the image with the corresponding images it uses the "nopic" image even though the correct image is still in the database.

So for example if I enter some data with one image attached this appears correctly. If I then change the image the old image reverts to "nopic" and the new image is displayed with the latest data. Even though in the database the old image is still present

 

Hope this makes sense

 

What am I doing wrong?

 

offending code below

 

Any help greatly appreciated

 



// Connect to the database 
  
$dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); 

  

// Retrieve the user data from MySQL
  
$query = "SELECT * FROM conversation where topic = 'polls' ";
  
//$data = mysqli_query($dbc, $query);

  


echo '<table>';
  


$result = mysqli_query($dbc, $query);  
while ($row = mysqli_fetch_array($result)) 
{
    if (is_file(MM_UPLOADPATH . $row['picture']) && filesize(MM_UPLOADPATH . $row['picture']) > 0) {

echo '<tr><td class="label"></td><td><img src="' . MM_UPLOADPATH . $row['picture'] .
        '" alt="Profile Picture" style="width:75px; max-height:55px;" />

' . $row["quote"] .  '';



}
else {
echo '<tr><td class="label"></td><td><img src=" ' . MM_UPLOADPATH  . 'nopic.png" style="width:60px; maxheight=44px;" />

' . $row["quote"] .  '';


}   
}

echo '</tr>'; 


echo '</table>'
?>

Link to comment
Share on other sites

I'm going to guess that the problem is in the code that edits/updates the database. You are perhaps altering the existing image name (adding a space or a cr/lf would be enough) so that the code that outputs the image/nopic no longer finds the corresponding actual image file.

 

What is the rest of your code needed to reproduce this problem, including any code that produces your 'edit' form?

Link to comment
Share on other sites

The editing part does delete the old image if a new one is uploaded but it is in a completely different table so I'm assuming this would have no effect.

What I'm currently doing is I'm taking the image from the edited table and adding it to another table. It is this second table that the code posted is referring to. Once I change the image the address for old images remains in the database so I can't understand why it fails to find this as looking at the database it is plainly there!

Below are the three programs that are interacting:

First the edit profile.

The "adding an entry code"

and finally the code to displays any entries. The image should  automatically display here.

Apologies if there is unnecessary code but I think the problem could be anywhere now!

 

<h3>Edit Profile</h3>


<?php

require_once('appvars.php');
  require_once('connectvars1.php');

  
// Make sure the user is logged in before going any further.
  
if (!isset($_SESSION['user_id'])) {
    echo '<p class="login">Please <a href="login1.php">log in</a> to access this page.</p>';
    
exit();
  
}
  


  // 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
    
$first_name = mysqli_real_escape_string($dbc, trim($_POST['firstname']));
    
$last_name = mysqli_real_escape_string($dbc, trim($_POST['lastname']));

$age = mysqli_real_escape_string($dbc, trim($_POST['age']));

$username = mysqli_real_escape_string($dbc, trim($_POST['username']));
    
$gender = mysqli_real_escape_string($dbc, trim($_POST['gender']));
    
$email = mysqli_real_escape_string($dbc, trim($_POST['email']));

$lookingfor = mysqli_real_escape_string($dbc, trim($_POST['lookingfor']));

$haircolor = mysqli_real_escape_string($dbc, trim($_POST['haircolor']));

$height = mysqli_real_escape_string($dbc, trim($_POST['height']));

$education = mysqli_real_escape_string($dbc, trim($_POST['education']));

$drink = mysqli_real_escape_string($dbc, trim($_POST['drink']));

$children = mysqli_real_escape_string($dbc, trim($_POST['children']));

$smoker = mysqli_real_escape_string($dbc, trim($_POST['smoker']));

$interests = mysqli_real_escape_string($dbc, trim($_POST['interests']));

$aboutme = mysqli_real_escape_string($dbc, trim($_POST['aboutme']));



//$old_password = mysqli_real_escape_string($dbc, trim($_POST['old_password']));

$new_password1 = mysqli_real_escape_string($dbc, trim($_POST['new_password1']));

$new_password2 = mysqli_real_escape_string($dbc, trim($_POST['new_password2']));
    
$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']; 
    
if (!empty($_FILES['new_picture']['tmp_name'])) {list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);}




//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 <= MM_MAXFILESIZE) &&
        ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) {
        if ($_FILES['new_picture']['error'] == 0) {
          

// Move the file to the target upload folder
          
$target = MM_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(MM_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 ' . (MM_MAXFILESIZE / 1024) .
          ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>';
      }
    }

    if ($new_password1 == $new_password2)  { 


echo '<p>passwords ok</p>';
}
else {
$error = true;
echo '<p class="error">The passwords do not match. Please <a href="editprofile3.php"> re enter them</a></p>';
mysqli_close($dbc);
        exit();
}

$error = false;


// Update the profile data in the database
    
if (!$error) {
      if (!empty($first_name) && !empty($last_name) && !empty($username) && !empty($gender) && !empty($email)) {
        // Only set the picture column if there is a new picture
// Only set the password in there is a new one
        if (!empty($new_picture)) {
if (!empty($new_password1)) {
// if (!empty($age)) {
          $query = "UPDATE registration SET first_name = '$first_name', last_name = '$last_name', age = '$age', username = '$username', gender = '$gender', " .
            " email = '$email', lookingfor = '$lookingfor', haircolor = '$haircolor', height = '$height', education = '$education', " .
    " drink = '$drink',   children = '$children',  " .
    " smoker = '$smoker', interests = '$interests', aboutme = '$aboutme', password = '$new_password1', picture = '$new_picture' WHERE user_id = '" . 		$_SESSION['user_id'] .
"'";
        
}}
       
        else {
          
$query = "UPDATE registration SET first_name = '$first_name', last_name = '$last_name', age = '$age', username = '$username', gender = '$gender', " .
            " email = '$email', lookingfor = '$lookingfor', haircolor = '$haircolor', " .
    " height = '$height', education = '$education', drink = '$drink', children = '$children', " .
    " smoker = '$smoker', interests = '$interests', aboutme = '$aboutme', password = '$new_password1' WHERE user_id = '" . $_SESSION['user_id'] . "'";
        }
        mysqli_query($dbc, $query) or die("<br>Query $query<br>Failed with error: " . mysqli_error($dbc) . '<br>On line: ' . __LINE__); 

        

// Confirm success with the user
        
echo '<p>Your profile has been successfully updated. Would you like to <a href="viewprofile1.php">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 * FROM registration WHERE user_id = '" . $_SESSION['user_id'] . "'";
    
$data = mysqli_query($dbc, $query);
    
$row = mysqli_fetch_array($data);

    
if ($row != NULL) {
      $first_name = $row['first_name'];
      
$last_name = $row['last_name'];

$username = $row['username'];

$age = $row['age'];

$gender = $row['gender'];
      
$email = $row['email'];
      
$lookingfor = $row['lookingfor'];

$haircolor = $row['haircolor'];

$height = $row['height'];

$education = $row['education'];

$drink = $row['drink'];

$children = $row['children'];

$smoker = $row['smoker'];

$interests = $row['interests'];

$aboutme = $row['aboutme'];

$new_password1 = $row['password'];

//$old_password = $row['old_password'];

$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 MM_MAXFILESIZE; ?>" />
    
<fieldset>
      <legend>Personal Information</legend>
      <label for="firstname">First name:</label>
      <input type="text" id="firstname" name="firstname" value="<?php if (!empty($first_name)) echo $first_name; ?>" /><br />

      <label for="lastname">Last name:</label>
      <input type="text" id="lastname" name="lastname" value="<?php if (!empty($last_name)) echo $last_name; ?>" /><br />

<label for="username">Username:</label>
      <input type="text" id="username" name="username" value="<?php if (!empty($username)) echo $username; ?>" /><br />

<label for="age">Age:</label>
      <input type="text" id="age" name="age" value="<?php if (!empty($age)) echo $age; ?>" /><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 />

<label for="lookingfor">Looking for:</label> <select id="lookingfor" name="lookingfor">

        <option value="a date" <?php if (!empty($lookingfor) && $lookingfor == 'a date') echo 'selected = "selected"'; ?>>a date</option>
<option value="friends" <?php if (!empty($lookingfor) && $lookingfor == 'lookingfor') echo 'selected = "selected"'; ?>>friends</option>
<option value="a sports partner" <?php if (!empty($lookingfor) && $lookingfor == 'lookingfor') echo 'selected = "selected"'; ?>>sports partner</option>
<option value="nothing in particular" <?php if (!empty($lookingfor) && $lookingfor == 'lookingfor') echo 'selected = "selected"'; ?>>nothing in particular</option>
      </select><br />

<label for="haircolor">Hair colour:</label>
      <input type="text" id="haircolor" name="haircolor" value="<?php if (!empty($haircolor)) echo $haircolor; ?>" /><br />



<label for"height">Height:</label>
<input type="text" id="height" name="height" value="<?php if (!empty($height)) echo $height; ?>" /><br />

<label for="education">Education:</label>
      <input type="text" id="education" name="education" value="<?php if (!empty($education)) echo $education; ?>" /><br />

<label for="drink">Do you drink?:</label>
      <input type="text" id="drink" name="drink" value="<?php if (!empty($drink)) echo $drink; ?>" /><br />

<label for="children">Do you want Children?:</label>
      <input type="text" id="children" name="children" value="<?php if (!empty($children)) echo $children; ?>" /><br />

<label for="smoker">Are you a smoker?</label> <select id="smoker" name="smoker">
	<option value="No" <?php if (!empty($smoker) && $smoker == 'No') echo 'selected = "selected"'; 
?>>No</option>
        <option value="Yes" <?php if (!empty($smoker) && $gender == 'Yes') echo 'selected = "selected"'; ?>>Yes</option>
      </select><br /><br />


<label for="interests">Main interests:</label>


<textarea rows="5"  cols="40"  name="interests"> <?php echo $interests; ?></textarea><br /><br />




<label for="aboutme">About me:</label>
<textarea  rows="5" cols="40" name="aboutme"> <?php echo $aboutme; ?></textarea><br />





      

      <label for="email">Email:</label>
      <input type="text" id="email" name="email" value="<?php if (!empty($email)) echo $email; ?>" /><br />


<label for "currentpwd">Current password:</label>
<?php echo $new_password1; ?><br />

<label for="new_password1">New Password:</label>
<input type="password" id="new_password1" name="new_password1" value="<?php if(!empty($new_password1)) echo $new_password1; ?>" /><br />
<label for="new_passowrd2">Verify New Password:</label>
<input type="password" id="new_password2" name="new_password2" value="<?php if(!empty($new_password1)) echo $new_password1; ?>" /><br />


<input type="hidden" name="old_picture" value="<?php if (!empty($old_picture)) echo $old_picture; ?>" />
      <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="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture"style="max-width:150px; max-height:110px" />';
      } 

    ?>
   
</fieldset>
    
<input type="submit" value="Save Profile" name="submit" />
  
</form>
<?php echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '. <a href="logout3.php">Log out</a>.</p>');

  ?>

<p><a href="index.php">Return to homepage</a></p>


 

<?php
require_once('appvars.php');


require_once('connectvars1.php');

  


error_reporting(E_ALL ^ E_NOTICE);  
$username=" ";
$topic=" ";
$quote=" ";
$picture=" ";
$pic = " ";
$referer = " ";

session_start();
require_once('appvars.php');
require_once('connectvars1.php');

// Make sure the user is logged in before going any further.
  
if (!isset($_SESSION['user_id'])) {
    echo '<p class="login">Please <a href="login1.php">log in</a> to join the conversation.</p>';
    
exit();
  
}



//Connect to the database
$dbc=mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);
if (!isset($_GET['user_id'])) {
    
$query = "SELECT * FROM registration WHERE user_id = '" . $_SESSION['user_id'] . "'";

}
  else 
{
    $query = "SELECT * FROM registration WHERE user_id = '" . $_GET['user_id'] . "'";
  
}
  $data = mysqli_query($dbc, $query);

  

if (mysqli_num_rows($data) == 1) {
    
// The user row was found so display the user data
    
$row = mysqli_fetch_array($data);
    
echo '<table>';
    
{
    if (is_file(MM_UPLOADPATH . $row['picture']) && filesize(MM_UPLOADPATH . $row['picture']) > 0) {

echo '<tr><td class="label"></td><td><img src="' . MM_UPLOADPATH . $row['picture'] .
        '" alt="Profile Picture" style="width:75px; max-height:55px;" />';



}
else {
echo '<tr><td class="label"></td><td><img src=" ' . MM_UPLOADPATH  . 'nopic.png" style="width:60px; maxheight=44px;" />';



}}

echo '</tr>'; 
  

}
if (isset($_POST['submit'])) {

//Grab the profile data from the POST
$username=mysqli_real_escape_string($dbc, trim($_POST['username']));
$topic=mysqli_real_escape_string($dbc, trim($_POST['topic']));
$quote=mysqli_real_escape_string($dbc, trim($_POST['quote']));
$picture=mysqli_real_escape_string($dbc, trim($_POST['picture']));





//Insert the data into the database
$query = "INSERT into conversation values (0,'$username', '$topic', '$quote', '$row[picture]', NOW())";

mysqli_query($dbc, $query);

?>
<div id="conwid">
<?php
//COnfirm success with the user
echo'<p>Your comment has been successfully added.</p>';
echo '<form action="' . $_SERVER["REFERER"] . '" method="get"> <input type="submit" value="Add another"> </form>'; 
echo '<p class="action"><a href="pollconversation2.php">join the conversation</a></p>';
mysqli_close($dbc);
exit();
}

?>
</div>


<div id="addcomment">

<table>


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

<tr>
<td>Username:</td>
<td><input type='text' name='username' size='50' value='
<?php echo '' . $_SESSION['username'] . '' 
    
?>' />
<?php if (empty($username)) {
      
// $name is blank
  
echo '<class="error">You forgot to enter your username.';
    
}
?>

</td>
</tr>

<tr>
<td>Topic:</td>
<td><select name="topic">
<option value="polls">Polls</option>
<option value="sevenoaks">Sevenoaks</option>
<option value="General">Other</option>


<?php $subject = $_POST['topic']; 


?> 
</td>
</tr>
<tr>

<td>Quote:</td>
<td><textarea cols="60" rows="6" wrap="hard" input type='text' name='quote' size='60' value='<?php 
   echo (isset($_POST["quote"])) ? htmlspecialchars($_POST['quote']) : ''; 
   ?>' />  
<?php if (empty($quote)) {
      
// $quote is blank
  
echo 'Please enter a comment';
    
}
?> 
</textarea>




</td>
</tr>




<tr>



<td><input type="submit" value="Add Comment" name="submit"/>
</td>
</tr>

</table>

</form>

</div>

?>

<?php require_once('top1.php');
?>

<h3>Conversation</h3>

<?php require_once('index1.php'); ?>
<?php require_once('navmenu.php'); ?>

<div id="sidebarmain">
<?php require_once('conversationlist.php'); ?>
</div>
<div id="fummenu">

<?php require_once('funmenu.php'); ?>
</div>



<div id="fact">
<div id="facttable">
<b>The polls</b>
<hr>
<?php
$profilepic = "";

require_once('appvars.php');
  
require_once('connectvars1.php');

  
// Start generating the table of results  
echo '<table border="0" cellpadding="2">';  




// Connect to the database 
  
$dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); 

  

// Retrieve the user data from MySQL
  
$query = "SELECT * FROM conversation where topic = 'polls' ";
  
//$data = mysqli_query($dbc, $query);

  


echo '<table>';
  


$result = mysqli_query($dbc, $query);  
$row['picture'] = $profilepic;

while ($row = mysqli_fetch_array($result)) 
{    echo '<tr class="results">';   
{
   if (is_file(MM_UPLOADPATH . $row['picture']) && filesize(MM_UPLOADPATH . $row['picture']) > 0) {

echo '<tr><td class="label"></td><td><img src="' . MM_UPLOADPATH . $row['picture'] .
        '" alt="Profile Picture" style="width:75px; max-height:55px;" />

' . $row["quote"] .  '';


}
else {
echo '<tr><td class="label"></td><td><img src=" ' . MM_UPLOADPATH  . 'nopic.png" alt="Profile Picture not added yet" style="width:60px; maxheight=44px;" />

' . $row["quote"] .  '';


}   
}

echo '</tr>'; 
}

echo '</table>'
?>

</div>



?>

Link to comment
Share on other sites

Your middle code that inserts into the conversation table has a form with username, topic, and quote fields. It does not have a picture field, so of course there is no picture data in the conversation table.

 

You should have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will report and display all the errors that it detects. You should not set error_reporting in your code (except for strictly debugging purposes) and you should certainly not set it to hide any of the types of errors. You will save a ton of time. You would have gotten an undefined error at the reference to $_POST['picture'] that would have alerted you to the problem.

 

Also, if you are making this code for a real site, you need to validate ALL external data that your code receives. In this case your validation logic would have told you that there was no $_POST['picture'] value and your logic would have prevented the insert query from executing without all expected data.

Link to comment
Share on other sites

Thanks you very kuch for these omments...

 

but isn't the pigture already there?It automatically appears from

 

{
    if (is_file(MM_UPLOADPATH . $row['picture']) && filesize(MM_UPLOADPATH . $row['picture']) > 0) {

echo '<tr><td class="label"></td><td><img src="' . MM_UPLOADPATH . $row['picture'] .
        '" alt="Profile Picture" style="width:75px; max-height:55px;" />';



}
else {
echo '<tr><td class="label"></td><td><img src=" ' . MM_UPLOADPATH  . 'nopic.png" style="width:60px; maxheight=44px;" />';


}}

 

I then insert it with this line:

$query = "INSERT into conversation values (0,'$username', '$topic', '$quote', '$row[picture]', NOW())";

 

why does this not work?

Link to comment
Share on other sites

oops, I didn't see that the query was using a different value than what the form processing code is producing. That's what you get when you have unnecessary lines of code in your program.

 

There's no guarantee that when the form is submitted that the SELECT query is putting a value into the $row variable. You will need to debug at what point you have expected data and at what point you don't. When you examine the conversation data table directly, does it have the expected values in the correct columns? Since your insert query doesn't list the columns, we have no way of even knowing if the query is formed correctly. Also, echo out that query to make sure that it has the expected values in it.

 

When you say you are changing the image, what exact step/code are you referring to?

Link to comment
Share on other sites

Hi PFMaBiSmAd

 

Thanks for all you help! much appreciated.

 

 

The data does go into the correct columns (even the picture column has correct img src address!)

 

I change the image in editprofile (first piece code above)

 

relevant code:

$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']; 
    
if (!empty($_FILES['new_picture']['tmp_name'])) {list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);}

 

...

 

<input type="hidden" name="old_picture" value="<?php if (!empty($old_picture)) echo $old_picture; ?>" />
      <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="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture"style="max-width:150px; max-height:110px" />';
      } 

Link to comment
Share on other sites

So, you are uploading a new profile image.

 

Well, your code is unlinking the old image if it's name is different than the new image name (if they are the same name the new image overwrote the old image) so the old image no longer exists and any rows in the conversation table that contain the old image name won't match any image file.

Link to comment
Share on other sites

If you don't want to unlink the old image file when it has a different name then the new image, you would remove the logic or comment out the unlink statement in the code -

 

if (!empty($old_picture) && ($old_picture != $new_picture)) {
              @unlink(MM_UPLOADPATH . $old_picture);
}

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.