2017-02-04 113 views
-1

我正在尝试使用表单发布新帖子。我无法在MySQL上插入图像

在这种形式中,我有4个参数(标题,描述/标签,完整描述和图像)。当我使用后端PHP API从应用程序接收数据时,我在MySQL数据库中插入一行。

基本上,我可以插入行的一切,除了图像。这里是我的代码:

<?php 
try 
{ 
    $db = new PDO('mysql:host=localhost;dbname=social;charset=utf8', 'root', 'root'); 
} 
catch(Exception $e) 
{ 
    die('Erreur : '.$e->getMessage()); 
} 
// $security = new White\Security; 

$post = $_POST; 
$file_tmp= $_FILES['img']; 
$img = file_get_contents($file_tmp); 
echo $img; 
$title = addslashes($post['title']); 
$description = addslashes($post['description']); 
$fullDesc = addslashes($post['full']); 

$sql = "INSERT INTO posts (title, description, img, fullDesc, likes) VALUES ('$title', '$description', '$img', '$fullDesc', 0)"; 
echo $sql; 
$db->exec($sql); 
$status = array('status' => "success"); 
// header("Access-Control-Allow-Origin: *"); 
header("Location: form.php"); 

和,这里是我的表:https://i.stack.imgur.com/Ly3gP.png

我搜索在谷歌的东西,这将解决我的问题,但我什么也没找到,所以这就是为什么我问这个问题,在这个论坛上!

+0

你真的想把图像放到表中吗?为什么不只是上传,然后保存在表中只上传图像的路径? – RIKI

+0

您是否尝试过使用准备的陈述? –

+0

@OTARIKI因为,我有一个无限的DB,只有15GB的磁盘。 –

回答

-1

mysql_escape_string($ img); 可能是你可以在百度搜索,当你找不到任何东西..

+0

OP哪里需要添加这个转义,你能否提供一个链接到文档(以及逃生的描述)?这个答案是非常短的... –

+0

我使用PHP 7,所以没有MySQL扩展 –