Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/exercises/mysqlintro/kompisar/
createtable.php
dbconnection.php
deletepost.php
deletetable.php
index.html
insertdefaultposts.php
insertpost.php
selectposts.php
updatepost.php
insertpost.php
62 lines UTF-8 Windows (CRLF)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
<!-- insertpost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Insert</title>
</head>
<body>
<?php
$message = null;
if (isset($_POST['firstname']) && isset($_POST['lastname']) &&
!empty($_POST['firstname']) && !empty($_POST['lastname'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = isset($_POST['email']) ? $_POST['email'] : "_";
$phone = isset($_POST['phone']) ? $_POST['phone'] : "_";
include ('dbconnection.php');
try {
# prepare
$sql = "INSERT INTO pdodemotable (firstname, lastname, email, phone, reg_date)
VALUES (?, ?, ?, ?, now())";
$stmt = $dbconn->prepare($sql);
# the data we want to insert
$data = array($firstname, $lastname, $email, $phone);
# execute width array-parameter
$stmt->execute($data);
echo "New record created successfully";
}
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>Epost:</td><td><input type="text" name="email" size=20 maxlength=30></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone" size=20 maxlength=10></td></tr>
<tr>
<td>* = obligatoriskt</td>
<td><button type="submit">Lägg till</button></td></tr>
</table>
</form>
<button onclick="window.location.href='index.html'">Meny</button>
</body>
</html>