I am currently trying to make an update page for user profiles on my site and. The code below works fine if the user updates all of their info, but if they leave out a field it inserts a blank record into the table.
Currently to get past this problem if the user has left a field blank I replace the blank field with $_SESSION['user']['field']
so it just re-inserts current data.
Here is my php at the moment
<?php
session_start();
if($_SESSION['uname']) {
$logged_in=true;
} else {
$logged_in=false;
}
include_once("../connection/conn.php");
if(isset($_POST['update'])) {
if($_POST['firstname']){ $firstname = $_POST['firstname']; }
else { $firstname = $_SESSION['uname']['firstname']; }
if($_POST['lastname']){ $lastname = $_POST['lastname']; }
else { $lastname = $_SESSION['uname']['lastname']; }
if($_POST['email']){ $email= $_POST['email']; }
else { $email = $_SESSION['uname']['email']; }
$id = $_SESSION['uname']['id'];
$query = "UPDATE users SET firstname=?, lastname=?, email=? WHERE id=? ";
$results = $condb->prepare($query);
$results->execute(array($firstname, $lastname,$email,$id));
if($results) {
echo "updated";
}
}
?>