Webbserverprogrammering 1

Show sourcecode

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

webbserverprogrammering/exercises/mysql-intro_2/

admin.php
createtable.php
dbconnection.php
deletepost.php
deletetable.php
insertdefaultposts.php
insertposts.php
mysql-log-in.php
selectposts.php
updateposts.php
welcome.php

updateposts.php

171 lines UTF-8 Windows (CRLF)
<!-- updatepost.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Update</title>
</head>
<body>
<?php
include ('dbconnection.php');
/**
 * @var PDO $dbconn
 * */
$message null;
$getid null;
$getfirstname null;
$getlastname null;
$getphone_number null;
$gete_mail null;
$getadmin null;

if (isset(
$_POST['firstname']) && isset($_POST['lastname']) && 
  isset(
$_POST['phone_number']) && isset($_POST['e_mail']) &&
  isset(
$_POST['id']) && isset($_POST['admin']) && !empty($_POST['firstname']) && 
  !empty(
$_POST['lastname']) && !empty($_POST['phone_number']) && 
  !empty(
$_POST['e_mail']) && !empty($_POST['id']) && !empty($_POST['admin'])) {
      
    
$id $_POST['id'];
    
$firstname $_POST['firstname'];
    
$lastname $_POST['lastname'];
    
$phone_number $_POST['phone_number'];
    
$e_mail $_POST['e_mail'];
    
$admin $_POST['admin'];
    
    try {    
        
# prepare
        
$sql "UPDATE friend SET firstname=?, lastname=?, phone_number=?, e_mail=?, admin=? WHERE id=?";
        
$stmt $dbconn->prepare($sql);
        
# the data we want to insert
        
$data = array($firstname$lastname$phone_number$e_mail$admin$id);
        
# execute width array-parameter
        
$stmt->execute($data);
            
        
$message .= "<br />Record updated successfully.<br />";
        
// clear form from info
        
$_GET['id'] = null;
    }
    catch(
PDOException $e)
        {
        
$message .= $sql "<br>" $e->getMessage();
    }
} else {
    
$message .= "<br />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 friend 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'] ?? '');
        
$getphone_number htmlentities($res['phone_number'] ?? '');
        
$gete_mail htmlentities($res['e_mail'] ?? '');
        
$getadmin htmlentities($res['admin'] ?? '');
        
        
$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>Mobil nummer*:</td>
            <td>
                <input type="text" name="phone_number" size="40" maxlength="10" 
                  value="<?= $getphone_number?>">
            </td>
            </tr> 
            <tr>
            <td>E-post*:</td>
            <td>
                <input type="text" name="e_mail" size="40" maxlength="100" 
                  value="<?= $gete_mail?>">
            </td>
            </tr>
            <td>Admin* (0/1):</td>
            <td>
                <input type="text" name="admin" size="40" maxlength="1" 
                  value="<?= $getadmin?>">
            </td>
            </tr> 
            <tr>
            <td>* = obligatoriskt</td>
            <td>
                <button type="submit">Lägg till</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 friend";
    
$stmt $dbconn->prepare($sql);
    
// fetch width column names, create a table
    
$data = array();  
    
$stmt->execute($data);
    
$output "<table><caption>En ostylad tabell!</caption>";
    while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
        
$idx htmlentities($res['id'] ?? '');
        
$first htmlentities($res['firstname'] ?? '');
        
$last htmlentities($res['lastname'] ?? '');
        
$phone htmlentities($res['phone_number'] ?? '');
        
$e_m htmlentities($res['e_mail'] ?? '');
        
$admin htmlentities($res['admin'] ?? '');
        
        
$output .= "<tr>".
            
"<td><a href='?id=$idx'>$idx</a></td>".
            
"<td>$first</td>".
            
"<td>$last</td>".
            
"<td>$phone</td>".
            
"<td>$e_m</td>".
            
"<td>$admin</td>".
        
"</tr>";
        
    }
    
$output .= "</table>";
    echo 
"$output";

    
$dbconn null;
?>

</body>
</html>