簡體   English   中英

MySQL / PHP(外鍵錯誤?)

[英]MySQL / PHP ( Foreign key mistake? )

我遇到了問題^^
我的PHP函數不想保存到我的MySQL表中,
但會將其保存到我選擇的文件夾中。 (連接有效(已測試),也可以從該位置將內容插入另一個表。)
我認為這與外鍵有關,因為當我作為示例執行此命令時

INSERT INTO TBilder (BildPfad, BildFreigabe, UserId)VALUES ('asd', 0, 10005);  

有用。 它按預期執行所有操作。

<div class="image_upload_div">
<form action= "#" class="dropzone"></form>
</div>

<?php
                    if (!empty($_FILES)) {
                        if (!is_dir($dir . "users") && !is_writable($dir. "users")) {
                            mkdir($dir . "users");
                        }
                        $temp = $_FILES['file']['tmp_name'];
                        $users = $dir . "users" . $ds;
                        $destination = $users . $_SESSION['userid'] . $ds;
                        $target = $destination . $_FILES['file']['name'];
                        if (!is_dir($destination) && !is_writable($destination)) {
                            mkdir($destination);
                        }

                        move_uploaded_file($temp, $target);

                        $stmt = $pdo->prepare("INSERT INTO TBilder (BildPfad, BildFreigabe, UserId) VALUES (:pfad, :freigabe, :user)");
                        $stmt->bindparam(":pfad", $target);
                        $stmt->bindparam(":freigabe", 0);
                        $stmt->bindparam(":user", $id);
                        $stmt->execute();

                    }

而我的整個數據庫:

DROP DATABASE SonderDB;
CREATE DATABASE SonderDB; 
use SonderDb;

DROP TABLE IF EXISTS TUsers;

CREATE TABLE IF NOT EXISTS TUsers (
  UserId INT AUTO_INCREMENT,
  UserName VARCHAR(255) NOT NULL,
  UserPassword VARCHAR(255) NOT NULL,
  PRIMARY KEY (UserId)) 
  AUTO_INCREMENT = 10000,
  ENGINE = InnoDB;



DROP TABLE IF EXISTS TBilder ;

CREATE TABLE IF NOT EXISTS TBilder (
  BildId INT AUTO_INCREMENT,
  BildPfad VARCHAR(255) NOT NULL,
  BildFreigabe INT,
  UserId INT,
  PRIMARY KEY (BildId),
  FOREIGN KEY (UserId) REFERENCES TUsers(UserId) ON UPDATE CASCADE)
  AUTO_INCREMENT = 10000
ENGINE = InnoDB;
  • 因為BildFreigabeUserId定義為INT ,所以應該這樣准備它們(請參閱我的代碼中的getInputParameterDataType() )。 我相信80%這是您的問題。
  • 我還要注意BildPfad ,因為它被定義為NOT NULL
  • 如果可以的話,我建議您使用異常處理,尤其是在db操作上。

如果您願意,我寫了更多代碼,以便為您提供全局視圖。 祝好運。


<?php

try {
    $pdo = getConnection();

    $sql = 'INSERT INTO TBilder (BildPfad, BildFreigabe, UserId) VALUES (:pfad, :freigabe, :user)';
    $stmt = $pdo->prepare($sql);

    if (!$stmt) {
        throw new Exception('The SQL statement can not be prepared!');
    }

    // Bind parameters by checking their datta type too!
    $stmt->bindparam(":pfad", $target, getInputParameterDataType($target));
    $stmt->bindparam(":freigabe", $freigabe, getInputParameterDataType(0));
    $stmt->bindparam(":user", $user, getInputParameterDataType($user));

    //********************************************************************
    // Try with bindValue(), if it's not working with bindParam().
    // It should. I normally work with bindValue().
    //********************************************************************
    // $stmt->bindValue(':pfad', $target, getInputParameterDataType($target));
    // $stmt->bindValue(':freigabe', $freigabe, getInputParameterDataType(0));
    // $stmt->bindValue(':user', $user, getInputParameterDataType($user));
    //********************************************************************

    if (!$stmt->execute()) {
        throw new Exception('The PDO statement can not be executed!');
    }

    $inserted = $stmt->rowCount() > 0 ? TRUE : FALSE;

    echo $inserted ? 'Inserted successfully' : 'Not inserted!';
} catch (PDOException $pdoException) {
    echo '<pre>' . print_r($pdoException, true) . '</pre>';
    exit();
} catch (Exception $exception) {
    echo '<pre>' . print_r($exception, true) . '</pre>';
    exit();
}

function getConnection() {
    $conn = new PDO('mysql:host=localhost;port=36;dbname=[...]', '[...]', '[...]');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $conn->setAttribute(PDO::ATTR_PERSISTENT, true);
    return $conn;
}

function getInputParameterDataType($value) {
    $dataType = PDO::PARAM_STR;
    if (is_int($value)) {
        $dataType = PDO::PARAM_INT;
    } elseif (is_bool($value)) {
        $dataType = PDO::PARAM_BOOL;
    }
    return $dataType;
}

暫無
暫無

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

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