Hi all. I have a script which is meant to take data from a form, check to see if a record matching the username is in a table, and update that record else if there is no matching user in the table, to insert a new record. The problem I have is that if the user does not re-enter all of their previous profile data along with the items they want to update, it overwrites that profile data with a blank field. Here is the script. How do I get it to just update the changed profile fields? Thanks for any suggestions.
<?php
session_start();
if (!isset($_SESSION['login']) || $_SESSION['login'] != '') {
header ("Location: login.php");
}
$email = $_REQUEST['email'];
$website = $_REQUEST['website'];
$gender = $_REQUEST['gender'];
$age = $_REQUEST['age'];
$location = $_REQUEST['location'];
$favbirds = $_REQUEST['favbirds'];
$favplaces = $_REQUEST['favplaces'];
$favguide = $_REQUEST['favguide'];
$favequip = $_REQUEST['favequip'];
$favfest = $_REQUEST['favfest'];
$orgs = $_REQUEST['orgs'];
$sum = $_REQUEST['sum'];
$wint = $_REQUEST['wint'];
$username = $_SESSION['username'];
//Connect and select database
mysql_connect("192.168.254.000", "uname", "pwd") or die(mysql_error());
mysql_select_db("tutorial") or die(mysql_error());
// Check matching of username
$result=mysql_query("select * from profiles where user = '$username'");
if(mysql_num_rows($result)!='0'){ // If match.
//update record
$query="UPDATE profiles SET email = '$email', website = '$website', gender = '$gender', age = '$age', location = '$location', favbirds = '$favbirds', favplaces = '$favplaces', favguide = '$favguide', favequip = '$favequip', favfest = '$favfest', orgs = '$orgs', sum = '$sum', wint = '$wint' WHERE user = '$username'";
mysql_query($query) or die(mysql_error());
header("Location: profile.php");
// $message="Profile updated successfully";
}else{ // If not match.
//insert data into table
$query="INSERT INTO profiles (email, website, gender, age, location, favbirds, favplaces, favguide, favequip, favfest, orgs, sum, wint, user) VALUES ('".$email."', '".$website."', '".$gender."', '".$age."', '".$location."', '".$favbirds."', '".$favplaces."', '".$favguide."', '".$favequip."', '".$favfest."', '".$orgs."', '".$sum."', '".$wint."', '$username')";
mysql_query($query) or die(mysql_error());
header("Location: profile.php");
// $message="Profile updated successfully";
}
?>