Created
August 4, 2010 03:35
-
-
Save therabidbanana/507592 to your computer and use it in GitHub Desktop.
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
DROP TABLE IF EXISTS `pages`; | |
CREATE TABLE `pages` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`title` varchar(255) DEFAULT NULL, | |
`content` text, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `titleu` (`title`) | |
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; |
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 | |
if(isset($_SERVER['PATH_INFO'])) $short = route($_SERVER['PATH_INFO']); | |
function db_connect(){ | |
/***** Start configuration ******/ | |
$username = 'root'; | |
$password = ''; | |
$hostname = 'localhost'; | |
$database = 'tenkiwi'; | |
/***** End configuration ******/ | |
if(!($link = mysql_connect($hostname, $username, $password))) | |
return cleanup(); | |
if(!mysql_select_db($database)) | |
return cleanup('', $link); | |
mysql_set_charset('utf8', $link); | |
return $link; | |
} | |
function cleanup($val = '', $link = false, $result = false){ | |
if($result) mysql_free_result($result); | |
if($link) mysql_close($link); | |
return $val; | |
} | |
function route($route){ | |
if($route=='/save'){save();} | |
if($route=='/load'){load();} | |
} | |
function save(){ | |
$link = db_connect(); | |
$page = mysql_real_escape_string($_POST['title']); | |
$content = mysql_real_escape_string($_POST['content']); | |
$q = sprintf("INSERT INTO pages (title, content) VALUES ('%s', '%s') ON DUPLICATE KEY UPDATE content='%s'", $page, $content, $content); | |
$res = mysql_query($q); | |
cleanup('', $link); | |
echo 'saved '.$page. ' with '.$q; | |
} | |
function load(){ | |
$link = db_connect(); | |
$page = mysql_real_escape_string($_REQUEST['title']); | |
$q = sprintf("SELECT * FROM pages WHERE title = '%s'",$page); | |
$res = mysql_query($q); | |
if($r = mysql_fetch_assoc($res)){ | |
$r['error'] = false; | |
$r['b'] = $_REQUEST['b']; | |
echo json_encode($r); | |
} | |
else{ | |
echo json_encode(array('error' => true, 'title' => $_REQUEST['title'], 'b' => $_REQUEST['b'])); | |
} | |
cleanup('', $link); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment