2016-08-04 153 views
0

我有這insert.php文件插入數據到我的數據庫,但每次我提交數據$result總是返回false。我不知道問題出在哪裏,它總是迴應"something error"

##Insert.php 

include_once("connection.php"); 

if (
    isset($_POST['txtCarModel']) && isset($_POST['txtCarType']) && 
    isset($_POST['txtCapacity']) && isset($_POST['image']) && 
    isset($_POST['txtFuelType']) && isset($_POST['txtPlateNumber']) && 
    isset($_POST['txtOwner']) 
) { 
    $now = DateTime::createFromFormat('U.u', microtime(true)); 
    $id = $now->format('YmdHis'); 

    $upload_folder = "upload"; 
    $path = "ftp://$upload_folder/$id.jpeg"; 
    $image = $_POST['image']; 
    $fullpath = "carkila.esy.es/$upload_folder/$id.jpeg"; 

    $Car_Model = $_POST['txtCarModel']; 
    $Car_Type = $_POST['txtCarType']; 
    $Capacity = $_POST['txtCapacity']; 
    $Fuel_Type = $_POST['txtFuelType']; 
    $PlateNumber = $_POST['txtPlateNumber']; 
    $Image = $_POST['image']; 
    $Owner = $_POST['txtOwner']; 

    $query = "INSERT INTO tbl_cars (Car_Model, owner, Car_Type,Capacity,fuelType,carPlatenuNumber,Image) VALUES ('$Car_Model', '$Owner', '$Car_Type',$Capacity,'$fuelType','$carPlatenuNumber','$Image')"; 

    $result = mysqli_query($conn, $query); 

    if ($result > 0) { 
     if (isset($_POST['mobile']) && $_POST['mobile'] == "android") { 
      echo "success"; 
      exit; 
    } 
    echo "Insert Successfully"; 
} else { 
    if (isset($_POST['mobile']) && $_POST['mobile'] == "android") { 
     echo "failed"; 
     exit; 
    } 
    echo "Something Error"; 
} 
+0

採取的mysqli的預處理語句的優勢,用'mysqli_error'檢查的問題。 – aynber

+0

'mysqli_error()'說什麼?當出現錯誤時結果爲'0',所以你需要找出發生了什麼錯誤。 – VoteyDisciple

+6

請注意,'mysqli_query'通過INSERT查詢返回true或false,而不是數字。 –

回答

-1

使用mysqli_affected_rows()

mysqli_query($conn, $query); 
$result = mysqli_affected_rows ($conn); 

它將返回受前一操作的行數。

mysqli_affected_rows

+0

由於發生錯誤而無法解決問題。 – Styphon

1
$query = "INSERT INTO tbl_cars (Car_Model, owner, Car_Type,Capacity,fuelType,carPlatenuNumber,Image) VALUES ('$Car_Model', '$Owner', '$Car_Type',$Capacity,'$fuelType','$carPlatenuNumber','$Image')"; 

$ carPlatenuNumber應該是$ plateNumber