Unable to update my userdetails table in MysSQL
Posted: Wed Dec 15, 2010 3:13 am
Hello All,
I have come back again to DevNetwork Forum for my rescue. I have a login Page which works fine and takes the users to the members page. In the members page I have given a option to the users to edit there profile. There are only two fields that they need to edit one is Email and second one is Location.
Now my questions are how to go about it. I have my members page code below which the user gets access to once they login:
The below code is for the editprofile.php when the user clicks on the Edit Profile Hyperlink in the above code in member page:
My coding is not so effective I know that I have told from an expert in this forum that I should not use which is again a bad practise, but I have used it. I would appricate any help with the above codes where user's can edit there own profile by loging in.
Regards,
Jagannath
I have come back again to DevNetwork Forum for my rescue. I have a login Page which works fine and takes the users to the members page. In the members page I have given a option to the users to edit there profile. There are only two fields that they need to edit one is Email and second one is Location.
Now my questions are how to go about it. I have my members page code below which the user gets access to once they login:
Code: Select all
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost","root","rajeshwari") or die ("Couldn't connect!");
mysql_select_db("test") or die ("Couldn't find db");
$query = mysql_query("SELECT * FROM userdetails WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
// check to see if they match!
if ($username==$dbusername&&$password==$dbpassword)
{
//$row = mysql_fetch_array($result, MYSQL_ASSOC);
//echo "{$row['firstname']}";
$_SESSION['username']=$dbusername;
echo " ";
echo "<b>You are logged in as:</b> <b>".$_SESSION['username']."</b> <a href='logout2.php'>[Logout]</a>";
$dt1 = time();
$dt2 = time();
$mysql_datetime1 = strftime("%d %B %Y", $dt1);
$mysql_datetime2 = strftime("%H:%M %p", $dt2);
echo " ";
echo "<b>Current Login Date: $mysql_datetime1</b>";
echo " ";
echo "<b>$mysql_datetime2</b>";
}
else
die ("Incorrect password!");
}
else
die("That user doesn't exist!");
}
else
die ("Please enter username and password");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Sanskar - Life with a difference</title>
<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
</head>
<body>
<div id="outer">
<div id="wrapper">
<div id="nav">
<div id="nav-left">
<div id="nav-right">
<ul>
<li><a href="editprofile.php">Edit Profile</a></li>
<li><a href="http://www.freewebsitetemplates.com">PRODUCTS</a></li>
<li><a href="http://www.freewebsitetemplates.com">SERVICES</a></li>
<li><a href="http://www.freewebsitetemplates.com">SHOPPING CART</a></li>
<li><a href="http://www.freewebsitetemplates.com">NEW GADGETS</a></li>
<li><a href="http://www.freewebsitetemplates.com">REGISTER</a></li>
</ul>
</div>
</div>
<div class="clear"></div>
</div>
<div id="head">
<div id="head-left"></div>
<div id="head-right"></div>
<div id="head-1"></div>
<?php
$q = mysql_query("SELECT * FROM userdetails WHERE username='$username'");
$rows = mysql_num_rows($q);
while($rows = mysql_fetch_array($q))
{
echo "WELCOME  ";
echo $rows['firstname'];
echo " !!";
echo " ";
echo "YOUR ACCOUNT DETAILS:";
echo "<br />";
echo "<br />";
echo "First Name:   ";
echo $rows['firstname'];
echo "<br />";
echo "Last Name:   ";
echo $rows['lastname'];
echo "<br />";
echo "Username:   ";
echo $rows['username'];
echo "<br />";
//echo "Password:   ";
//echo $rows['password'];
//echo "<br />";
echo "Email Id:   ";
echo $rows['email'];
echo "<br />";
echo "Location:   ";
echo $rows['location'];
}
?>
</div>
<div id="head-2"></div>
</body>
</html>
The below code is for the editprofile.php when the user clicks on the Edit Profile Hyperlink in the above code in member page:
Code: Select all
<?php
session_start();
$id=$_GET['id'];
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="userdetails"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username = $_POST['username'];
$get_memberdetails_sql = "SELECT * FROM $tbl_name WHERE username = '$username'";
$result = mysql_query($get_memberdetails_sql);
$get_memberdetails_res = mysql_fetch_array($result);
$email = $get_memberdetails_res['email'];
$location = $get_memberdetails_res['location'];
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Sanskar - Life with a difference</title>
<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
<script language = "Javascript">
function Validate()
{
if (document.form1.firstname.value == '')
{
alert('Please fill in your first name');
return false;
}
if (document.form1.lastname.value == '')
{
alert('Please fill in your last name');
return false;
}
if (document.form1.username.value == '')
{
alert('Please fill in your desired username');
return false;
}
if (document.form1.password.value == '')
{
alert('Please fill in your desired password!');
return false;
}
if (document.form1.cpassword.value == '')
{
alert('Please fill in your password again for confirmation!');
return false;
}
if (document.form1.password.value != document.form1.cpassword.value)
{
alert("The two passwords are not identical! "+
"Please enter the same password again for confirmation");
return false;
}
if (document.form1.email.value == '')
{
alert('Please fill in your Email Address');
return false;
}
if (document.form1.location.value == '')
{
alert('Please fill in your Email Address');
return false;
}
return true;
}
</script>
</head>
</head>
<body>
<div id="outer">
<div id="wrapper">
<div id="nav">
<div class="clear"></div>
</div>
<div id="head">
<div id="head-left"></div>
<div id="head-right"></div>
<div id="head-1"></div>
<h1><span class="logo"><span class="top">Sanskar</span><span class="gadgets">life with a difference</span></span></h1>
</div>
<div id="head-2"></div>
<div id="login">
<div id="login-bot">
<div id="login-box">
<?php
echo $_SESSION['username'];
?>
<h2 class="login"><em>User</em> Update Page</h2>
<form name='form1' method="post" action="update_ac.php" onsubmit="return Validate();">
<div id="login-username">
<div><label for="email">Email</label>: <input type="text" name="email" id="email" value="<?php echo $email;?>"></div>
<div><label for="location">Location</label>: <input type="text" name="location" id="location" value="<?php echo $location;?>"></div>
</div>
<div class="clear">
<div id="login-button">
<input type="image" src="images/btn_login.gif" name="l" value="h"/>
</div>
</div>
</form>
</div>
<div id="login-welcome">
<div>
<h2>Welcome</h2>
<p>Don't forget to check <a href="http://www.freewebsitetemplates.com">free website templates</a> every day, because we add a new free website template almost daily.</p>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</body>
</html>
Regards,
Jagannath