gpt4 book ai didi

php - 使用 php 准备好的语句将图像(blob)插入 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 16:03:11 25 4
gpt4 key购买 nike

我需要使用准备好的语句将图像插入 mysql 数据库...除了图像之外,其他所有内容都在插入

$name = mysqli_real_escape_string($conn, $_POST['name']);
$category = mysqli_real_escape_string($conn, $_POST['category']);
$privacy = mysqli_real_escape_string($conn, $_POST['privacy']);
$maxmembers = mysqli_real_escape_string($conn, $_POST['maxmembers']);
$description = mysqli_real_escape_string($conn, $_POST['description']);
$file = $_FILES['avatar']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['avatar']['tmp_name']));
$image_name =addslashes($_FILES['avatar']['tmp_name']);
$image_size = getimagesize($_FILES['avatar']['tmp_name']);



$sql = "INSERT INTO hjuma_groups (name, description, category, privacy, maxmembers, owner, avatarname, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL error";
}else {
mysqli_stmt_bind_param($stmt,"sssssssb", $name,$description,$category, $privacy,$maxmembers, $owner, $image_name, $image);
mysqli_stmt_execute($stmt);
}

最佳答案

我认为您缺少的拼图是图像数据上的base64_encode,如下所示:

$name =             $_POST['name']);
$category = $_POST['category'];
$privacy = $_POST['privacy'];
$maxmembers = $_POST['maxmembers']);
$description = $_POST['description']);
$name = $_FILES['avatar']['name'];
$image_name = $_FILES['avatar']['tmp_name'];
$image = base64_encode( file_get_contents( $image_name ) );
$image_size = getimagesize( $image_name );



$sql = "insert into `hjuma_groups`
( `name`, `description`, `category`, `privacy`, `maxmembers`, `owner`, `avatarname`, `avatar`)
values
(?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);

if( !mysqli_stmt_prepare( $stmt, $sql ) ) {
echo "SQL error";
}else {
mysqli_stmt_bind_param( $stmt, "sssssssb", $name, $description, $category, $privacy, $maxmembers, $owner, $image_name, $image );
mysqli_stmt_execute($stmt);
}

关于php - 使用 php 准备好的语句将图像(blob)插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55981845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com