Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/databaser/databaser1/
createtable.php
databaser1.php
selectposts.php
selectposts.php
290 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Select</title>
<style>
.list {
color: black;
border: 3px solid black;
}
.list td {
padding: 5px;
}
</style>
</head>
<body>
<?php
include('../../../dbconnection.php'); ?>
<br>
<h1>Lägg till kompis</h1>
<?php
$message = null;
if (
isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['mobil']) &&
!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['mobil'])
) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$mobil = isset($_POST['mobil']) ? $_POST['mobil'] : "";
$epost = isset($_POST['epost']) ? $_POST['epost'] : "";
try {
# prepare
$sql = "INSERT INTO Kompisar (firstname, lastname, mobil, epost)
VALUES (?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($firstname, $lastname, $mobil, $epost);
# execute width array-parameter
$stmt->execute($data);
echo "New record created successfully";
$lastId = $dbconn->lastInsertId();
echo "id på sista posten: $lastId";
} catch (PDOException $e) {
echo $sql . $e->getMessage();
}
} else {
$message .= "Du måste fylla i förnamn och efternamn!<br /><br />";
}
echo $message;
?>
<form method="post" action="">
<table>
<tr>
<td>Förnamn*:</td>
<td><input type="text" name="firstname" size=40 maxlength=100>
</td>
</tr>
<tr>
<td>Efternamn*:</td>
<td><input type="text" name="lastname" size=40 maxlength=100></td>
</tr>
<tr>
<td>Mobilnummer:</td>
<td><input type="text" name="mobil" size=20 maxlength=20></td>
</tr>
<tr>
<td>Epost:</td>
<td><input type="text" name="epost" size=30 maxlength=30></td>
</tr>
<tr>
<td>* = obligatoriskt</td>
<td><input type="submit" value="Lägg till"></td>
</tr>
</table>
</form>
</body>
<br>
<br>
<h1>Updatera</h1>
<?php
$message = null;
$getid = null;
$getfirstname = null;
$getlastname = null;
$getmobil = null;
$getepost = null;
if (
isset($_POST['firstname']) && isset($_POST['lastname']) &&
isset($_POST['id']) && !empty($_POST['firstname']) &&
!empty($_POST['lastname']) && !empty($_POST['id'])
) {
$id = $_POST['id'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$mobil = isset($_POST['mobil']) ? $_POST['mobil'] : 0;
$epost = isset($_POST['epost']) ? $_POST['epost'] : 0;
try {
# prepare
$sql = "UPDATE Kompisar SET firstname=?, lastname=?, mobil=?, epost=?
WHERE id=?";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($firstname, $lastname, $mobil, $epost, $id);
# execute width array-parameter
$stmt->execute($data);
$message .= "Record updated successfully.<br />";
// clear form from info
$_GET['id'] = null;
} catch (PDOException $e) {
$message .= $sql . "<br>" . $e->getMessage();
}
} else {
$message .= "Först väljer du en post.
Sen måste du fylla i minst förnamn och efternamn!<br /><br />";
}
if (isset($_GET['id']) && !empty($_GET['id'])) {
$id = $_GET['id'];
try {
# prepare
$sql = "SELECT * FROM Kompisar WHERE id=?";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($id);
# execute width array-parameter
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_ASSOC);
$getid = htmlentities($res['id']);
$getfirstname = htmlentities($res['firstname']);
$getlastname = htmlentities($res['lastname']);
$getmobil = htmlentities($res['mobil']);
$getepost = htmlentities($res['epost']);
$message .= "<br />Record was selected successfully.<br />";
} catch (PDOException $e) {
$message .= $sql . "<br>" . $e->getMessage();
}
} else {
$message .= "<br />Välj en ny post att uppdatera.<br /><br />";
}
echo $message;
?>
<form method="post" action="">
<table>
<tr>
<td>Förnamn*:</td>
<td>
<input type="text" name="firstname" size="40" maxlength="40"
value="<?= $getfirstname; ?>">
</td>
</tr>
<tr>
<td>Efternamn*:</td>
<td>
<input type="text" name="lastname" size="40" maxlength="40"
value="<?= $getlastname; ?>">
</td>
</tr>
<tr>
<td>Mobilnummer:</td>
<td>
<input type="text" name="mobil" size="30" maxlength="30"
value="<?= $getmobil; ?>">
</td>
</tr>
<tr>
<td>Epost:</td>
<td>
<input type="text" name="epost" size="30" maxlength="30"
value="<?= $getepost; ?>">
</td>
</tr>
<tr>
<td>* = obligatoriskt</td>
<td>
<button type="submit">Uppdatera</button>
<input type="hidden" name="id" value="<?= $getid; ?>">
</td>
</tr>
</table>
</form>
<?php
// Ouput table with all posts
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM kompisar";
$stmt = $dbconn->prepare($sql);
// fetch width column names, create a table
$data = array();
$stmt->execute($data);
$output = "<table class='list'>";
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$idx = htmlentities($res['id']);
$first = htmlentities($res['firstname']);
$last = htmlentities($res['lastname']);
$mobil = htmlentities($res['mobil']);
$epost = htmlentities($res['epost']);
$output .= "<tr>" .
"<td><a href='?id=$idx'>$idx</a></td>" .
"<td>$first</td>" .
"<td>$last</td>" .
"<td>$mobil</td>" .
"<td>$epost</td>" .
"</tr>";
}
$output .= "</table>";
echo "$output";
?>
<br>
<br>
<h1>Kompisar!</h1>
<?php
$message = null;
$id = null;
if (isset($_POST['id']) && !empty($_POST['id'])) {
$id = $_POST['id'];
try {
# prepare
$sql = "DELETE FROM kompisar WHERE id=?";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($id);
# execute width array-parameter
$stmt->execute($data);
$message .= "Record deleted successfully.<br />";
} catch (PDOException $e) {
$message .= $sql . "<br>" . $e->getMessage();
}
} else {
$message .= "<br />";
}
echo $message;
// Ouput table with all posts
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM kompisar";
$stmt = $dbconn->prepare($sql);
// fetch width column names, create a table
$data = array();
$stmt->execute($data);
$output = "<table class='list'>";
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$idx = htmlentities($res['id']);
$first = htmlentities($res['firstname']);
$last = htmlentities($res['lastname']);
$mobil = htmlentities($res['mobil']);
$epost = htmlentities($res['epost']);
$output .= "<tr>" .
"<td>$idx</td>" .
"<td>$first</td>" .
"<td>$mobil</td>" .
"<td>$epost</td>" .
"<td><form method='post' action=''>" .
"<input type='hidden' name='id' value='$idx'>" .
"<button type='submit'>Ta bort</button></form></td>" .
"</tr>";
}
$output .= "</table>";
echo "$output";
$dbconn = null;
?>
</html>