<?php include('open_connection.php');
      include('header.php'); ?>
	  
<form method="get"> 
<?php 
		//$message = '<font color=red>Please insert username and password</font>';
		if (isset($_POST['login'])) {
			$username = trim(addslashes($_POST['username']));
			$password = trim(addslashes($_POST['password']));
			
			if ($username != '' && $password != '') {
				include 'open_connection.php';
				$sql = "SELECT user_name, user_password, user_section, user_access  FROM user
						WHERE user_name = '$username' AND user_password = '$password'";
																		
				$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				
				if (mysql_num_rows($result) == 1) {
					$_SESSION['isLogged_in'] = true;
					$_SESSION['username'] = $row['user_name'];
					$_SESSION['section'] = $row['user_section'];
					$_SESSION['access'] = $row['user_access'];
					//$_GET['kodpengguna'] = $row['kodpengguna'];
					//$kodpengguna = $_GET['kodpengguna'];
					
					if( $_SESSION['section'] == 'humancapital' &&  $_SESSION['access'] == 'admin' )
						{	
							// echo "<script language='javascript'>window.location='home_admin.php?kodpengguna=$kodpengguna'/script>";
							echo "<script language='javascript'>window.location='staff_admin.php'</script>";
							}
<?php 

//connects to database 
$con = mysql_connect("localhost","root",""); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
}

mysql_select_db("sally", $con);

//retrieve data from database 
$result = mysql_query("SELECT * FROM people");

Print "<table border cellpadding=3>"; 
while($row = mysql_fetch_array( $result )) 
{ 
Print "<tr>"; 
Print "<td>" . $row['ID'] . "</td>"; 
Print "<td>" . $row['Name'] . "</td>"; 
Print "<td>" . $row['Telephone'] . "</td>"; 
Print "<td>" . $row['Birthday'] . "</td>"; 
Print "<td>" . "<a href='edit_one.php?action=edit&id=" . $row['ID'] . "'>Edit</a>" . "</td>";

Print "</tr>"; 
} 
echo "</table>"; 
/* 
TODO: 
1.) When the page loads, retrieve data from database 
2.) Populate the form with the retrieved data 
3.) When the user is done with the editing and clicks on the Update button, should send all form data back to the database for updating 
4.) Display message on whether update was successful or not 
*/ 
?> 
</form>

edit_one.php:

<input type="hidden" name="ID" value="<? echo "$ID" ?>">

<html><head><title></title></head> 
<body> 
<?php 
if (isset($_GET["abc"])) 
{ 
$ID=$_POST['ID']; 
$Name = $_POST['Name']; 
$Telephone = $_POST['Telephone']; 
$Birthday = $_POST['Birthday'];

$con = mysql_connect("localhost","root",""); 
if (!$con) 
{ 
echo "Fail to connect to mySQL!"; 
} 
mysql_select_db("sally", $con); 
$sql="UPDATE people 
SET Name='$Name', Telephone='$Telephone', Birthday='$Birthday' 
WHERE ID = $ID"; 
$result = mysql_db_query($db, $sql, $con);

if (!mysql_query($sql,$con)) 
{ 
die('Error: ' . mysql_error()); 
} 
else { 
echo "Updated successfully!"; 
} 
//trying to mk the data appear into my textbox 
//mysql_close($con); 
} 
?>

<?php 
$query = mysql_query ("SELECT * from people WHERE ID = $ID"); 
?> 

<table width="300" cellpadding="3" cellspacing="0" border="2"> 
<tr align="center" valign="top"> 
<td colspan="1" rowspan="1" align="center"> 
<form method="get"> 
<p> 
<input type="hidden" name="ID" value="<? echo $ID; ?>"> 
<p><label>Name: </label>&nbsp;

<? echo "<input type=\"text\" name=\"Name\" value=\"$Name\">"; ?> 

</p> 
<p><label>Telephone: </label>&nbsp; 
<? echo "<input type=\"text\" name=\"Telephone\" value=\"$Telephone\">"; ?> 
</p> 
<label>Birthday: </label>&nbsp; 
<? echo "<input type=\"text\" name=\"Birthday\" value=\"$Birthday\">"; ?>

<br> 
<br> 
<input name="abc" type="Submit" value="Update"> 
</p> 
</form></td></tr></table>

</body> 
</html>
