我目前正在尝试使用php将图像上传到数据库。此时您可以上传图像,它们将被放置在名为images的文件夹中。这工作正常,但现在我想把它放到数据库中,由于某种原因,我的功能不起作用:(
upload.php的
<form action="upload.php" method="post" enctype="multipart/form-data">
<h2>Select Image:</h2><input type="file" name="image"> <br/>
<input type="submit" name="upload" Value="Upload now"><br/>
<?php
$saveImage = $user->saveImage($name,$image_name);
if(isset($_POST['upload'])){
$image_name = $_FILES['image'] ['name'];
$image_type = $_FILES['image'] ['type'];
$image_size = $_FILES['image'] ['size'];
$image_tmp_name = $_FILES['image'] ['tmp_name'];
saveImage($name,$image_name);
if($image_name==''){
echo "<script>alert('Please Select a file')</script>";
exit();
}
else {
move_uploaded_file($image_tmp_name, "images/$image_name");
echo "<p>Image uploaded succesfully</p><br/>";
echo "<img src='images/$image_name'>";
}
}
?>
class.user.php
public function saveImage($name,$image_name){
$stmt = $this->db->prepare("INSERT INTO images(image_name,image_image)
VALUES(:name, :image_name)");
$stmt->bindparam(":name", $name);
$stmt->bindparam(":image_name", $image_name);
$stmt->execute();
return $stmt;
}
我正在尝试构建一个自定义CMS,以了解OOP,因为我是面向对象编程的新手,我觉得很难。也许你在这里看错了?
错误:
注意:未定义的变量:第83行的/Applications/MAMP/htdocs/test/upload.php中的名称
注意:未定义的变量:第83行的/Applications/MAMP/htdocs/test/upload.php中的image_name
致命错误:未捕获异常'PDOException',消息'SQLSTATE [23000]:完整性约束违规:1048列'image_name'不能为null'/Applications/MAMP/htdocs/test/class.user.php:148 Stack trace:#0 /Applications/MAMP/htdocs/test/class.user.php(148):PDOStatement-&gt; execute()#1 /Applications/MAMP/htdocs/test/upload.php(83):USER-&gt ;第14行/Applications/MAMP/htdocs/test/class.user.php中抛出的saveImage(NULL,NULL)#2 {main}
答案 0 :(得分:0)
基本image_name
为空,这是您的数据库约束所不允许的。
好吧,$name
永远不会被设置,这反过来又违反了image_name NOT NULL的数据库约束。
答案 1 :(得分:0)
$saveImage = $user->saveImage($name,$image_name);
将此行删除到您的代码