2016-07-23 154 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函数看看图像名称值是否正确。正如你在做的,可能图像名称设置为空。