将下拉列表中的ID保存到其他表中


Save ID from dropdown list into other table

我有一个包含字段和一个下拉列表的表单。下拉列表中的数据显示在表中:categorie。(字段为:catID、catName)

当我从下拉列表中选择一个类别并填充所有其他输入字段时,它会将所有输入字段和类别表中的catName保存到tabel:event中。如何将categorie表中选定的catID保存到事件表中?

有人能把我带到这里吗?

问候,Benny

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

您在<option id="id"></option>中犯了一个错误如果你想从select child中获取价值。

您必须将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查询提示。。。尽你所能不要使用"select * from",如果你不需要比"*"更好的所有列,就使用它

$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');
    }
?>

对我来说,你错用了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的列表存储到下一个页面,您可以在下一个页中重新运行查询,并将它们存储在一个数组中。