早上好! 最近,我尝试将图像与其他String类型数据一起插入数据库。它适用于String类型的数据,但不适用于BLOB类型的图像。我注意到,仅插入图像名称,而不插入图像。我是PHP脚本和SQL数据库的初学者。有人可以帮我吗?:)
$fname = mysqli_real_escape_string($connect, $_POST['yname']);
$lname = mysqli_real_escape_string($connect, $_POST['email']);
$age = mysqli_real_escape_string($connect, $_POST['name']);
$url = mysqli_real_escape_string($connect, $_POST['url']);
$color1 = mysqli_real_escape_string($connect, $_POST['color1']);
$color2 = mysqli_real_escape_string($connect, $_POST['color2']);
$contact1 = mysqli_real_escape_string($connect, $_POST['contact1']);
$contact2 = mysqli_real_escape_string($connect, $_POST['contact2']);
$contact3 = mysqli_real_escape_string($connect, $_POST['contact3']);
$contact4 = mysqli_real_escape_string($connect, $_POST['contact4']);
$bio = mysqli_real_escape_string($connect, $_POST['message']);
$farewell = mysqli_real_escape_string($connect, $_POST['farewell']);
$ppname = mysqli_real_escape_string($connect, $_POST['ppname']);
$password = mysqli_real_escape_string($connect, $_POST['password']);
$dadress = mysqli_real_escape_string($connect, $_POST['dadress']);
$version = "full";
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
$filename2 = $_FILES['uploadfile2']['name'];
$filetmpname2 = $_FILES['uploadfile2']['tmp_name'];
$folder = 'imagesuploadedf/';
// edited and added below code. it will check if folder exists and create if not exists.....
$foldername = 'imagesuploadedf';
if ( ! is_dir($foldername)) {
mkdir($foldername); }
// end of edited and added code
move_uploaded_file($filetmpname, $folder.$filename);
move_uploaded_file($filetmpname2, $folder.$filename2);
// $sql = "INSERT INTO `uploadedimage` (`imagename`) VALUES ('$filename')";
// connect to mysql database using mysqli
// mysql query to insert data
$query = "INSERT INTO `tabela`(`name`, `url`, `email`, `imagename`, `imagename2`, `color1`, `color2`, `contact1`, `contact2`, `contact3`, `contact4`, `bio`, `farewell`, `ppname`, `version`) VALUES ('$fname', '$lname', '$age', '$filename', '$filename2', '$color1', '$color2', '$contact1', '$contact2', '$contact3', '$contact4', '$bio', '$farewell', '$ppname', '$version')";
$sql = "INSERT INTO `tabela`(`name`, `url`, `email`, `color1`, `color2`, `contact1`, `contact2`, `contact3`, `contact4`, `bio`, `farewell`, `ppname`, `version`, `dadress`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($connect);
if(!mysqli_stmt_prepare($stmt, $sql)){
echo "Error";
} else{
mysqli_stmt_bind_param($stmt, "ssssssssssssss", $fname, $url, $lname, $color1, $color2, $contact1, $contact2, $contact3, $contact4, $bio, $farewell, $ppname, $version, $dadress);
mysqli_stmt_execute($stmt);
}
$sql2 = "UPDATE tabela SET imagename = '$filename', imagename2 = '$filename2' WHERE ppname = '$ppname'";
mysqli_query($connect,$sql2);
答案 0 :(得分:0)
如果您的BLOB字段为imagename
,则不要使用$filename = $_FILES['uploadfile']['name']
将图像名称分配给变量,而应使用此代码获取图像数据
$imageBlob = fopen($filetmpname, 'rb');
或
$imageBlob = file_get_content($filetmpname);
,然后使用此$imageBlob
通过简单的更新查询将其存储在数据库列中。