早上好!最近我尝试将图像和其他字符串类型的数据一起插入数据库。它适用于字符串类型的数据,但不适用于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);
1条答案
按热度按时间5gfr0r5j1#
如果你的blob字段是
imagename
然后使用$filename = $_FILES['uploadfile']['name']
,使用此代码获取图像数据或
然后用这个
$imageBlob
使用简单的更新查询存储在db列中。