我遇到了将子行插入到我的数据库中的问题。生病总是得到错误插入错误: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