我有定制的 MVC。我正在使用 PDO。我想将图像保存在服务器中并将链接保存到数据库。
public function add(){
// Sanitize POST
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if($post['submit']){
if($post['title'] == '' || $post['body'] == '' || $post['link'] == ''){
Messages::setMsg('Please Fill In All Fields', 'error');
return;
}
$images=$_FILES['upic']['name'];
$tmp_dir=$_FILES['upic']['tmp_name'];
$imageSize=$_FILES['upic']['size'];
$upload_dir='uploads/';
$imgExt=strtolower(pathinfo($images,PATHINFO_EXTENSION));
$valid_extensions=array('jpeg', 'jpg', 'png', 'gif', 'pdf');
$picProfile=rand(1000, 1000000).".".$imgExt;
move_uploaded_file($tmp_dir, $upload_dir.$picProfile);
// Insert into MySQL
$this->query('INSERT INTO shares (title, body, link, user_id, upic) VALUES(:title, :body, :link, :user_id, :upic)');
$this->bind(':title', $post['title']);
$this->bind(':body', $post['body']);
$this->bind(':link', $post['link']);
$this->bind(':user_id', 1);
$this->bind(':upic', $post['upic']);
$this->execute();
// Verify
if($this->lastInsertId()){
// Redirect
header('Location: '.ROOT_URL.'shares');
}
}
return;
}
使用此代码,图像保存在服务器中,但我在数据库中没有得到任何信息。当我删除图像插入代码时,所有其他数据存储都很好。如何在服务器中上传图像并将链接保存在数据库表中?
我收到以下错误
注意:未定义索引:upic 在 C:\xampp\htdocs\test3\models\share.php 第 36 行
最佳答案
你在这里做错了。你可以试试这个。
$dbhost = "localhost";
$dbname = "mypdo";
$dbusername = "root";
$dbpassword = "123456";
$link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);
$statement = $link->prepare("INSERT INTO shares (title, body, link, user_id, upic) VALUES(:title, :body, :link, :user_id, :upic)");
$statement->execute(array(
"title" => $post['title'],
"body" => $post['body'],
"link" => $post['link'],
"user_id" => 1,
"upic" => $upload_dir.$picProfile,
));
关于php - 将图像保存在服务器中并链接到数据库 PHP 数据对象 PDO OOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45453208/