簡體   English   中英

我的SQL查詢無法正常工作+在MariaDB中插入圖片

[英]My SQL query doesn't work + insert an image in MariaDB

我有2個問題,我的第一個問題是我的PHP腳本有錯誤。 它基本上給我這個

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
    }
    ?>

我認為到此為止,我的問題已經很清楚了,如果您需要更多信息,請在評論部分😄中提問。

關於第一個問題:您正在使用:

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

bindParam()需要一個參數,一個變量。

如果只想綁定一個值,則應改用bindValue()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM