2014-01-29 58 views
0

我有一個站點的管理端的這個簡短的腳本,將上傳和圖像或上傳鏈接到數據庫。我最近把這個站點從一個hostpapa服務器移到了一個godaddy服務器。這個腳本在hostpapa上效果很好。導入到godaddy服務器後,只能上傳圖片工作。上傳一個鏈接失敗,id返回0.它很奇怪,因爲auto_increment被設置並且上傳圖片證明auto_increment工作得很好。不知道爲什麼id爲鏈接上傳返回0,並且當我嘗試上傳鏈接時沒有任何內容插入到數據庫中。新的數據庫導入,mysql_insert_id()返回0

include '../connect.php'; 

if ($_POST['upload']) 
{ 
//get file attributes. 
$name = $_FILES['myfile']['name']; 
$tmp_name = $_FILES['myfile']['tmp_name']; 
$title = $_POST['title']; 
$url = $_POST['url']; 
$type = $_POST['type']; 


if ($name) 
{ 
$location = "imgs/$name"; 
$dir = "../imgs/$name"; 
move_uploaded_file($tmp_name,$dir); 

$query = mysql_query("INSERT INTO `links` VALUES ('','$title','','$location','$type')"); 
//last id 
$id = mysql_insert_id(); 
    echo $id,$location,$title; 
die ("Image successfully uploaded."); 

} 

//this bit below is what doesn't work as it should. 
$query = mysql_query("INSERT INTO `links` VALUES ('','$title','$url','','$type')"); 
//last id 
$id = mysql_insert_id(); 
    echo $id,$url,$title; 
die ("Link successfully loaded."); 

} 
+2

檢查mysql錯誤。 –

回答

0

感謝@Ed科特雷爾和他的建議使用:

echo mysql_errno().": ".mysql_error(); 

我能看到的錯誤是不使用 - mysql_real_escape_string因爲錯誤涌現了這是一個語法錯誤,因爲標題該鏈接包含一個撇號。

在變量中實現mysql_real_escape_string解決了這個問題。謝謝埃德。

0

你的問題幾乎肯定與magic quotes。 GoDaddy在默認情況下啓用了魔術引號,這很容易破壞您的查詢,特別是因爲您只將數據包裝在'中,並且不會對輸入進行任何轉義。嘗試關閉魔術引號,將其放在腳本的頂部,問題應該消失。

if (get_magic_quotes_gpc()) { 
    function magicQuotes_awStripslashes(&$value, $key) { 
     $value = stripslashes($value); 
    } 
    $gpc = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST); 
    array_walk_recursive($gpc, 'magicQuotes_awStripslashes'); 
} 

此外,你應該知道,mysql_*已經過時和不安全的 - 你應該使用庫MySQLi或PDO來代替。

最後,您可以開放SQL注入。你說這是該網站的管理區域,但你不能假設沒有什麼不好的事情會使它到達這個頁面。對查詢中使用的每個變量使用準備好的語句或至少mysql_real_escape_string()

+0

感謝您的提示。一旦我再次得到這個工作,我會執行。現在關閉魔術引號不起作用。 :(問題依然存在 – Guage

+1

請在調用mysql_insert_id()之前加上'echo mysql_errno()。「:」.mysql_error();'然後讓我們知道您在嘗試保存鏈接時看到的錯誤。 –

+0

另外,請發佈表的「CREATE TABLE」語句;如果沒有它,很難看到發生了什麼。 –