Home > database >  PHP Dropdown - Inserting Records from Multi Dependent Dropdown to MYSQL
PHP Dropdown - Inserting Records from Multi Dependent Dropdown to MYSQL

Time:11-25

Good Day! StackOverFlow Community I have the below-mentioned code where Multi-Dependent Dropdown shows up with data. When I try to insert in MYSQL DB, only ID is inserted not the name whereas I am inserting the name I surely am making mistake somewhere, need your precious guidance

Thanks

<?php
require_once ("DBController.php");
$db_handle = new DBController();
$query = "SELECT * FROM categories";
$countryResult = $db_handle->runQuery($query);
?>
<html>
<head>
<TITLE>Dynamically Load Dependent Dropdown on Multi-Select using PHP and
    jQuery</TITLE>


<head>
<style>
body {
    width: 610px;
    font-family: calibri;
}

.frmDronpDown {
    border: 1px solid #7ddaff;
    background-color: #C8EEFD;
    margin: 2px 0px;
    padding: 40px;
    border-radius: 4px;
}

.demoInputBox {
    padding: 10px;
    border: #bdbdbd 1px solid;
    border-radius: 4px;
    background-color: #FFF;
    width: 50%;
}

.row {
    padding-bottom: 15px;
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"
    type="text/javascript"></script>
<script>
function getState() {
        var str='';
        var val=document.getElementById('country-list');
        for (i=0;i< val.length;i  ) { 
            if(val[i].selected){
                str  = val[i].value   ','; 
            }
        }         
        var str=str.slice(0,str.length -1);
        
    $.ajax({          
            type: "GET",
            url: "get_state.php",
            data:'country_id=' str,
            success: function(data){
                $("#state-list").html(data);
            }
    });
}
</script>
</head>
<body>
<div class="form-group" style="font-family:arial;">
                    <label for="landnumber">Land Number:</label><b><label style="float:right;">رمز الأرض</b></label>
                    <select dir="rtl" lang="ar" id='landnumber' name='landnumber' class="form-control" required>
                                <option disabled selected>-- رمز الأرض --</option>
                    
                    <?php
        include "connect.php";  // Using database connection file here
        $records = mysqli_query($db, "SELECT landnumber From landnumbers");  // Use select query here 

        while($data = mysqli_fetch_array($records))
        {
            echo "<option value='". $data['landnumber'] ."'>" .$data['landnumber'] ."</option>";  // displaying data in option menu
        }   
    ?>  
</select>

                    </div>
                    <br />
                    <form method="post" action="">
    <div class="frmDronpDown">
        <div class="row">
            <label>Categories - التصنيفات </label><br /> <select name="country[]"
                id="country-list" class="demoInputBox"
                onChange="getState();" multiple size=4>
                <option value="">Select Categories</option>
<?php
foreach ($countryResult as $country) {
    ?>
<option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>
<?php
}
?>
</select>
        </div>
        <div class="row">
            <label>Activities - الأنشطة</label><br /> <select name="state[]"
                id="state-list" class="demoInputBox" multiple size=5>
                <option value="">الأنشطة</option>
            </select>
        </div>
          <input type="submit" name="submit" id="action" class="btn btn-info" value="Insert" />
        </form>
    </div>
</body>
</html>

<?php
   if(isset($_POST['submit'])){
require('connect.php');
        $state_string = implode(', ', $_POST['country']);
        $sql = '
            INSERT INTO
                `chosen_activities` (
                 `name`
                )
            VALUES (
                "'. $state_string .'"
            )';
        mysqli_query($db,$sql);
    }
?>

This below line of code inserts only ID not the name, I want to insert name

 <option value="<?php echo $country["id"]; ?>"><?php echo $country["name"]; ?></option>

Thanks

CodePudding user response:

In a select input, the value of the selected option is submitted to the backend on form submit.

So if you want to submit the name instead of id you just have to change the values of the options.

<option value="<?php echo $country["name"]; ?>"><?php echo $country["name"]; ?></option>

This will submit the name.

CodePudding user response:

From your information, I get that you are trying to insert a name instead of an ID, I hope you are using ID as int in mysql corresponding table, Here you have to know Int only allow numbers to insert, So you have to create a separate column for name field or you have to change the data type of the Id column to allow insert.

For good Practices your multi-dependent dropdown keeps id as foreign key

CodePudding user response:

change the value in $country['name']

 <option value="<?php echo $country["name"]; ?>"><?php echo $country["name"]; ?></option>
  • Related