2017-09-01 45 views
0

當我嘗試在具有多個外鍵的表上插入數據時。此表單在導航上有一個ID。插入帶有select和URL 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); 
?> 

當我通過表單插入數據,我沒有看到輸入表內,但「空」值插入此表(我刪除了一些列這是不相關):

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也是一個外鍵

回答

0

我不知道在哪裏$_REQUEST['typosegment']從何而來。

但是<select id="select1">的值將由$_POST['select1']獲得。

+0

似乎Davids的$ _REQUEST ['typosegment']'是你的'$ _POST ['select1']';-)因爲每個select選項的值都引用了「ID_SEGMENT_VC_TYPO」字段,它也被用在插入語句中在'backend_valuechain.php'中。 – 2017-09-01 21:20:39

+0

是正確的:typosegment是select1 ... 在POST中更改REQUEST並不會改變我的問題...實際上,我不想插入文本我放入選擇選項,但我在選項: ..在這種情況下,我想在表中插入「1」作爲整數... – davidvera