gpt4 book ai didi

php - 使用php将图像插入mysql

转载 作者:行者123 更新时间:2023-11-29 04:48:09 26 4
gpt4 key购买 nike

我正在尝试使用来自 session 变量的用户 user_id 将上传的图像插入到 mysql 中。我找了一个在线教程,改成如下:

<?php
require_once('../Login/includes/session_timeout_db.inc.php');
$dbuser="";
$dbname="";
$dbpass="";
$dbserver="";

$con = mysql_connect($dbserver,$dbuser,$dbpass);
if (!$con){ die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);

// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


// Create the query and insert
// into our database.
$query = "INSERT INTO images, owner_id";
$query .= "(image) VALUES ('$data', '.$_SESSION['id'].')";
$results = mysql_query($query, $link);

// Print results
print "Thank you, your file has been uploaded.";

}
else {
print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8” />
<title>Manage Tasks</title>
<link rel="stylesheet" type="text/css" href="../allcss/style.css" />
</head>

<body>

<?php
require('../Login/includes/header.inc.php');
?>

<div id="content">
<form enctype="multipart/form-data" action="" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
</div>

</body>
</html>

我在查询的第二行遇到错误:

$query = "INSERT INTO images, owner_id";
$query .= "(image) VALUES ('$data', '.$_SESSION['id'].')";
$results = mysql_query($query, $link);

*错误是解析错误:语法错误,意外的 T_ENCAPSED_AND_WHITESPACE,期望在第 28 行出现 T_STRING 或 T_VARIABLE 或 T_NUM_STRING*

有人可以帮忙吗?

提前致谢

最佳答案

您的 images 字段需要是 BLOB 类型.但是不要这样做,将文件保存在文件系统中,并将目录路径保存到数据库中的图像。

关于php - 使用php将图像插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599286/

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