将图像上传到mysql数据库

时间:2016-07-23 17:49:52

标签: php mysqli

你好我尝试将表单信息上传到数据库并上传每个字段除了图像文件和图像移动到路径但当我检查表时图像列中没有图像名称...我的代码:

EventMachine::WebSocket::HandshakeError - Connection and Upgrade headers required:

1 个答案:

答案 0 :(得分: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函数来查看图像名称值是否正确。正如您所做的那样,图像名称可能设置为null。