2016-07-23 116 views
0

你好,我嘗試上傳表單信息到數據庫和每一個領域上傳除了圖像文件和圖像移動到路徑,但當我檢查表時圖像列中沒有圖像名稱...我的代碼:上傳圖像到mysql數據庫

$server = "localhost"; 
$username = "root"; 
$password = "root3"; 
$dbname = "sampledb"; 

// creat connection 
$connection = new mysqli($server, $username, $password, $dbname); 

// check the connection 
if ($connection->connect_error) { 
    die("connect to database failed :" . $connection->connect_error); 
} 
// check value from select menu 
if ($_POST['typelist'] == 'item') { 
    // prepare and bind parameters 
    if (isset($_FILES['item_image'])) { 
     $itemquery = $connection->prepare("insert into items(item_image, item_manufacturing_year, item_Length, item_width, item_weight, item_price, item_description, item_model) VALUES (? , ? , ? , ? , ? , ? , ? , ?)"); 
     $itemquery->bind_param("bsssssss", $image, $manufacturing_year, $length, $width, $weight, $price, $description, $model); 

     // set all parameters and execute the query 
     $image = $_FILES['item_image']['name']; 
     $temp = $_FILES['item_image']['tmp_name']; 
     $path = "../images/items/$image"; 
     move_uploaded_file($temp, $path); 
     $manufacturing_year = $_POST['manufacturing_year']; 
     $length  = $_POST['item_length']; 
     $width  = $_POST['item_width']; 
     $weight  = $_POST['item_weight']; 
     $price  = $_POST['item_price']; 
     $description = $_POST['item_description']; 
     $model  = $_POST['item_model']; 
     $itemquery->execute(); 
     $itemquery->close(); 
     $connection->close(); 
    } 
} 
+0

爲什麼使用'b'並在'bind_param'中傳遞字符串? –

+0

嘗試在定義'$ model'之後在行上放置'$ itemquery-> bind_param'。 – castis

+0

@RC你認爲是錯的。 –

回答

0

問題是你是設置圖像的名字後的參數綁定的查詢,試圖以這種方式:

$server = "localhost"; 
$username = "root"; 
$password = "root3"; 
$dbname = "sampledb"; 

// creat connection 
$connection = new mysqli($server, $username, $password, $dbname); 

// check the connection 
if ($connection->connect_error) 
{ 
    die("connect to database failed :" . $connection->connect_error); 
} 

// check value from select menu 
if ($_POST['typelist'] == 'item') 
{ 

    // prepare and bind parameters 
    if (isset($_FILES['item_image'])) 
    { 

    // set all parameters FIRST TO BIND ON THE QUERY 
    $image = $_FILES['item_image']['name']; // Now you have the image name 
    $temp = $_FILES['item_image']['tmp_name']; 
    $path = "../images/items/$image"; 

    move_uploaded_file($temp, $path); 

    $manufacturing_year = $_POST['manufacturing_year']; 
    $length = $_POST['item_length']; 
    $width = $_POST['item_width']; 
    $weight = $_POST['item_weight']; 
    $price = $_POST['item_price']; 
    $description = $_POST['item_description']; 
    $model = $_POST['item_model']; 

    $itemquery = $connection->prepare("insert into items(item_image, item_manufacturing_year, item_Length, item_width, item_weight, item_price, item_description, item_model) VALUES (? , ? , ? , ? , ? , ? , ? , ?)"); 
    $itemquery->bind_param("bsssssss", $image, $manufacturing_year, $length, $width, $weight, $price, $description, $model); 

    $itemquery->execute(); 
    $itemquery->close(); 
    $connection->close(); 
    } 
} 

嘗試使用的var_dump()PHP函數看看圖像名稱值是否正確。正如你在做的,可能圖像名稱設置爲空。