使用 [image] 类型的列插入并存储在 table 中的数据库文件中
Insert and store in database file in table with a column of type [image]
问题:我正在尝试将图像直接上传到数据库table 在此列 logo [image] NULL
这个文件是 register.php
require __DIR__ . '/lib/library.php';
$app = new Users();
$login_error_message = '';
$register_error_message = '';
// check Register request
if (!empty($_POST['btnRegister'])) {
if ($_POST['name'] == "") {
$register_error_message = 'Name field is required!';
} else if ($_POST['email'] == "") {
$register_error_message = 'Email field is required!';
} else if ($_POST['tel_number'] == "") {
$register_error_message = 'Nº Telemóvel field is required!';
} else if ($_POST['login'] == "") {
$register_error_message = 'Username field is required!';
} else if ($_POST['nif'] == "") {
$register_error_message = 'NIF field is required!';
} else if ($_POST['role_id'] == "") {
$register_error_message = 'Perfil field is required!';
}else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$register_error_message = 'Invalid email address!';
} else if ($app->isEmail($_POST['email'])) {
$register_error_message = 'Email is already in use!';
} else if ($app->isUsername($_POST['login'])) {
$register_error_message = 'Username is already in use!';
} else {
}
if(getimagesize($_FILES['logo']['tmp_name'])==FALSE){
$image = NULL;
}else{
$image = $_FILES['logo']['tmp_name'];
$image = addslashes(file_get_contents($image));
$user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['tel_number'], $_POST['nif'], $_POST['password'], $_POST['role_id'], $_POST['login'], $image);
// set session and redirect user to the profile page
$_SESSION['user_id'] = $user_id;
//header("Location: profile.php");
}
}
此函数负责向数据库中插入数据,如您所见,我正在尝试将图像作为 Blob 插入,但每当我尝试向 table 和徽标文件存在 returns 以下错误:
Array ( [0] => IMSSP [1] => -7 [2] => 将输入参数 8 的字符串转换为 UCS-2 时发生错误:错误代码 0x0 )
如果我不提交徽标文件,它 returns 会出现不同的错误:
getimagesize(): 文件名不能为空
public function Register($name, $email, $tel_number, $NIF, $password, $role_id, $login, $logo)
{
try {
$db = DB();
$query = $db->prepare("INSERT INTO start_users(name, email, tel_number, NIF, psw, role_id, login, logo) VALUES (:name,:email,:tel_number,:NIF,:psw,:role_id,:login,:logo)");
$query->bindParam("name", $name, PDO::PARAM_STR);
$query->bindParam("email", $email, PDO::PARAM_STR);
$query->bindParam("tel_number", $tel_number, PDO::PARAM_INT);
$query->bindParam("NIF", $NIF, PDO::PARAM_INT);
$enc_password = password_hash ($password, PASSWORD_DEFAULT);
$query->bindParam("psw", $enc_password, PDO::PARAM_STR);
$query->bindParam("role_id", $role_id, PDO::PARAM_INT);
$query->bindParam("login", $login, PDO::PARAM_INT);
$query->bindParam("logo", $logo, PDO::PARAM_LOB);
$query->execute();
print_r($query->errorInfo());
return $db->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
部分表单报错
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="">Logo</label>
<input type="file" name="logo" accept="image/x-png,image/gif,image/jpeg" class="form-control"/>
</div>
<div class="form-group">
<input type="submit" name="btnRegister" class="btn btn-primary" value="Register"/>
</div>
</form>
说明:
如果您的问题被正确标记并且您使用 PHP 驱动程序作为 SQL 服务器(sqlsrv
标记),那么您需要为 :logo
指定二进制编码参数:
$query->bindParam(":logo", $logo, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
您还可以考虑以下内容:
image
数据类型将在 SQL 服务器的未来版本中删除,应替换为 varbinary(max)
数据类型。
- 如果您使用命名占位符,请使用以下格式的参数名称
:name
。
PHP(根据您的代码):
<?php
public function Register($name, $email, $tel_number, $NIF, $password, $role_id, $login, $logo)
{
try {
$db = DB();
$query = $db->prepare("INSERT INTO start_users(name, email, tel_number, NIF, psw, role_id, login, logo) VALUES (:name,:email,:tel_number,:NIF,:psw,:role_id,:login,:logo)");
$query->bindParam(":name", $name, PDO::PARAM_STR);
$query->bindParam(":email", $email, PDO::PARAM_STR);
$query->bindParam(":tel_number", $tel_number, PDO::PARAM_INT);
$query->bindParam(":NIF", $NIF, PDO::PARAM_INT);
$enc_password = password_hash ($password, PASSWORD_DEFAULT);
$query->bindParam(":psw", $enc_password, PDO::PARAM_STR);
$query->bindParam(":role_id", $role_id, PDO::PARAM_INT);
$query->bindParam(":login", $login, PDO::PARAM_INT);
$query->bindParam(":logo", $logo, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
$query->execute();
print_r($query->errorInfo());
return $db->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
?>
工作示例:
<?php
# Connection info
$server = 'server\instance';
$database = 'database';
$uid = 'username';
$pwd = 'password';
# Connection
try {
$dbh = new PDO("sqlsrv:server=$server;Database=$database", $uid, $pwd);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch( PDOException $e ) {
die("Error connecting to SQL Server. ".$e->getMessage());
}
# Insert data
try {
$logo = file_get_contents('image.jpg');
$stmt = $dbh->prepare("INSERT INTO start_users(logo) VALUES (:logo)");
$stmt->bindParam(":logo", $logo, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
$result = $stmt->execute();
if ($result === false) {
die( "Error executing stored procedure.");
}
} catch( PDOException $e ) {
die( "Error executing stored procedure: ".$e->getMessage());
}
# End
$stmt = null;
?>
问题:我正在尝试将图像直接上传到数据库table 在此列 logo [image] NULL
这个文件是 register.php
require __DIR__ . '/lib/library.php';
$app = new Users();
$login_error_message = '';
$register_error_message = '';
// check Register request
if (!empty($_POST['btnRegister'])) {
if ($_POST['name'] == "") {
$register_error_message = 'Name field is required!';
} else if ($_POST['email'] == "") {
$register_error_message = 'Email field is required!';
} else if ($_POST['tel_number'] == "") {
$register_error_message = 'Nº Telemóvel field is required!';
} else if ($_POST['login'] == "") {
$register_error_message = 'Username field is required!';
} else if ($_POST['nif'] == "") {
$register_error_message = 'NIF field is required!';
} else if ($_POST['role_id'] == "") {
$register_error_message = 'Perfil field is required!';
}else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$register_error_message = 'Invalid email address!';
} else if ($app->isEmail($_POST['email'])) {
$register_error_message = 'Email is already in use!';
} else if ($app->isUsername($_POST['login'])) {
$register_error_message = 'Username is already in use!';
} else {
}
if(getimagesize($_FILES['logo']['tmp_name'])==FALSE){
$image = NULL;
}else{
$image = $_FILES['logo']['tmp_name'];
$image = addslashes(file_get_contents($image));
$user_id = $app->Register($_POST['name'], $_POST['email'], $_POST['tel_number'], $_POST['nif'], $_POST['password'], $_POST['role_id'], $_POST['login'], $image);
// set session and redirect user to the profile page
$_SESSION['user_id'] = $user_id;
//header("Location: profile.php");
}
}
此函数负责向数据库中插入数据,如您所见,我正在尝试将图像作为 Blob 插入,但每当我尝试向 table 和徽标文件存在 returns 以下错误: Array ( [0] => IMSSP [1] => -7 [2] => 将输入参数 8 的字符串转换为 UCS-2 时发生错误:错误代码 0x0 )
如果我不提交徽标文件,它 returns 会出现不同的错误: getimagesize(): 文件名不能为空
public function Register($name, $email, $tel_number, $NIF, $password, $role_id, $login, $logo)
{
try {
$db = DB();
$query = $db->prepare("INSERT INTO start_users(name, email, tel_number, NIF, psw, role_id, login, logo) VALUES (:name,:email,:tel_number,:NIF,:psw,:role_id,:login,:logo)");
$query->bindParam("name", $name, PDO::PARAM_STR);
$query->bindParam("email", $email, PDO::PARAM_STR);
$query->bindParam("tel_number", $tel_number, PDO::PARAM_INT);
$query->bindParam("NIF", $NIF, PDO::PARAM_INT);
$enc_password = password_hash ($password, PASSWORD_DEFAULT);
$query->bindParam("psw", $enc_password, PDO::PARAM_STR);
$query->bindParam("role_id", $role_id, PDO::PARAM_INT);
$query->bindParam("login", $login, PDO::PARAM_INT);
$query->bindParam("logo", $logo, PDO::PARAM_LOB);
$query->execute();
print_r($query->errorInfo());
return $db->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
部分表单报错
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="">Logo</label>
<input type="file" name="logo" accept="image/x-png,image/gif,image/jpeg" class="form-control"/>
</div>
<div class="form-group">
<input type="submit" name="btnRegister" class="btn btn-primary" value="Register"/>
</div>
</form>
说明:
如果您的问题被正确标记并且您使用 PHP 驱动程序作为 SQL 服务器(sqlsrv
标记),那么您需要为 :logo
指定二进制编码参数:
$query->bindParam(":logo", $logo, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
您还可以考虑以下内容:
image
数据类型将在 SQL 服务器的未来版本中删除,应替换为varbinary(max)
数据类型。- 如果您使用命名占位符,请使用以下格式的参数名称
:name
。
PHP(根据您的代码):
<?php
public function Register($name, $email, $tel_number, $NIF, $password, $role_id, $login, $logo)
{
try {
$db = DB();
$query = $db->prepare("INSERT INTO start_users(name, email, tel_number, NIF, psw, role_id, login, logo) VALUES (:name,:email,:tel_number,:NIF,:psw,:role_id,:login,:logo)");
$query->bindParam(":name", $name, PDO::PARAM_STR);
$query->bindParam(":email", $email, PDO::PARAM_STR);
$query->bindParam(":tel_number", $tel_number, PDO::PARAM_INT);
$query->bindParam(":NIF", $NIF, PDO::PARAM_INT);
$enc_password = password_hash ($password, PASSWORD_DEFAULT);
$query->bindParam(":psw", $enc_password, PDO::PARAM_STR);
$query->bindParam(":role_id", $role_id, PDO::PARAM_INT);
$query->bindParam(":login", $login, PDO::PARAM_INT);
$query->bindParam(":logo", $logo, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
$query->execute();
print_r($query->errorInfo());
return $db->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
?>
工作示例:
<?php
# Connection info
$server = 'server\instance';
$database = 'database';
$uid = 'username';
$pwd = 'password';
# Connection
try {
$dbh = new PDO("sqlsrv:server=$server;Database=$database", $uid, $pwd);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch( PDOException $e ) {
die("Error connecting to SQL Server. ".$e->getMessage());
}
# Insert data
try {
$logo = file_get_contents('image.jpg');
$stmt = $dbh->prepare("INSERT INTO start_users(logo) VALUES (:logo)");
$stmt->bindParam(":logo", $logo, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
$result = $stmt->execute();
if ($result === false) {
die( "Error executing stored procedure.");
}
} catch( PDOException $e ) {
die( "Error executing stored procedure: ".$e->getMessage());
}
# End
$stmt = null;
?>