Skip to content

Instantly share code, notes, and snippets.

@JuhaRaty
Created September 13, 2017 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JuhaRaty/8cf243c2ebffc8ca1eb87bc746db6cfb to your computer and use it in GitHub Desktop.
Save JuhaRaty/8cf243c2ebffc8ca1eb87bc746db6cfb to your computer and use it in GitHub Desktop.
<?php
//Asetukset
$user='root';
$password='';
$database='todo';
$host='localhost';
//yhteys
$dsn="mysql:host=$host;charset=UTF8;dbname=$database";
$pdo=new PDO($dsn, $user, $password);
//GET
if(isset($_GET['delete'])){
$pdoStatement=$pdo->prepare('DELETE FROM todo WHERE id='.$_GET['delete']);
$pdoStatement->execute();
}else if(isset($_GET['add'])){
$pdoStatement=$pdo->prepare('INSERT INTO todo(value) VALUES ("'.$_GET['add'].'")');
$pdoStatement->execute();
}else if(isset($_GET['update'])){
$pdoStatement=$pdo->prepare('UPDATE todo SET value="'.$_GET['value'].'" WHERE id="'.$_GET['update'].'";');
$pdoStatement->execute();
}
//Luetaan tietokanta vasta getin jälkeen, jotta muutokset nähdään suoraan
$pdoStatement=$pdo->prepare('SELECT * FROM todo;');
$pdoStatement->execute();
$hits=$pdoStatement->fetchAll();
//Näytetään sivu
echo '<h1>To-Do:</h1>';
foreach($hits as $row) {
echo '<table>';
echo "<tr><td><p>".$row['id']." ".$row['value']."</p></td><td><a href='?delete=".$row['id']."'>Delete</a></td></tr>\n"; //Taulukkoon tulee id, value ja delete-linkki
echo '</table>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment