2017-02-24 117 views
1

我有2个问题,我的第一个问题是我的PHP脚本有错误。它基本上给我这个我的SQL查询不起作用+在MariaDB中插入图片

Fatal error: Uncaught Error: Cannot pass parameter 2 by reference in /customers/1/d/9/the-scientist.fr/httpd.www/api/addPost.php:30 Stack trace: #0 {main} thrown in /customers/1/d/9/the-scientist.fr/httpd.www/api/addPost.php on line 30 

我的第二个问题是,我想在MariaDB的一排插入一张图片,我想做同样的事情的phpMyAdmin在一个BLOB列的图像插入。所以,这是我的PHP脚本:

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

$post = $_POST; 
$img = base64_encode(file_get_contents($_FILES['img']['tmp_name'])); 
$title = addslashes($post['title']); 
$description = addslashes($post['description']); 
$fullDesc = addslashes($post['full']); 
// if (!empty($title) & !empty($description) & !empty($fullDesc) & !empty($img)) { 

// } 
// else { 
// // header("Location: form.php?error=Fill the form!"); 
// } 

$stmt = $db->prepare("INSERT INTO posts (title, description, img, fullDesc, likes) VALUES (:title, :description, :img, :fullDesc, :likes)"); 

$stmt->bindParam(':title', $title); 
$stmt->bindParam(':description', $description); 
$stmt->bindParam(':img', $img); 
$stmt->bindParam(':fullDesc', $fullDesc); 
$stmt->bindParam(':likes', 0); 

$stmt->execute(); 

    // header("Access-Control-Allow-Origin: *"); 
header("Location: form.php?error=$sql"); 

此外,这是形式:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionic/1.3.2/css/ionic.min.css"> 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<form enctype="multipart/form-data" class="form" id="form" action="./addPost.php" method="POST"> 
     <div class="list"> 
      <label class="item item-input"> 
      <input type="text" placeholder="Titre" class="AddPosttitle" name="title"> 
      </label> 
      <label class="item item-input"> 
      <input class="description" type="text" placeholder="Mot Clés" maxlength="60" name="description"> 
      </label> 
      <label class="item item-input"> 
      <div> 
       <span id='button_upload'>Image : </span> 
       <input type='file' class="img" name="img"> 
      </div> 
      </label> 
      <label class="item item-input"> 
      <textarea placeholder="Description" class="full" name="full"></textarea> 
      </label> 
      <div class="padding"> 
       <button class="button button-block button-positive submit-btn" type="submit"> 
       Envoyer 
      </button> 
      </div> 
     </div> 
    </form> 
    <style type="text/css"> 
     .form { 
      background: #FFF; 

     } 
    </style> 
    <?php 
    if (!empty($_GET['error'])){ 
     ?> 
     <script type="text/javascript"> 
      function findGetParameter(parameterName) { 
       var result = null, 
        tmp = []; 
       var items = location.search.substr(1).split("&"); 
       for (var index = 0; index < items.length; index++) { 
        tmp = items[index].split("="); 
        if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); 
       } 
       return result; 
      } 
      alert(findGetParameter("error")); 
     </script><?php 
    } 
    ?> 

我认为,在这一点上,我的问题是很清楚的,如果您需要了解更多信息,请在评论部分。

+1

我不知道这是否是一个在线测试问题。但是您应该考虑不要将合法的PDO连接放在数据库中。 (如果这是合法的连接) –

+0

关于你的第二个问题:你刚刚陈述了你想要做的和你正在使用的代码,但没有问题是什么。插入是否无效,数据不是您期望的等等? – jeroen

+0

@ B.Dionys别担心,我知道我在做什么,只能从我的服务器访问MariaDB服务器。 –

回答

2

关于第一个问题:您正在使用:

$stmt->bindParam(':likes', 0); 

bindParam()需要一个参数,一个变量。

如果你只是想绑定一个值,你应该使用bindValue()代替:

$stmt->bindValue(':likes', 0);