[英]Save image in Server and link in database PHP Data Objecta PDO OOP
我已定制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;
}
使用此代碼圖像可以保存在服務器中,但是數據庫中什么也沒有。 當我刪除圖像插入代碼時,所有其他數據存儲都很好。 如何在服務器中上傳圖像並將鏈接保存在數據庫表中?
我收到以下錯誤
注意:未定義的索引:第36行的C:\\ xampp \\ htdocs \\ test3 \\ models \\ share.php中的upic
您在這里做錯了。 你可以試試看。
$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,
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.