Selected option couldn't store in MySQL

Below code couldn't prompt the alert and also insert the selected option in database. I've tried to change the code but still doesn't works. Please do let me know on which part is the one I did mistake in this issue, it will be really helpful to proceed. Thank you for your time.

Code to store the selected option in database

<?php
session_start(); 
require('../setting/config.php');

$query ="SELECT * FROM marketing_data";
$results = mysqli_query($conn,$query);
?>

<?php

    if(isset($_POST['update'])){
            $sem = $_POST['sem'];
            $program = $_POST['program'];
            $query = "INSERT INTO `update_current_sem` (`id`, `sem`, `program`) VALUES (NULL, '$sem', '$program')";
        if(performQuery($query)){
                echo "<script>alert('Current sem has been updated')</script>";

            }else{
                echo "<script>alert('Unknown error occured.')</script>";
            }
        }        
?>

Body

   <select name="sem" id="sem" class="demoInputBox"      onChange="getProgram(this.value);">
        <option value="">Select Semester</option>
            <?php   
            $sql = "SELECT * FROM marketing_data";
            $res = mysqli_query($conn, $sql);
            while($row = mysqli_fetch_assoc($res)) {

                if($bul[$row['semester']] != true && $row['semester'] != 'semester'){
                ?>
                    <option value="<?php echo $row['semester']; ?>"><?php echo   $row['semester']; ?></option>
            <?php   
                    $bul[$row['semester']] = true;
                }
            } 
            ?>    
        </select>

    <select name="program" id="program" class="demoInputBox">
        <option value="">Select Program</option>
    </select>   
<div class="submit"><input type="submit" value="Update" name="update"></div>

getProgram.php

   <?php
     require('../setting/config.php');
        if(!empty($_REQUEST["sem_id"])) {

        $query ="SELECT * FROM marketing_data WHERE Semester=" . "'" . mysqli_escape_string($conn, $_POST["sem_id"] ) ."'";
        $result = mysqli_query($conn, $query);
    ?>
        <option value="">Select Program</option>

    <?php
        while($row2=mysqli_fetch_assoc($result)){
            if($bul2[$row2['student_prg']] != true && $row2['student_prg'] != 'student_prg')        { ?>
                <option value="<?php echo $row2['student_prg']; ?>"><?php echo     $row2['student_prg']; ?>  </option>
     <?php  
             $bul2[$row2['student_prg']] = true;
             }
         }
        }
    ?>

config.php

<?php 

define('DBINFO','mysql:host=localhost;dbname=marketing_system');
define('DBUSER','root');
define('DBPASS','');

$conn = mysqli_connect("localhost","root","","marketing_system");


function performQuery($query){
        $con = new PDO(DBINFO,DBUSER,DBPASS);
        $stmt = $con->prepare($query);
        $result = $stmt->fetchAll();
        if($stmt->execute()){
            return true;
        }else{
            return false;
        }
    }

    function fetchAll($query){
        $con = new PDO(DBINFO, DBUSER, DBPASS);
        $stmt = $con->query($query);
        return $stmt->fetchAll();
    }

?>

update_current_sem table structure

1 answer

  • answered 2020-06-06 16:07 Code wizard

    <?php
    session_start(); 
    include_once('config.php');
    $query ="SELECT * FROM marketing_data";
    $results = mysqli_query($conn,$query);
    ?>
    
    <?php
    
        if(isset($_POST['update'])){
                $sem = $_POST['sem'];
                $program = $_POST['program'];
                $val = "INSERT INTO update_current_sem(id, sem, program) VALUES (NULL, '$sem', '$program')";
            if($conn->query($val)===TRUE){
                    echo "<script>alert('Current sem has been updated')</script>";
    
                }else{
                    echo "<script>alert('Unknown error occured.')</script>";
                }
            }        
    ?>