Last active 1444463281

PHP/MySQL todo

todo.php Raw
1<?php
2/*
3 * PHP/MySQL todo script
4 * by blha303 (b3@blha303.tk)
5 * Change $host, $user and $password to the mysql login info you want to use.
6 * Released under the WTF license.
7 */
8echo "<html>
9 <head><title>Things to do</title></head>
10 <body>
11 <h1>Things to do</h1>
12 <form name='things' method=POST action='todo.php'>
13 ";
14$host="localhost";
15$user="things";
16$password="PASSWORD";
17$cxn = mysqli_connect($host,$user,$password);
18if (!empty($_POST['checkbox'])) {
19 $sql1 = "UPDATE things.items set isVisible=0 where iditems=".$_POST['checkbox'];
20 $result = mysqli_query($cxn,$sql1);
21 if($result == false) { echo "error updating table"; }
22}
23if (!empty($_POST['retr'])) {
24 $sql2 = 'select iditems,content,isVisible from things.items where iditems='.$_POST['retr'];
25 $result = mysqli_query($cxn,$sql2);
26 if($result == false) { echo "error retrieving data. <a href='todo.php'>Refresh</a>"; }
27 else {
28 echo "<table border='1'>";
29 echo "<tr><th>ID</th><th>Content</th><th>isVisible</th></tr>";
30 for($i = 0; $i < mysqli_num_rows($result); $i++)
31 {
32 $row_array = mysqli_fetch_row($result);
33 echo "<tr>";
34 for($j = 0;$j < mysqli_num_fields($result);$j++)
35 {
36 echo " <td>".$row_array[$j]."</td>\n";
37 }
38 echo "</tr>";
39 }
40 echo "</table><a href='todo.php'>Back</a>";
41 }
42}
43if (!empty($_POST['newcontent'])) {
44 $sql3 = "INSERT INTO things.items (content, isVisible) VALUES ('".$_POST['newcontent']."', 1)";
45 $result = mysqli_query($cxn,$sql3);
46 if($result == false) { echo "Could not add new todo"; }
47}
48if (!empty($_POST['list'])) {
49 $sql4 = "SELECT * from things.items";
50 $result = mysqli_query($cxn,$sql4);
51 if($result == false) { echo "Could not list all items. <a href='todo.php'>Back</a>"; }
52 else {
53 echo "<table border='1'>";
54 echo "<tr><th>ID</th><th>Content</th><th>isVisible</th></tr>";
55 for($i = 0; $i < mysqli_num_rows($result); $i++)
56 {
57 $row_array = mysqli_fetch_row($result);
58 echo "<tr>";
59 for($j = 0;$j < mysqli_num_fields($result);$j++)
60 {
61 echo " <td>".$row_array[$j]."</td>\n";
62 }
63 echo "</tr>";
64 }
65 echo "</table><a href='todo.php'>Back</a>";
66 }
67}
68if (empty($_POST['retr'])) {
69if (empty($_POST['list'])) {
70$sql="select iditems,content from things.items where isVisible = 1;";
71$result = mysqli_query($cxn,$sql);
72if($result == false)
73{
74echo "<h4>Error: ".mysqli_error($cxn)."</h4>";
75}
76else
77{
78/* Table that displays the results */
79echo "<table>
80";
81for($i = 0; $i < mysqli_num_rows($result); $i++)
82{
83echo " <tr>
84";
85$row_array = mysqli_fetch_row($result);
86for($j = 1;$j < mysqli_num_fields($result);$j++)
87{
88echo " <td><input type='checkbox' name='checkbox' value='".$row_array[$j-1]."' onClick='things.submit()'>".$row_array[$j]."</td>\n";
89}
90}
91echo "
92 </table>
93 Retrieve: <input type='text' name='retr' size=2><input type='submit' value=''><br>
94 New: <input type='text' name='newcontent'><input type='submit'><br>
95 <input type='submit' name='list' value='List'>
96 </body>
97</html>";
98} } }
99?>