2017-02-24 30 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);