Edit User Account Information :: PHP/MYSQL Help

Discussion in 'PHP' started by thrillseeker30, Mar 4, 2009.

  1. #1
    I have a script that is built to update data in a MySQL table. My problem is, I'm unable to simplify the MySQL UPDATE command into one line or else previously stored data becomes "corrupt" (the row updates the column 'name' with the number 0).

    Here is the script and HTML page that goes along with it.

    <?php
    
    // Edit Account Form
    if(isset($_POST['edit_submit'])) {
           
    	require("db_connect.inc.php");
    	
    	$name = mysql_real_escape_string(trim($_POST['name']));
    	$company =  mysql_real_escape_string(trim($_POST['company']));
    	$number = trim($_POST['number']);
    	$location = mysql_real_escape_string(trim($_POST['location']));
    	
    	$edit_error = array();
    	$edit_error_msg = "";
       
    	if(empty($name)) {	
    		$edit_error['name'] = "Plese enter your full name.";
    			
    	} else if(!eregi('^[[:alpha:] \'\-]+$', $name)) {
    				$edit_error['name'] = "Please use valid characters in your full name.";
    	}
    	
    	if(!empty($company)) {
    		if(!eregi('^[[:alpha:] \'\.\,\-]+$', $company)){
    			$edit_error['company'] = "Please use valid characters in the name of your company.";
    		}	
    	} 
    	
    	if(empty($number)) {
    		$edit_error['number'] = "Please enter your phone number.";	
    		
    	} else if(!eregi('^[0-9]{3}-[0-9]{3}-[0-9]{4}$', $number)) {
    				$edit_error['number'] = "Please use valid characters for your phone number.";
    	}
    	
    	if(!empty($location)) {
    		if(!eregi('^[[:alpha:] \,\-]+$', $location)) {
    			$edit_error['location'] = "Please use valid characters in the location name.";
    		} 
    	}
        
    	if(empty($edit_error)) {
    		$change_query = 'UPDATE users SET name = "' . $name . '" WHERE id = "' . $_SESSION['id'] . '"';
    		$change_result = mysql_query($change_query) or die('error:' . mysql_error());
    		
    		$change_query2 = 'UPDATE users SET company = "' . $company . '" WHERE id = "' . $_SESSION['id'] . '"';
    		$change_result2 = mysql_query($change_query2) or die('error:' . mysql_error());
    		
    		$change_query3 = 'UPDATE users SET number = "' . $number . '" WHERE id = "' . $_SESSION['id'] . '"';
    		$change_result3 = mysql_query($change_query3) or die('error:' . mysql_error());
    		
    		$change_query4 = 'UPDATE users SET location = "' . $location . '" WHERE id = "' . $_SESSION['id'] . '"';
    		$change_result4 = mysql_query($change_query4) or die('error:' . mysql_error());
    
    		if(mysql_affected_rows() == 1) {
    			$_SESSION['name'] = $name;
    			$_SESSION['company'] = $company;
    			$_SESSION['number'] = $number;
    			$_SESSION['location'] = $location;		
    		}
    	} else {
    		$edit_error_msg = "<ul>\n";
    		foreach($edit_error as $msg) {
    			$edit_error_msg .= "<li>$msg</li>\n";
    		}	
    		$edit_error_msg .= "</ul>\n";
    	}	     
    }
    
    ?>
    PHP:

     
    thrillseeker30, Mar 4, 2009 IP