<?php
session_start();
ob_start();
require 'config.php';

?>
<html>
	<head>
		<title>Bilder i database</title>
	</head>
	<body>
		<?php
		//vise kontakter
		$select = mysql_query("SELECT * FROM adresser ORDER BY id DESC");
		echo '<table border="1">
		<tr>
			<th width="100px">Navn</th>
			<th width="100px">Telefon</th>
			<th>Email</th>
			<th width="200px" colspan="3">Admin</th>
		</tr>
		<tr>
			<th colspan="6"><a href="?p=leggtil">Legg til en kontakt</a></th>
		</tr>';
		while($select2 = mysql_fetch_array($select))
		{
			$navn = $select2['fnavn'].' '.$select2['enavn'];
			$tlf = $select2['tlf'];
			$mail = $select2['email'];
			$id = $select2['id'];
			?>
			<tr>
				<td><?php echo $navn; ?></td>
				<td><?php echo $tlf; ?></td>
				<td><?php echo $mail; ?></td>
				<td><a href="?p=endre&id=<?php echo $id; ?>">Endre</a></td>
				<td><a href="?p=fjern&id=<?php echo $id; ?>">Fjern</a></td>
			</tr>
			<?php
		}
		$p = htmlspecialchars($_GET['p']);
		$id = htmlspecialchars($_GET['id']);
		if($p == "leggtil")
		{
			?>
			<form action="?p=leggtil" method="post">
			<pre>
			Fornavn:	<input name="fnavn" type="text"><br>
			Etternavn:	<input name="enavn" type="text"><br>
			Tlf:		<input name="tlf" type="text"><br>
			Email: 		<input name="email" type="text"><br>
			<input name="submit" type="submit" value="Legg til">
			</pre>
			</form>
			<?php
			$fnavn = htmlspecialchars($_POST['fnavn']);
			$enavn = htmlspecialchars($_POST['enavn']);
			$tlf = htmlspecialchars($_POST['tlf']);
			$email = htmlspecialchars($_POST['email']);
			$submit = htmlspecialchars($_POST['submit']);
			if(isset($submit))
			{
				if(!empty($fnavn) && !empty($enavn) && !empty($tlf) && !empty($email))
				{
					$insert = mysql_query("INSERT INTO adresser ( fnavn,enavn,tlf,email ) VALUES ( '$fnavn','$enavn','$tlf','$email' )");
					if($insert)
					{
						header("Location: ?msg=leggtil");
					}
					else
					{
						header("Location: ?msg=leggtilfeil");
					}
				}
			}
		}
		elseif($p == "fjern")
		{
			$delete = mysql_query("DELETE FROM adresser WHERE id='$id'");
			if($delete)
			{
				header("Location: ?msg=fjern");
			}
			else
			{
				header("Location: ?msg=fjernfeil");
			}
		}
		elseif($p == "endre")
		{
			$select3 = mysql_query("SELECT * FROM adresser WHERE id='$id' ");
			$select4 = mysql_fetch_array($select3);
			$fnavn = $select4['fnavn'];
			$enavn = $select4['enavn'];
			$tlf = $select4['tlf'];
			$email = $select4['email'];
			?>
			<form action="?p=endre&id=<?php echo $id; ?>" method="post">
			<pre>
			Fornavn:	<input name="fnavn" type="text" value="<?php echo $fnavn;?>"><br>
			Etternavn:	<input name="enavn" type="text" value="<?php echo $enavn;?>"><br>
			Tlf:		<input name="tlf" type="text" value="<?php echo $tlf;?>"><br>
			Email: 		<input name="email" type="text" value="<?php echo $email;?>"><br>
			<input name="submit" type="submit" value="Endre">
			</pre>
			</form>
			<?php
			$fnavn = htmlspecialchars($_POST['fnavn']);
			$enavn = htmlspecialchars($_POST['enavn']);
			$tlf = htmlspecialchars($_POST['tlf']);
			$email = htmlspecialchars($_POST['email']);
			$submit = htmlspecialchars($_POST['submit']);
			if(isset($submit))
			{
				if(!empty($fnavn) && !empty($enavn) && !empty($tlf) && !empty($email))
				{
					$update = mysql_query("UPDATE adresser SET fnavn='$fnavn', enavn='$enavn', tlf='$tlf', email='$email' WHERE id='$id' ");
					if($update)
					{
						header("Location: ?msg=endre");
					}
					else
					{
						header("Location: ?msg=endrefeil");
					}
				}
			}
		}
		?>
	</body>
</html>
