display info from two tables

Discussion in 'PHP' started by tldmic, Feb 13, 2010.

  1. #1
    Good day,
    here I am again looking for help.
    I am new into this system,so please bear with me.
    I am designing a woebsite for me and my friends, thus far I am able to create their profiles.
    I have two tables, users and userlocation,
    both having "id" as common. I want to display the country of the user and his name,

    user table has name and userlocation table has country. I want to retrieve this and edit it and replace the old country or name.

    please see code below,
    please help

    tldmic

    
    <?php 
    /********************** MYSETTINGS.PHP**************************
    This updates user settings and password
    ************************************************************/
    include 'dbc.php';
    page_protect();
    
    $rs_settings = mysql_query("select * from users where id='$_SESSION[user_id]'");
    $loc_settings = mysql_query("select * from userlocation where id='$_SESSION[user_id]'");
    
    if($_POST['doUpdate'] == 'Update')  
    {
    
    $rs_pwd = mysql_query("select pwd from users where id='$_SESSION[user_id]'");
    list($old) = mysql_fetch_row($rs_pwd);
    //check for old password in md5 format
    	if($old == md5($_POST['pwd_old']))
    	{
    	$newmd5 = md5(mysql_real_escape_string($_POST['pwd_new']));
    	mysql_query("update users set pwd='$newmd5' where id='$_SESSION[user_id]'");
    	header("Location: mysettings.php?msg=Your new password is updated");
    	} else
    	{
    	 header("Location: mysettings.php?msg=Your old password is invalid");
    	}
    
    }
    
    if($_POST['doSave'] == 'Save')  
    {
    // Filter POST data for harmful code (sanitize)
    foreach($_POST as $key => $value) {
    	$data[$key] = filter($value);
    }
    
    
    mysql_query("UPDATE users SET `name` ='$data[name]'WHERE id='$_SESSION[user_id]'") or die(mysql_error());
    mysql_query("UPDATE userlocation SET `country` ='$_post[country]'WHERE id='$_SESSION[user_id]'") or die(mysql_error());
    
    header("Location: mysettings.php?msg=Profile Sucessfully saved");
     }
    ?>
    <html>
    <head>
    <title>My Account Settings</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script language="JavaScript" type="text/javascript" src="js/jquery.validate.js"></script>
      <script>
      $(document).ready(function(){
        $("#myform").validate();
    	 $("#pform").validate();
      });
      </script>
    <link href="styles.css" rel="stylesheet" type="text/css">
    </head>
    
    <body bgcolor="lavender">
    <table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
      <tr> 
        <td colspan="3">&nbsp;</td>
      </tr>
      <tr> 
        <td width="160" valign="top"><?php 
    /*********************** MYACCOUNT MENU ****************************
    This code shows my account menu only to logged in users. 
    Copy this code till END and place it in a new html or php where
    you want to show myaccount options. This is only visible to logged in users
    *******************************************************************/
    if (isset($_SESSION['user_id'])) {?>
    <div class="myaccount">
      <p><strong>My Account</strong></p>
      <a href="myaccount.php">My Account</a><br>
      <a href="mysettings.php">Settings</a><br>
        <a href="logout.php">Logout </a>
      <p>You can add more links here for users</p></div>
    <?php } 
    /*******************************END**************************/
    ?>
          <p>&nbsp; </p>
          <p>&nbsp;</p>
          <p>&nbsp;</p>
          <p>&nbsp;</p></td>
        <td width="732" valign="top">
    <h3 class="titlehdr">My Account - Settings</h3>
          <p> 
            <?php	
          if (isset($_GET['msg'])) {
    	  $message = urlencode($_GET['msg']);
    	  echo "<div class=\"msg\">$message</div>";
    	  }
    	  ?>
          </p>
          <p>Here you can make changes to your profile. Please note that you will 
            not be able to change your email which has been already registered.</p>
    	
    
    
    
    
    	
    	  <?php 
    	  
    
    	//  
    	  while (($row_settings = mysql_fetch_array($rs_settings))  && ($rowloc_settings = mysql_fetch_array($loc_settings))) {?>
          <form action="mysettings.php" method="post" name="myform" id="myform">
            <table width="90%" border="0" align="center" cellpadding="3" cellspacing="3" class="forms">
    
    
    		  <tr> 
                <td colspan="2"> name<br> <input name="name" type="text" id="name"  class="required" value="<?php echo $row_settings['name']; ?>" disabled size="50"> 
                </td>
              </tr>
    		  
    		  
    		  <tr> 
                <td colspan="2"> country<br> <input name="country" type="text" id="country"  class="required" value="<?php echo $rowloc_settings['country']; ?>" size="50"> 
                </td>
              </tr>		  
    
              <tr> 
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
            </table>
            <p align="center"> 
              <input name="doSave" type="submit" id="doSave" value="Save">
            </p>
          </form>
    	  <?php }  ?>
    	   
    	  
    </body>
    </html>
    PHP:
    :)


    please help
    many thanks
    tldmic
     
    Last edited: Feb 13, 2010
    tldmic, Feb 13, 2010 IP