如何使用mysqli将图像插入数据库?

时间:2018-06-18 15:03:55

标签: php html mysqli xampp

我想将图像插入数据库。我正在使用localhost(使用XAMPP)。我知道将图像存储到数据库而不是上传它们是一种不好的做法,但我必须这样做。这是我的html表单:

<form action="addItem.php" method="post" enctype="multipart/form-data" >
    <h1> Add item </h1>
    <p>
        <label for="a">Name:</label>
        <input id="a" type="text"  name="name" required />
    </p>
    <p>
        <label for="b">Condition:</label>
        <select id="b"  name="condition" type="text" required />
            <option value="Nou">Nou </option>
            <option value="Folosit">Folosit </option>
        </select>
    </p>
    <p>
        <label for="c">End date: </label>
        <input  id="c" type="text" name="end_date" required placeholder="Ex. 2018.06.13 23:59:00"/>
    </p>
    <p>
        <label for="e">Category:</label>
        <select id="e" name="category" required >
            <option value="mobile phones">Mobile Phones</option>
            <option value="cars">Cars</option>
            <option value="houses">Houses</option>
            <option value="tv">TV</option>
            <option value="PC">PC</option>
            <option value="clothes">Clothes</option>
        </select>
    </p>
    <p>
        <label for="f"> Starting Price:</label>
        <input  id="f" type="text" name="startingPrice" required min="0" />
    </p>
    <p>
        <label for="g"> Pictures: </label>
        <input id="g" type="file" accept="image/*" name="image" />
    </p>
    <p>
        <label for="h">Description: </label>
        <textarea  id="h" rows="5" cols="40" name="description">
        </textarea>
    </p>
    <input  type ="submit" value="Add item"  name="submit" />
</form>

这是php代码:

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }    

    $stmt = $conn->prepare("INSERT INTO products (product_name, product_description, seller_id, category_id, image, grad_uzura) 
                            VALUES (?,?,?,?,?,?)");
    $stmt->bind_param("ssiibs", $product_name, $product_description, 
    $seller_id_products, $category_id, $image, $condition);

    // insert into products
    $condition = $_POST['condition'];

    $target = "img\\".basename($_FILES['image']['name']);
    $image = file_get_contents($_FILES['image']['tmp_name']);
    echo $image;
    move_uploaded_file($_FILES['image']['tmp_name'],$target);
    //$imagegetmp = addslashes (file_get_contents($_FILES['pictures']));
    $product_name =$_POST['name'];
    echo "product name :".$product_name."<br>";
    $product_description = $_POST['description'];
    echo "product desc:".$product_description."<br>";
    $afisare=$_SESSION['utilizator'];
    echo "username :".$afisare."<br>";

    $sql = "SELECT user_id FROM users WHERE username ='$afisare' "; //extrag user id din sesiune
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    echo "user_id :".$row['user_id']."<br>";
    $seller_id_products= $row['user_id'];

    $category_name = $_POST['category']; //extrag id-ul categoriei
    echo "category_name :".$category_name."<br>";
    $sql = "SELECT category_id FROM categories WHERE category_name = '$category_name'";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    $category_id= $row['category_id'];
    echo "category id :".$category_id."<br>";

    $stmt->execute();

我测试是否使用echo $ image从表单中提取图像并且它可以正常工作。但在phpmyadmin我没有插入图像。除了图像之外,我表中的所有其他字段都包含正确的数据,所以我认为我的问题不在于准备好的语句。我还尝试修改Apache配置中文件的最大大小。我不知道我做错了什么。

0 个答案:

没有答案