Show sourcecode
The following files exists in this folder. Click to view.
public_html/exercises/databas/ovn_db1/
createtable.php
deletepost.php
index.html
insertpost.php
selectposts.php
showtable.php
updatepost.php
insertpost.php
69 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
<!-- insertpost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Ny Kompis</title>
</head>
<body>
<form action="index.html" method="get"><button type="submit">Tillbaka till meny</button></form>
<?php
$message = null;
if (isset($_POST['firstname']) && isset($_POST['lastname']) &&
!empty($_POST['firstname']) && !empty($_POST['lastname'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$mobilenumber = isset($_POST['mobilenumber']) ? $_POST['mobilenumber'] : 0;
$email = $_POST['email'];
include ('../dbconnection.php');
if (!$dbconn) {
die("Connection failed: Can't connect to database.");
}
try {
# prepare
$sql = "INSERT INTO kompisar (firstname, lastname, mobilenumber, email)
VALUES (?, ?, ?, ?)";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($firstname, $lastname, $mobilenumber, $email);
# 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 . "<br>" . $e->getMessage();
}
$dbconn = null;
} else {
$message .= "<br />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="mobilenumber" size=20 maxlength=20></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" size=20 maxlength=20></td></tr>
<tr>
<td>* = obligatoriskt</td>
<td><button type="submit">Lägg till</button></td></tr>
</table>
</form>
</body>
</html>