【发布时间】:2018-02-10 21:10:12
【问题描述】:
当我尝试在具有多个外键的表上插入数据时。此表单在导航栏上有一个 ID。
所以我创建了一个表单,在其中从不同的表(链接到我要插入值的表)中选择值,并在同一页面中执行操作:
<form method="post" action="backend_valuechain.php?id=<?php echo $_GET['id'] ?>
<label>Nom du segment</label>
<input type="text" class="form-control input-lg" id="segmentname"/>
<label>My Select Name</label>
<select id="select1">
<?php
foreach ($pdo->query($typosegment = 'SELECT ID_SEGMENT_VC_TYPO, SEGMENT_VC_TYPOLOGY FROM segment_vc_typo;') as $row) {
echo '<option name="' . $row['SEGMENT_VC_TYPOLOGY'] . '" value="' .$row['ID_SEGMENT_VC_TYPO'] . '"> ' . $row['SEGMENT_VC_TYPOLOGY'] . '</option>'; } ?>
</select>
......
我还有其他几个选择和输入文本,但为了让我的问题更易于阅读,我只是抽取了一个示例。创建表单后,我计划将数据插入数据库。
<?php
try{
$vcNewSegment = "INSERT INTO segment_vc (ID_VC,SEGMENT_VC_NAME,ID_SEGMENT_VC_TYPO) VALUES (:id,:segmentname,:typosegment)";
$stmt = $pdo->prepare($vcNewSegment);
$stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$stmt->bindParam(':segmentname', $_REQUEST['segmentname'], PDO::PARAM_STR);
$stmt->bindParam(':typosegment', $_REQUEST['typosegment'], PDO::PARAM_INT);
$stmt->execute();
echo "OK !";
}
catch(PDOException $e){
die("ERROR: Could not able to execute $vcNewSegment. " .
$e->getMessage());
}
unset($pdo);
?>
当我通过表单插入数据时,我在表中看不到输入,但在此表中插入了“NULL”值(我删除了一些不相关的列):
CREATE TABLE `segment_vc` (
`ID_SEGMENT_VC` int(20) NOT NULL,
`ID_VC` int(20) DEFAULT NULL,
`ID_SEGMENT_VC_TYPO` int(20) DEFAULT NULL,
`SEGMENT_VC_NAME` varchar(90) DEFAULT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
ID_SEGMENT_VC 是主键 ID_VC 是一个外键(它出现在 URL 中).. ID_SEGMENT_TYPO 也是外键
【问题讨论】:
标签: php mysql sql pdo prepared-statement