Benny Benny - 6 months ago 36
SQL Question

Save ID from dropdown list into other table

I have a form with fields and one dropdown list. The data from the dropdown list comes out the table: categorie. (fields are: catID, catName)

When I select a categorie from the drop down list and fill all the other input fields, it saves all the input fields and only the catName from the categorie table into the tabel: event.
How can I also save the selected catID from the categorie table into the event table?

Can someone helm me out here?

Regards, 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>

Answer

You made a mistake in <option id="id"></option> if you wanna take value from select child.

you must change id to value="" or you must add value="id" proper.

change your select box block completely like below..

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

and a tip for mysql query... to the best of one's ability don't use "select * from" use it if you dont need to all colums better than "*"

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

PHP file

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