2012-08-30 29 views
1
<?php 

include ("apps/controller/database.php"); include ("header.php"); 
//Connect to the DB $database->connect(); //Check the url has been set 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 


$url = isset($_POST['url']) ? $database->escape($_POST['url']) : false; 
//$url = isset($_POST['url']) ? $database->escape($_POST['url']) : ''; 
//This is optional ;) 
$password = isset($_POST['password']) ? $database->escape($_POST['password']) : ''; 
//Okay, now we've figured out if they've set the url, let's add it to the DB 
if (isset($url)) { 
    $id = md5(uniqid()); 
    $res = $database->query("INSERT INTO urls (url,pass,key) VALUES('".$url."','".$password."','".$id."')"); 
    echo '<br /><br /><div id="both" class="types both"><a href="http://localhost/view.php?id=' . 
     $id . '">http://localhost/view.php?id=' . $id . '</a></div>'; 
} 
//Close the DB 
$database->close(); 
}else 
{ 
    echo '<br /><br /><div id="err" class="notification error"><a class="close" href="#" alt="close" title="Close this notification"> </a> You cannot view this URL directly. Please create a url.</div>'; 
} 


$database->close(); 

我的MYSQL結構是手動:ID,網址,觀點,鍵您的SQL語法有錯誤;檢查對應於你的MySQL服務器版本正確的語法

+0

我強烈建議分開演示文稿和數據庫連接之間的功能。 – taz

+0

迴應您的查詢 –

+0

您最好使用自動增量主鍵。它避免了每次插入行時必須創建ID的開銷。 –

回答

4

keyMySQL reserved keyword,你需要的,如果你想與反引號引用它將其用作字段名稱。

INSERT INTO urls (url,pass,`key`)... 
+0

+1,並給出了代碼/語法,如果有人不知道什麼反撥:) – Fluffeh

+0

謝謝。我會投票這個最好的答案,快速和可以理解:) –

+0

必須等5分鐘纔可以:( –

相關問題