Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserverprogrammering/exercises/mysqlintro/inlogg/

admin.php
dbconnection.php
deletepost.php
hemlig.php
index.php
insertpost.php
lås.php
selectposts.php
updatepost.php

insertpost.php

68 lines UTF-8 Windows (CRLF)
<?php 
session_start
();
?>
<!-- insertpost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Insert</title>
</head>

<body>
<?php
include('lås.php');
$message null;
if (isset(
$_POST['firstname']) && isset($_POST['lastname']) && 
  !empty(
$_POST['firstname']) && !empty($_POST['lastname'])) {
    
$firstname $_POST['firstname'];
    
$lastname $_POST['lastname'];
    
$username $_POST['username'];
    
$password $_POST['password'];
    
$admin $_POST['admin'];
    
    include (
'dbconnection.php');
    try {    
        
# prepare
        
$sql "INSERT INTO användare (firstname, lastname, username, password, admin, reg_date) 
          VALUES (?, ?, ?, ?, ?, now())"
;
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($firstname$lastname$username$password$admin);
        
# 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>Användare*:</td><td><input type="text" name="username" size=20 maxlength=30></td></tr> 
<tr><td>Password*:</td><td><input type="text" name="password" size=20 maxlength=20></td></tr> 
<tr><td>Admin*:</td><td><input type="text" name="admin"></td></tr> 
<tr>
<td>* = obligatoriskt</td>
<td><button type="submit">Lägg till</button></td></tr> 
</table> 
</form>
<button onclick="window.location.href='admin.php'">Meny</button>
</body>
</html>