Created
September 15, 2012 15:20
-
-
Save blha303/3728434 to your computer and use it in GitHub Desktop.
PHP/MySQL todo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* PHP/MySQL todo script | |
* by blha303 ([email protected]) | |
* Change $host, $user and $password to the mysql login info you want to use. | |
* Released under the WTF license. | |
*/ | |
echo "<html> | |
<head><title>Things to do</title></head> | |
<body> | |
<h1>Things to do</h1> | |
<form name='things' method=POST action='todo.php'> | |
"; | |
$host="localhost"; | |
$user="things"; | |
$password="PASSWORD"; | |
$cxn = mysqli_connect($host,$user,$password); | |
if (!empty($_POST['checkbox'])) { | |
$sql1 = "UPDATE things.items set isVisible=0 where iditems=".$_POST['checkbox']; | |
$result = mysqli_query($cxn,$sql1); | |
if($result == false) { echo "error updating table"; } | |
} | |
if (!empty($_POST['retr'])) { | |
$sql2 = 'select iditems,content,isVisible from things.items where iditems='.$_POST['retr']; | |
$result = mysqli_query($cxn,$sql2); | |
if($result == false) { echo "error retrieving data. <a href='todo.php'>Refresh</a>"; } | |
else { | |
echo "<table border='1'>"; | |
echo "<tr><th>ID</th><th>Content</th><th>isVisible</th></tr>"; | |
for($i = 0; $i < mysqli_num_rows($result); $i++) | |
{ | |
$row_array = mysqli_fetch_row($result); | |
echo "<tr>"; | |
for($j = 0;$j < mysqli_num_fields($result);$j++) | |
{ | |
echo " <td>".$row_array[$j]."</td>\n"; | |
} | |
echo "</tr>"; | |
} | |
echo "</table><a href='todo.php'>Back</a>"; | |
} | |
} | |
if (!empty($_POST['newcontent'])) { | |
$sql3 = "INSERT INTO things.items (content, isVisible) VALUES ('".$_POST['newcontent']."', 1)"; | |
$result = mysqli_query($cxn,$sql3); | |
if($result == false) { echo "Could not add new todo"; } | |
} | |
if (!empty($_POST['list'])) { | |
$sql4 = "SELECT * from things.items"; | |
$result = mysqli_query($cxn,$sql4); | |
if($result == false) { echo "Could not list all items. <a href='todo.php'>Back</a>"; } | |
else { | |
echo "<table border='1'>"; | |
echo "<tr><th>ID</th><th>Content</th><th>isVisible</th></tr>"; | |
for($i = 0; $i < mysqli_num_rows($result); $i++) | |
{ | |
$row_array = mysqli_fetch_row($result); | |
echo "<tr>"; | |
for($j = 0;$j < mysqli_num_fields($result);$j++) | |
{ | |
echo " <td>".$row_array[$j]."</td>\n"; | |
} | |
echo "</tr>"; | |
} | |
echo "</table><a href='todo.php'>Back</a>"; | |
} | |
} | |
if (empty($_POST['retr'])) { | |
if (empty($_POST['list'])) { | |
$sql="select iditems,content from things.items where isVisible = 1;"; | |
$result = mysqli_query($cxn,$sql); | |
if($result == false) | |
{ | |
echo "<h4>Error: ".mysqli_error($cxn)."</h4>"; | |
} | |
else | |
{ | |
/* Table that displays the results */ | |
echo "<table> | |
"; | |
for($i = 0; $i < mysqli_num_rows($result); $i++) | |
{ | |
echo " <tr> | |
"; | |
$row_array = mysqli_fetch_row($result); | |
for($j = 1;$j < mysqli_num_fields($result);$j++) | |
{ | |
echo " <td><input type='checkbox' name='checkbox' value='".$row_array[$j-1]."' onClick='things.submit()'>".$row_array[$j]."</td>\n"; | |
} | |
} | |
echo " | |
</table> | |
Retrieve: <input type='text' name='retr' size=2><input type='submit' value=''><br> | |
New: <input type='text' name='newcontent'><input type='submit'><br> | |
<input type='submit' name='list' value='List'> | |
</body> | |
</html>"; | |
} } } | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment