Jump to content

Data not appearing in my form


Doug

Recommended Posts

Hello,

 

I have been wrestling with the code below for most of this week and cannot see what I have done wrong. I would like the user to be able to "claim" their business by clicking on a link. I can get the correct business uploaded but then the form for changing the details does not populate. (I think it is referring to another database table but do not see why as it is not referenced.

The top half of the code below works showing the required data. Why does this data not replicate in the lower half of the code? Any help greatly appreciated

 

code:


<?php
  
session_start();

  
  ?>

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

  



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

  
if (!isset($_GET['user_id'])) {
    
$query = "SELECT * FROM antique WHERE user_id = '" . $_SESSION['user_id'] . "'";
  
  
}
  else 
{
    $query = "SELECT * FROM antique 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 (!empty($row['name'])) {
      echo '<tr><td class="label">Name:</td><td>' . $row['name'] . '</td></tr>';
    

   
}



   if (!empty($row['phone'])) {
      echo '<tr><td class="label">Phone:</td><td>'  . $row['phone'] .
        ' </td></tr>';
    
}
    
   if (!empty($row['address1'])) {
      echo '<tr><td class="label">Address1:</td><td>'  . $row['address1'] .
        ' </td></tr>';
    
}
   if (!empty($row['address2'])) {
      echo '<tr><td class="label">Address2:</td><td>'  . $row['address2'] .
        ' </td></tr>';
    
}
   if (!empty($row['postcode'])) {
      echo '<tr><td class="label">Postcode:</td><td>'  . $row['postcode'] .
        ' </td></tr>';
    
}
   if (!empty($row['webadd'])) {
        echo '<tr><td class="label">Web address:</td><td>'  . $row['webadd'] .
        ' </td></tr>';
    
}
  if (!empty($row['username'])) {
      echo '<tr><td class="label">Username:</td><td>'  . $row['username'] .
        ' </td></tr>';
    
}
echo '</table>';
  

echo '<class = "label">USER ID: ' . $row['user_id'] . '';

if (!isset($_GET['user_id']) || ($_SESSION['user_id'] == $_GET['user_id'])) {
      echo '<p>Would you like to <a href="index5.php">Go to Homepage</a>?</p>';
    } } 

// End of check for a single row of user results
  
else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }



?>

<hr>


<?php





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

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

$address1 = mysqli_real_escape_string($dbc, trim($_POST['address1']));
    
$address2 = mysqli_real_escape_string($dbc, trim($_POST['address2']));
    
$postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));

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

$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
    
$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']; 


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

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

    
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)) {
              
            }
          }
          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>';
      }
    }



$error = false;


// Update the profile data in the database
    
if (!$error) {
      if (!empty($name) && !empty($phone) && !empty($address1) && !empty($address2) && !empty($postcode)) {
        // 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($age)) {
          $query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', " .
            " email = '$email', webadd = '$webadd', picture = '$new_picture', username = '$username'  WHERE username = '" . 		$_SESSION['username'] .
"'";
        
}}
       
        else {
          
$query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', " .
            " email = '$email', webadd = '$webadd', username = '$username'  WHERE username = '" . 		$_SESSION['username'] .
"'";
        }
       // 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="viewprofile4.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 antique WHERE user_id= '" . $_SESSION['user_id'] . "'";

$data = mysqli_query($dbc, $query);
    
$row = mysqli_fetch_array($data);

    
if ($row != NULL) {
      $name = $row['name'];
      
$phone = $row['phone'];

$address1 = $row['address1'];

$address2 = $row['address2'];

$postcode = $row['postcode'];
      
$email = $row['email'];
      
$webadd = $row['webadd'];

$old_picture = $row['picture'];

$username = $row['username'];

$user_id = $row['user_id'];
    
}
    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="name">Name:</label>
      <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />

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

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


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

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

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

      <label for="webadd">Web address:</label>
      <input type="text" id="webadd" name="webadd" value="<?php if (!empty($email)) echo $webadd; ?>" /><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" />';}
  ?> <br />

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

<label for="user_id">User ID:</label>
      <input type="text" id="user_id" name="user_id" value="<?php if (!empty($user_id)) echo $user_id; ?>" /><br />
   
</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>');
echo '<class = "label">USER ID: ' . $row['user_id'] . '';

?>


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


<?php require_once('footer.php');
?>
</body> 

</html>

Link to comment
Share on other sites

