Webbserverprogrammering 1

Källkod

Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.

webbserverprogrammering/exercises/dbintrotest/

10 filer

create_table.php
database_handler.php
dbconnection.php
dbintrotest.php
delete_row.php
delete_table.php
insert_default_values.php
insert_values.php
print_table.php
update_values.php

update_values.php

147 lines UTF-8 Windows (CRLF)
<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Update values</title>
 <meta charset="utf-8">
</head>
<body>

 <?php

 
include ('dbconnection.php');

 
$message null;
 
$getid null;
 
$getfirstname null;
 
$getlastname null;
 
$getage 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'];
  
$age = isset($_POST['age']) ? $_POST['age'] : 0;
  
  try {    
   
# prepare
   
$sql "UPDATE pdodemotable SET firstname=?, lastname=?, age=? 
     WHERE id=?"
;
   
$stmt $dbconn->prepare($sql);
   
# the data we want to insert
   
$data = array($firstname$lastname$age$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 pdodemotable 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']);
   
$getage htmlentities($res['age']);
   
   
$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>Ålder:</td>
    <td>
     <input type="text" name="age" size="20" maxlength="20" value="<?= $getage?>">
    </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 pdodemotable";
 
$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']);
  
$agex htmlentities($res['age']);
  
  
$output .= "<tr>".
   
"<td><a href='?id=$idx'>$idx</a></td>".
   
"<td>$first</td>".
   
"<td>$last</td>".
   
"<td>$agex</td>".
  
"</tr>";
  
 }
 
$output .= "</table>";
 echo 
"$output";

 
$dbconn null;

 
?>

</body>
</html>