2016-05-09 39 views
0

我有一个带有字段和一个下拉列表的表单。下拉列表中的数据出现在表格中:categorie。 (字段是:catID,catName)从下拉列表中将ID保存到其他表中

当我从下拉列表中选择一个分类并填充所有其他输入字段时,它将所有输入字段以及只有来自分类表的catName保存到tabel:事件中。 我怎样才能将所选的catID从categorie表中保存到事件表中?

有人可以让我出去吗?

的问候,本尼

<?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

您在<option id="id"></option> 犯了一个错误,如果从选择的孩子,你想利用价值。

您必须将id更改为value =“”或者您必须添加value =“id”proper。

改变你的选择框块完全像下面..

<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查询小费......尽自己的不使用"select * from"使用它,如果你不需要所有colums能力比 “*”

$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

我更新我的anser,请检查它。我使用爆炸从一个选项获取2值... –

+0

现在没有任何反应,catID或catName不存储在表中。 – Benny

0

不如我你missusing的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存储到下一页的列表中,则可以在下一页中重新运行查询并将它们存储在数组中。

相关问题