PHPで簡易TODOリスト(MySQL編)

Webで使えるTodoリストが欲しかったのですが、どれも機能が充実しすぎて、
私には荷が重い。簡単にやること一覧の管理だけできれば良かったので、作ってみました。


ソースを公開しますので、ご自由に。
著作権は放棄します。

create.sql

create table TODO_LIST(
ID int,
MSG varchar(100),
LIMIT_DATE timestamp
)

connection.php

<?php

//DB接続
function getConnection(){
	//接続
	if (!($con = mysql_connect("server_name", "user_id", "password"))) {
		die(mysql_error());
	}
	//DB選択
	if (!(mysql_select_db("db_name"))) {
		die(mysql_error());
	}
	return $con;
}

function executeQuery($sql){
	if (!($rs = mysql_query($sql))) {
		die(mysql_error());
	}
	return $rs;
}

//DB切断
function closeConnection($con){
	mysql_close($con);
}

?>

edit.php

<?php
require_once 'connection.php';
	
	//接続
	$con = getConnection();
	
	$sql = "";
	switch ($_POST["mode"]){
		case "ins":
			$sql = getInsertQuery($_POST["msg"]);
			break;
		case "del";
			$sql = getDeleteQuery($_POST["id"]);
			break;
	}
	executeQuery($sql);
	
	//切断
	closeConnection($con);
	
	header("Location: index.php");
	
function getInsertQuery($msg){
	$max_id = "";
	$rs = executeQuery("SELECT MAX(ID) + 1 AS MAX_ID FROM TODO_LIST");
	while ($item = mysql_fetch_array($rs)) {
		$max_id = $item["MAX_ID"];
	}
	if ($max_id == "") {
		$max_id = "1";
	}
	
	$sql = "INSERT INTO TODO_LIST(ID,MSG) VALUES (" . $max_id . ",'" . $msg . "')";
	return $sql;
}

function getDeleteQuery($id){
	$sql = "DELETE FROM TODO_LIST WHERE ID = " . $id;
	return $sql;
}
?>

index.php

<?php
require_once 'connection.php';
?>
<html>
<head>
<title>TODO LIST</title>

</head>
<body>
<hr>
<?php
	$con = getConnection();

	$sql = "SELECT * FROM TODO_LIST ORDER BY ID";
	$rs = executeQuery($sql);
	
	echo "<table border='0'>
			<tr><td>内容</td><td></td></tr>";
	while ($item = mysql_fetch_array($rs)) {
		echo "<form action='edit.php' method='POST'>

				<tr>
				<td>" .  $item["MSG"] . "</td>
				<td><input type='submit' value='削除'>
				<input type='hidden' value='del' name='mode'>
				<input type='hidden' value='" . $item["ID"] . "' name='id'>

				</td>
				</tr>
			</form>";
	}
	print "</table>";
	closeConnection($con);
?>
<form action='edit.php' method='POST'>
<input type='text' name='msg' size='50'>
<input type='submit' value='登録'>

<input type='hidden' value='ins' name='mode'>
</form>
</body>
</html>

もどる