Turning on error reporting helps you see where the errors are.

When you don't know why things don't appear as they should try to print important data that your script depends on to the screen to see if they actually are what and where you think they are.

 

Sorry: refresh!, I get

 

Notice: Undefined index: user_id

 

I still don't know why this would be

 

"Notice: Undefined index: user_id" means there is some array, or so you think, that has a key with name user_id. With other words, such an index doesn't exist, but you wrote the code as if it exists at some point in the code. There should be a line number that tells you where it thinks it exists.

 

if (!isset($_GET['user_id'])) {
    
$query = "SELECT * FROM antique WHERE user_id = '" . $_SESSION['user_id'] . "'";
  
  
}

First time in the code I can see you use it, and while isset is fine about it maybe not existing, then you have written the script to assume there exists a $_SESSION['user_id']. This could be the error, I honestly don't know.

 

Does any of the table stuff print to your screen? Is it just that it doesn't populate them with data from the database?

Link to comment
Share on other sites

I get three errors:

Notice: Undefined index: user_id in C:\Program Files (x86)\EasyPHP5.2.10\www\OneSevenoaks\editBusprofile5.php on line 92

 

Notice: Undefined index: user_id in C:\Program Files (x86)\EasyPHP5.2.10\www\OneSevenoaks\editBusprofile5.php on line 260

 

Notice: Undefined index: username in C:\Program Files (x86)\EasyPHP5.2.10\www\OneSevenoaks\editBusprofile5.php on line 349

 

Both username and user_id do exist (indeed in the first part of the code I asked for the user_id and it is correctly displayed yet on the very next line I get the error!

Link to comment
Share on other sites

I get three errors:

Notice: Undefined index: user_id in C:\Program Files (x86)\EasyPHP5.2.10\www\OneSevenoaks\editBusprofile5.php on line 92

 

Notice: Undefined index: user_id in C:\Program Files (x86)\EasyPHP5.2.10\www\OneSevenoaks\editBusprofile5.php on line 260

 

Notice: Undefined index: username in C:\Program Files (x86)\EasyPHP5.2.10\www\OneSevenoaks\editBusprofile5.php on line 349

 

Both username and user_id do exist (indeed in the first part of the code I asked for the user_id and it is correctly displayed yet on the very next line I get the error!

 

Since you have edited your code a little, the lines have changed slightly as well, but from what I can see, most of those errors are due to you expecting the various elements in the $_SESSION array to already have been set.

 

Go to those lines in your code, and you will see you try to use elements in the $_SESSION array, and they obviously don't exists.

 

I see the one on line 260 actually is used in a query, so that query most likely fails.

Just to prove it swap the line 262 with this:

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

This will output any error during the query.

Link to comment
Share on other sites

forgive me, I'm new. but how come the dat works here:


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

  



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

  
if (!isset($_GET['user_id'])) {
    
$query = "SELECT * FROM antique WHERE user_id = '" . $_SESSION['user_id'] . "'";
  
  
}
  else 
{
    $query = "SELECT * FROM antique 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 (!empty($row['name'])) {
      echo '<tr><td class="label">Name:</td><td>' . $row['name'] . '</td></tr>';
    

   
}



   if (!empty($row['phone'])) {
      echo '<tr><td class="label">Phone:</td><td>'  . $row['phone'] .
        ' </td></tr>';
    
}
    
   if (!empty($row['address1'])) {
      echo '<tr><td class="label">Address1:</td><td>'  . $row['address1'] .
        ' </td></tr>';
    
}
   if (!empty($row['address2'])) {
      echo '<tr><td class="label">Address2:</td><td>'  . $row['address2'] .
        ' </td></tr>';
    
}
   if (!empty($row['postcode'])) {
      echo '<tr><td class="label">Postcode:</td><td>'  . $row['postcode'] .
        ' </td></tr>';
    
}
   if (!empty($row['webadd'])) {
        echo '<tr><td class="label">Web address:</td><td>'  . $row['webadd'] .
        ' </td></tr>';
    
}
  if (!empty($row['username'])) {
      echo '<tr><td class="label">Username:</td><td>'  . $row['username'] .
        ' </td></tr>';
    
}
echo '</table>';
  

echo '<class = "label">USER ID: ' . $row['user_id'] . '';

if (!isset($_GET['user_id']) || ($_SESSION['user_id'] == $_GET['user_id'])) {
      echo '<p>Would you like to <a href="index5.php">Go to Homepage</a>?</p>';
    } } 

// End of check for a single row of user results
  
else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }

?>

 

 

and not here

<?php


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

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

$address1 = mysqli_real_escape_string($dbc, trim($_POST['address1']));
    
$address2 = mysqli_real_escape_string($dbc, trim($_POST['address2']));
    
$postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));

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

$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
    
$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']; 


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

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

    
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)) {
              
            }
          }
          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>';
      }
    }



$error = false;


// Update the profile data in the database
    
if (!$error) {
      if (!empty($name) && !empty($phone) && !empty($address1) && !empty($address2) && !empty($postcode)) {
        // 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($age)) {
          $query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', " .
            " email = '$email', webadd = '$webadd', picture = '$new_picture', username = '$username'  WHERE username = '" . 		$_SESSION['username'] .
"'";
        
}}
       
        else {
          
$query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', " .
            " email = '$email', webadd = '$webadd', username = '$username'  WHERE username = '" . 		$_SESSION['username'] .
"'";
        }
       // 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="viewprofile4.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 antique WHERE user_id= '" . $_SESSION['user_id'] . "'";

$data = mysqli_query($dbc, $query);
    
$row = mysqli_fetch_array($data);

    
if ($row != NULL) {
      $name = $row['name'];
      
$phone = $row['phone'];

$address1 = $row['address1'];

$address2 = $row['address2'];

$postcode = $row['postcode'];
      
$email = $row['email'];
      
$webadd = $row['webadd'];

$old_picture = $row['picture'];

$username = $row['username'];

$user_id = $row['user_id'];
    
}
    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="name">Name:</label>
      <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />

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

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


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

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

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

      <label for="webadd">Web address:</label>
      <input type="text" id="webadd" name="webadd" value="<?php if (!empty($email)) echo $webadd; ?>" /><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" />';}
  ?> <br />

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

<label for="user_id">User ID:</label>
      <input type="text" id="user_id" name="user_id" value="<?php if (!empty($user_id)) echo $user_id; ?>" /><br />
   
</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>');
echo '<class = "label">USER ID: ' . $row['user_id'] . '';

?>


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


<?php require_once('footer.php');
?>
</body> 

</html>

 

It's the same $_SESSION isn't it?

Link to comment
Share on other sites

I think there might have been some miscommunication.

 

On the first script, I don't see session_start anywhere, so I'm assuming you have it in one of your required files. On the second script, I don't see you require that same file...you're saying you are though, correct?

 

Can you post the full code for both pages?

Link to comment
Share on other sites

Yes, of course.

 

It is in fact one long program. I split it up as the first part works (displays data) and the second half where the same information should appear in the form so that it can be edited does not

 

(would it help to split the program up. I don't see how it would have any effect?

 




<?php
  
error_reporting(E_ALL);
session_start();

  
  ?>

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

  



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

  
if (!isset($_GET['user_id'])) {
    
$query = "SELECT * FROM antique WHERE user_id = '" . $_SESSION['user_id'] . "'";
  
  
}
  else 
{
    $query = "SELECT * FROM antique 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 (!empty($row['name'])) {
      echo '<tr><td class="label">Name:</td><td>' . $row['name'] . '</td></tr>';
    

   
}



   if (!empty($row['phone'])) {
      echo '<tr><td class="label">Phone:</td><td>'  . $row['phone'] .
        ' </td></tr>';
    
}
    
   if (!empty($row['address1'])) {
      echo '<tr><td class="label">Address1:</td><td>'  . $row['address1'] .
        ' </td></tr>';
    
}
   if (!empty($row['address2'])) {
      echo '<tr><td class="label">Address2:</td><td>'  . $row['address2'] .
        ' </td></tr>';
    
}
   if (!empty($row['postcode'])) {
      echo '<tr><td class="label">Postcode:</td><td>'  . $row['postcode'] .
        ' </td></tr>';
    
}
   if (!empty($row['webadd'])) {
        echo '<tr><td class="label">Web address:</td><td>'  . $row['webadd'] .
        ' </td></tr>';
    
}
  if (!empty($row['username'])) {
      echo '<tr><td class="label">Username:</td><td>'  . $row['username'] .
        ' </td></tr>';
    
}
echo '</table>';
  

echo '<class = "label">USER ID: ' . $row['user_id'] . '';

if (!isset($_GET['user_id']) || ($_SESSION['user_id'] == $_GET['user_id'])) {
      echo '<p>Would you like to <a href="index5.php">Go to Homepage</a>?</p>';
    } } 

// End of check for a single row of user results
  
else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }



?>

<hr>


<?php


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

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

$address1 = mysqli_real_escape_string($dbc, trim($_POST['address1']));
    
$address2 = mysqli_real_escape_string($dbc, trim($_POST['address2']));
    
$postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));

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

$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
    
$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']; 


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

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

    
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)) {
              
            }
          }
          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>';
      }
    }



$error = false;


// Update the profile data in the database
    
if (!$error) {
      if (!empty($name) && !empty($phone) && !empty($address1) && !empty($address2) && !empty($postcode)) {
        // 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($age)) {
          $query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', " .
            " email = '$email', webadd = '$webadd', picture = '$new_picture', username = '$username'  WHERE username = '" . 		$_SESSION['username'] .
"'";
        
}}
       
        else {
          
$query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', " .
            " email = '$email', webadd = '$webadd', username = '$username'  WHERE username = '" . 		$_SESSION['username'] .
"'";
        }
       // 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="viewprofile4.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 antique WHERE user_id= '" . $_SESSION['user_id'] . "'";

$data = mysqli_query($dbc, $query);
    
$row = mysqli_fetch_array($data);

    
if ($row != NULL) {
      $name = $row['name'];
      
$phone = $row['phone'];

$address1 = $row['address1'];

$address2 = $row['address2'];

$postcode = $row['postcode'];
      
$email = $row['email'];
      
$webadd = $row['webadd'];

$old_picture = $row['picture'];

$username = $row['username'];

$user_id = $row['user_id'];
    
}
    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="name">Name:</label>
      <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />

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

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


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

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

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

      <label for="webadd">Web address:</label>
      <input type="text" id="webadd" name="webadd" value="<?php if (!empty($email)) echo $webadd; ?>" /><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" />';}
  ?> <br />

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

<label for="user_id">User ID:</label>
      <input type="text" id="user_id" name="user_id" value="<?php if (!empty($user_id)) echo $user_id; ?>" /><br />
   
</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>');
echo '<class = "label">USER ID: ' . $row['user_id'] . '';

?>


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


<?php require_once('footer.php');
?>
</body> 

</html>

Link to comment
Share on other sites

Yes, of course.

 

It is in fact one long program. I split it up

 

Ah, well that's a possibility I wasn't accounting for :facepalm:

 

Just to prove it swap the line 262 with this:

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

This will output any error during the query.

 

Did you do this?

Link to comment
Share on other sites

I'm using notepad.

 

So I may be using the wrong line (tried several) but the entire page fails when I try inserting it.

It should be around line 260, and should look like this:

// Grab the profile data from the database
    
$query="SELECT * FROM antique WHERE user_id= '" . $_SESSION['user_id'] . "'";

$data = mysqli_query($dbc, $query);
    
$row = mysqli_fetch_array($data);

In fact, the query might not fail, but it will look like this:

SELECT * FROM antique WHERE user_id=''

I doubt there is any id that equals empty.

 

Your program never finds any variable with this name: $_SESSION['user_id'], which is what the error message tells you (that notice message that appeared after you turned on error reporting).

What that code I told you to add after the execution of the mysqli_query does is to stop the script if there's a syntax error in the query. It might actually not give you an error, because I don't think it checks type. If the user_id is a number, then you don't even need the two ' around it.

 

Back to the point, your script fails, because $_SESSION['user_id'] is not set.

Link to comment
Share on other sites

have a go at this...

<?php
error_reporting(E_ALL);
session_start();
require_once('appvars.php');
require_once('connectvars1.php');

echo "<PRE>";
print_r($_SESSION);
echo "</pre>";
echo "<PRE>";
print_r($_GET);
echo "</pre>";

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

