2016-05-09 4 views
0

フィールドとドロップダウンリストを持つフォームがあります。ドロップダウンリストのデータは、表categorieから出力されます。ドロップダウンリストのIDを他のテーブルに保存

ドロップダウンリストからカテゴリを選択し、他のすべての入力フィールドを入力すると、すべての入力フィールドが格納され、カテゴリテーブルのcatNameのみがtabel:イベントに保存されます(フィールドはcatID、catName)。 categorieテーブルの選択したcatIDもイベントテーブルに保存するにはどうすればよいですか?

誰かが私をここに案内できますか?あなたが選択した子からのテイク値をしたい場合は、<option id="id"></option> でミスを犯した

よろしく、ベニー

<?php require '../Connections/localhost.php'; ?> 
<?php 
    if(isset($_POST['newEvent'])) { 

     session_start(); 
     $eventName = $_POST['SelCatName']; 
     $eventSDate = $_POST['Event-StartDate']; 
     $eventSTime = $_POST['Event-StartTime']; 
     $eventEDate = $_POST['Event-EndDate']; 
     $eventETime = $_POST['Event-EndTime']; 
     $eventDescription = $_POST['Event-Description']; 
     $catID = $_POST["catID"]; 

    $sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$eventName}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID}')"); 


    header('Location: eventOK.php'); 
    } 
?> 



<form action="" method="post" name="RegisterForm" id="RegisterForm"> 

    <div class="FormElement"> 

     <select name="SelCatName" class="TField" id="SelCatName"> 
      <option selected="selected" id="0">--Selecteer een categorie--</option>   
        <?php 
         $GetAllCategories = $con->query("SELECT * FROM categorie"); 
         while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){ 
        ?> 
      <option id="<?php echo $ViewAllCategories['catID']; ?>"><?php echo $ViewAllCategories ['catName']; ?> </option> 

       <?php } ?>  

     </select> 

答えて

0

idをvalue = ""に変更するか、value = "id"を適切に追加する必要があります。

変更あなたの選択ボックスを完全に下記のようなブロック..

<select name="SelCatName" class="TField" id="SelCatName"> 
<option selected="selected" id="0">--Selecteer een categorie--</option>   
<?php 
$GetAllCategories = $con->query("SELECT * FROM categorie"); 
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){ 
?> 
<option value="<?PHP echo $ViewAllCategories['catID'].'-'.$ViewAllCategories['catName']; ?>"> <?PHP echo $ViewAllCategories['catName']; ?></option> 

<?php } ?>  
</select> 

とmysqlのクエリのためのヒント...あなたはすべてのcolumsする必要がいけない場合、それを使用"select * from"を使用していない自分の能力を最大限にあなたはmissusingしている私には "*"

$con->query("SELECT catID, catName FROM categorie"); 

PHPファイル

<?php require '../Connections/localhost.php'; ?> 
<?php 
    if(isset($_POST['newEvent'])) { 

     session_start(); 

     //$eventName = $_POST['SelCatName']; 
     $eventSDate = $_POST['Event-StartDate']; 
     $eventSTime = $_POST['Event-StartTime']; 
     $eventEDate = $_POST['Event-EndDate']; 
     $eventETime = $_POST['Event-EndTime']; 
     $eventDescription = $_POST['Event-Description']; 
     $catIDs = $_POST["SelCatName"]; 
     $catID = explode("-", $catIDs); 

    $sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$catID[1]}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID[0]}')"); 


    header('Location: eventOK.php'); 
    } 
?> 
+0

ありがとうございます。 ここで、catIDのみがテーブル・イベントに格納されます。 また、catNameは、eventNameフィールド以外のイベントテーブルに格納される必要があります。 – Benny

+0

私のアンサーを更新してください。 1つのオプションから2つの値を取得するためにexplodeを使用します... –

+0

catIDまたはcatNameはテーブルに格納されません。 – Benny

0

よりも良いですIDの属性は、値のためのIDを交換してください:あなたのオプションタグが既にIDと名前を持っている

<select name="SelCatName" class="TField" id="SelCatName"> 

あなたのコードは次のようになります。

したがって
<?php 
    $GetAllCategories = $con->query("SELECT * FROM categorie"); 
    while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){ 
?> 
    <option value="<?php echo $ViewAllCategories['catID']; ?>"> <?php echo $ViewAllCategories ['catName']; ?> </option> 

<?php } ?> 

あなただけSelCatName =としてあなたのポストを見つけます(数値ID) catIDとcatNameのリストを次のページに格納する必要がある場合は、次のページでクエリを再実行して配列に格納することができます。

関連する問題