我正在学习面向对象编程的PHP,并希望帮助我在数据库中插入图像。我该怎么办?
通常情况下,数据插入时没有图像,但不能随图像一起输入数据。因此,请解决此问题。
protected function create()
{
$this->validate();
if (!empty($this->errors)) {
return false;
}
$attributes = $this->sanitized_attributes();
$sql = "INSERT INTO " . static::$table_name . " (";
$sql .= join(', ', array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
$result = self::$database->query($sql);
if ($result) {
$this->id = self::$database->insert_id;
}
return $result;
}
插入数据时没有插入图像,但尝试插入带有图像的数据,请帮助我
答案 0 :(得分:0)
尝试一下
<html>
<body>
<?php
$msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_FILES['image']['tmp_name'];
$img = file_get_contents($image);
$con = mysqli_connect('localhost','root','','admin') or die('Unable To connect');
$sql = "insert into images (image) values(?)";
$stmt = mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt, "s",$img);
mysqli_stmt_execute($stmt);
$check = mysqli_stmt_affected_rows($stmt);
if($check==1){
$msg = 'Image Successfullly UPloaded';
}else{
$msg = 'Error uploading image';
}
mysqli_close($con);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<button>Upload</button>
</form>
<?php
echo $msg;
?>
</body>
</html>