$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 (!empty($row['name'])) { echo '<tr><td class="label">Name:</td><td>' . $row['name'] . '</td></tr>';}
if (!empty($row['phone'])) {	echo '<tr><td class="label">Phone:</td><td>'  . $row['phone'] . ' </td></tr>';}
if (!empty($row['address1'])) { echo '<tr><td class="label">Address1:</td><td>'  . $row['address1'] . ' </td></tr>';}
if (!empty($row['address2'])) { echo '<tr><td class="label">Address2:</td><td>'  . $row['address2'] . ' </td></tr>';}
if (!empty($row['postcode'])) {	echo '<tr><td class="label">Postcode:</td><td>'  . $row['postcode'] . ' </td></tr>';}
if (!empty($row['webadd'])) { echo '<tr><td class="label">Web address:</td><td>'  . $row['webadd'] . ' </td></tr>';}
if (!empty($row['username'])) { echo '<tr><td class="label">Username:</td><td>'  . $row['username'] . ' </td></tr>';}
echo '</table>';
echo '<class = "label">USER ID: ' . $row['user_id'] . '';
if (!isset($_GET['user_id']) || ($_SESSION['user_id'] == $_GET['user_id'])) { 
	echo '<p>Would you like to <a href="index5.php">Go to Homepage</a>?</p>';
}
}else{
echo '<p class="error">There was a problem accessing your profile.</p>';
}
echo "<hr>";
if (isset($_POST['submit'])) {
// Grab the profile data from the POST
$name = mysqli_real_escape_string($dbc, trim($_POST['name']));
$phone = mysqli_real_escape_string($dbc, trim($_POST['phone']));
$address1 = mysqli_real_escape_string($dbc, trim($_POST['address1']));
$address2 = mysqli_real_escape_string($dbc, trim($_POST['address2']));
$postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));
$webadd = mysqli_real_escape_string($dbc, trim($_POST['webadd']));
$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
$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']; 
$username = mysqli_real_escape_string($dbc, trim($_POST['username']));
$user_id = mysqli_real_escape_string($dbc, trim($_POST['user_id']));
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)) {
          }
			}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>';
	}
}
$error = false;
// Update the profile data in the database
if (!$error) {
	if (!empty($name) && !empty($phone) && !empty($address1) && !empty($address2) && !empty($postcode)) {
		// 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($age)) {
			$query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', email = '$email', webadd = '$webadd', picture = '$new_picture', username = '$username'  WHERE username = '" . 		$_SESSION['username'] ."'";
		}
	}else{
		$query = "UPDATE antique SET name = '$name', phone = '$phone', address1 = '$address1', address2 = '$address2', postcode = '$postcode', email = '$email', webadd = '$webadd', username = '$username'  WHERE username = '" . $_SESSION['username'] . "'";
	}
    // 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="viewprofile4.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>';
}
}else {
// Grab the profile data from the database
$query="SELECT * FROM antique WHERE user_id= '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
if ($row != NULL) {
	$name = $row['name'];
	$phone = $row['phone'];
	$address1 = $row['address1'];
	$address2 = $row['address2'];
	$postcode = $row['postcode'];
	$email = $row['email'];
	$webadd = $row['webadd'];
	$old_picture = $row['picture'];
	$username = $row['username'];
	$user_id = $row['user_id'];
}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="name">Name:</label>
		<input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />
	<label for="phone">Phone:</label>
		<input type="text" id="phone" name="phone" value="<?php if (!empty($phone)) echo $phone; ?>" /><br />
	<label for="address1">Address1:</label>
		<input type="text" id="address1" name="address1" value="<?php if (!empty($address1)) echo $address1; ?>" /><br />
	<label for="address2">Address2:</label>
		<input type="text" id="address2" name="address2" value="<?php if (!empty($address2)) echo $address2; ?>" /><br />
	<label for="postcode">Postcode:</label>
		<input type="text" id="postcode" name="postcode" value="<?php if (!empty($postcode)) echo $postcode; ?>" /><br />
	<label for="email">Email:</label>
		<input type="text" id="email" name="email" value="<?php if (!empty($email)) echo $email; ?>" /><br />
	<label for="webadd">Web address:</label>
		<input type="text" id="webadd" name="webadd" value="<?php if (!empty($email)) echo $webadd; ?>" /><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" />';
		}
		?> 
		<br />
	<label for="username">Username:</label>
		<input type="text" id="username" name="username" value="<?php if (!empty($username)) echo $username; ?>" /><br />
	<label for="user_id">User ID:</label>
		<input type="text" id="user_id" name="user_id" value="<?php if (!empty($user_id)) echo $user_id; ?>" /><br />
</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>';
echo '<class = "label">USER ID: ' . $row['user_id'] . '';
?>
<p><a href="index.php">Return to homepage</a></p>
<?php 
require_once('footer.php');
?>
</body> 
</html>

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.