我遇到了將子行插入到我的數據庫中的問題。生病總是得到錯誤插入錯誤:SQLSTATE [23000]:完整性約束違規:1452不能添加或更新子行,外鍵約束失敗(doku
server
,約束kunde->server
外鍵(kunden_id
)參考文獻kunden
(id
))完整性約束違規:1452無法將子行添加或更新到我的數據庫中
守則是
<form method="POST" action="server-eintragen.php">
<div class="form-group">
<label for="firmenname">Servername</label>
<input type="text" class="form-control" name="name" placeholder="Servername" >
</div>
<div class="form-group">
<label for="Addresse">Bestückung</label>
<input type="text" class="form-control" name="CPU" placeholder="Welche CPU?" >
<input type="text" class="form-control" name="CPU-Anzahl" placeholder="Wieviele Sockets?" >
<input type="text" class="form-control" name="RAM" placeholder="Wieviel RAM" >
<input type="text" class="form-control" name="Festplatte" placeholder="Wie groß ist die Festplatte" >
<input type="hidden" class="form-control" name="kunden_id" value=<?php htmlentities($_GET['kundenid']) ?> >
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Abschicken</button>
</div>
</form>
和
<?php
error_reporting(E_ALL);
error_reporting(-1);
include "../../includes/php/db.php";
$name = htmlentities($_POST['name']);
$cpu = htmlentities($_POST['CPU']);
$cpu_anzahl = htmlentities($_POST['CPU-Anzahl']);
$ram= htmlentities($_POST['RAM']);
$festplatte = htmlentities($_POST['Festplatte']);
$created_at = date("Y-m-d H:i:s");
$updated_at = date("Y-m-d H:i:s");
$kunden_id = htmlentities($_POST['kundenid']);
$stmt = $dbh->prepare('INSERT INTO server (name, cpu, cpu_anzahl, ram, festplatte, created_at, updated_at, kunden_id)VALUES(:name, :cpu, :cpu_anzahl, :ram, :festplatte, :created_at, :updated_at, :kunden_id)');
try {
$stmt->execute(
array(
'name' => $name,
'cpu' => $cpu,
'cpu_anzahl' => $cpu_anzahl,
'ram' => $ram,
'festplatte' => $festplatte,
'created_at' => $created_at,
'updated_at' => $updated_at,
'kunden_id' => $kunden_id
)
);
}
catch (PDOException $e)
{
echo 'Insert Error: ' . $e->getMessage() . "\n";
}
?>
我Databasedesign是這樣的:http://www.directupload.net/file/d/4698/vczudxas_jpg.htm
那麼'$ kunden_id'的值是什麼,它存在於數據庫中嗎? – jeroen
我認爲你在$ kunden_id變量中獲得一個id,它在父表(kunden)中不可用.kindly打印該變量以查看其值 – Shahrukh
$ kunden_id在我的測試用例3中,這是kunde(客戶)在我的數據庫中。是的,回聲也是「3」,所以它應該存在於我的數據庫中,但在服務器端的客戶端[ID]的柱子中存在,我想用新的服務器數據 – n1ghty