2013-08-02 66 views
0

我正在嘗試管理面板的工作。我下載了這個例子管理面板設計:http://medialoot.com/item/html5-admin-template/PHP MySQL插入到GET方法

HTML結構:http://pastebin.ubuntu.com/5940949/

我試過PHP代碼,

<?php 
if(isset($_GET['submit'])){ 
    if($_FILES["carImage"]["error"] > 0){ 
    echo '<script type="text/javascript">alert('."File not upload" . $_FILES["carImage"]["error"].');</script>'; 
    }else{ 
     if(!is_dir("../inc/imj/cars")){ 
      mkdir("../inc/imj/cars"); 
     } 
     $carImage_ = "../inc/imj/cars".$_FILES["carImage"]["name"]; 
     move_uploaded_file($_FILES["carImage"]["tmp_name"],$carImage_); 

     mysql_query(" 

     INSERT INTO contents ('indexId','catId','carModelName','carCash','carImage','carAlt','carTitle','carKMH','carWeight','carWheelInch','carColur','info') 
     VALUES (NULL,'".$_GET['catId']."','".$_GET['carModelName']."','".$_GET['carCash']."','".$carImage_."','".$_GET['carAlt']."','".$_GET['carTitle']."','".$_GET['carKMH']."','".$_GET['carWeight']."','".$_GET['carWheelInch']."','".$_GET['carColur']."','".$_GET['info']."') 

     "); 

    } 
}  
?> 

我試圖替代查詢代碼,

mysql_query(" 

INSERT INTO contents ('indexId','catId','carModelName','carCash','carImage','carAlt','carTitle','carKMH','carWeight','carWheelInch','carColur','info') 
VALUES (NULL,".$_GET['catId'].",".$_GET['carModelName'].",".$_GET['carCash'].",".$carImage_.",".$_GET['carAlt'].",".$_GET['carTitle'].",".$_GET['carKMH'].",".$_GET['carWeight'].",".$_GET['carWheelInch'].",".$_GET['carColur'].",".$_GET['info'].") 

"); 

我猜的文件不上傳。所以不行。

因爲錯誤;

注意:未定義指數:carImage在..

注意:未定義指數:更多資料在..

注:表contents.indexId是主鍵和自動遞增。

如何解決纔好呢?

感謝您的關注。

UPDATE

圖片上傳的問題是;

<?php 
$catId = isset($_GET['catId']) ? $_GET['catId'] : ""; 
$carModelName = isset($_GET['carModelName']) ? $_GET['carModelName'] : ""; 
$carCash = isset($_GET['carCash']) ? $_GET['carCash'] : ""; 
$carAlt = isset($_GET['carAlt']) ? $_GET['carAlt'] : ""; 
$carTitle = isset($_GET['carTitle']) ? $_GET['carTitle'] : ""; 
$carKMH = isset($_GET['carKMH']) ? $_GET['carKMH'] : ""; 
$carWeight = isset($_GET['carWeight']) ? $_GET['carWeight'] : ""; 
$carWheelInch = isset($_GET['carWheelInch']) ? $_GET['carWheelInch'] : ""; 
$carColur = isset($_GET['carColur']) ? $_GET['carColur'] : ""; 
$info = isset($_GET['info']) ? $_GET['info'] : ""; 
if(isset($_GET['submit'])){ 
    if($_FILES["carImage"]["error"] > 0){ 
    echo '<script type="text/javascript">alert('."File not upload" . $_FILES["carImage"]["error"].');</script>'; 
    }else{ 

     $carUrl_ = "../inc/imj/urun"; 
     if(!is_dir($carUrl_)){mkdir($carUrl_);} 

     $carImage_ = $carUrl_.$_FILES["carImage"]["name"]; 
     $carUrlName = $_FILES["carImage"]["name"]; 
     move_uploaded_file($_FILES["carImage"]["tmp_name"],$carImage_); 

     mysql_query(" 

     INSERT INTO contents ('indexId','catId','carModelName','carCash','carImage','carAlt','carTitle','carKMH','carWeight','carWheelInch','carColur','info') 
     VALUES (".$catId.",".$_GET['carModelName'].",".$_GET['carCash'].",".$carUrlName.",".$carAlt.",".$carTitle.",".$carKMH.",".$carWeight.",".$carWheelInch.",".$carColur.",".$info.") 

     "); 

    } 
}  
?> 

更新2

<?php 
$catId = isset($_GET['catId']) ? $_GET['catId'] : ""; 
$carModelName = isset($_GET['carModelName']) ? $_GET['carModelName'] : ""; 
$carCash = isset($_GET['carCash']) ? $_GET['carCash'] : ""; 
$carAlt = isset($_GET['carAlt']) ? $_GET['carAlt'] : ""; 
$carTitle = isset($_GET['carTitle']) ? $_GET['carTitle'] : ""; 
$carKMH = isset($_GET['carKMH']) ? $_GET['carKMH'] : ""; 
$carWeight = isset($_GET['carWeight']) ? $_GET['carWeight'] : ""; 
$carWheelInch = isset($_GET['carWheelInch']) ? $_GET['carWheelInch'] : ""; 
$carColur = isset($_GET['carColur']) ? $_GET['carColur'] : ""; 
$info = isset($_GET['info']) ? $_GET['info'] : ""; 
if(isset($_GET['submit'])){ 

     mysql_query(" 

     INSERT INTO contents ('indexId','catId','carModelName','carCash','carImage','carAlt','carTitle','carKMH','carWeight','carWheelInch','carColur','info') 
     VALUES (".$catId.",".$_GET['carModelName'].",".$_GET['carCash'].",tryImg.png,".$carAlt.",".$carTitle.",".$carKMH.",".$carWeight.",".$carWheelInch.",".$carColur.",".$info.") 

     "); 

}  
?> 
+0

我認爲你最好在這裏使用POST代替GET。你嘗試過嗎? – Maximus2012

+0

另外,你確定錯誤是在MySQL查詢?錯誤可能在代碼的其他部分嗎? – Maximus2012

+0

我不能使用INSERT INTO方法將行添加到表中。 – fthmad9

回答

1

對於獲取每個變量,檢查變量在MySQL INSERT語句中使用它之前設置。

$variable = (isset($_GET['variable'])) ? $_GET['variable'] : ""; 

而且不要插入indexId,它會自動增加,你可以從查詢語句中刪除它。

mysql_query(" 

INSERT INTO contents ('catId','carModelName','carCash','carImage','carAlt','carTitle','carKMH','carWeight','carWheelInch','carColur','info') 
VALUES (".$_GET['catId'].",".$_GET['carModelName'].",".$_GET['carCash'].",".$carImage_.",".$_GET['carAlt'].",".$_GET['carTitle'].",".$_GET['carKMH'].",".$_GET['carWeight'].",".$_GET['carWheelInch'].",".$_GET['carColur'].",".$_GET['info'].") 

"); 

更新

當用戶離開一個字段爲空,

isset($_GET['that-field']) 

返回false,即值未設置。

else isset返回true,即用戶設置該變量的值。

所以你不能使用$ _GET ['var']而不知道var是否被賦予了一些值。 如果用戶沒有給出任何值,那麼在將其輸入到數據庫之前,您必須自己設置它。

因此,使用,

if(isset($_GET['variable'])){ 
    $variable = $_GET['variable']; 
} else{ 
    $variable = ""; 
} 

你將不得不這樣做對所有的變量,你運行MySQL查詢之前,用'$variable'那裏,而不是$_GET['variable']

+0

我的確如此;如果(isset($ _ GET ['catId'])|| isset($ _ GET ['carModelName'])|| isset($ _ GET ['carCash'])|| isset($ _ GET ['carAlt']) || isset($ _ GET ['carTitle'])|| isset($ _ GET ['carKMH'])|| isset($ _ GET ['carWeight'])|| isset($ _ GET ['carWheelInch'])|| || isset($ _ GET ['carColour'])|| isset($ _ GET ['info'])){//查詢和文件上傳設置..} Bu我沒有工作..:/ – fthmad9

+0

您應該使用'&&'而不是'||'。對?如果它們中的任何一個沒有設置,你想丟棄一切? –

+0

好的。這次沒有錯誤。但不添加。我只是想將信息添加到表格中。 – fthmad9