jeudi 18 février 2021

How to get checkbox values insert into multiple rows?

My code is inserting the checkbox values into one row - i would like to have the code insert multiple rows depending on number of checkboxes checked. Can any one tell me what im doing wrong?

I have tried implode, explode - but haven't had any succes so far. Please can any one tell me what i'm doing wrong. Hope fulle by modifing my code bellow...? Thx

  <?php
    // Initialize the session
    session_start();

    // Include config file
    require_once "assets/scripts/config.php";

    $param_uniqid = $_SESSION['uniqid']; 
    $param_company = $_SESSION['company']; 
    $param_vat = $_SESSION['vat']; 
    $param_username = $_SESSION['username']; 
 
    // Check if the user is logged in, if not then redirect him to 
    login page
    if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== 
    true){
    header("location: login.php");
    exit;
    }

    // Define variables and initialize with empty values
    $l_comps = $status_request = $user = $car = $uniqid = $company 
    = $vat = $username = "";
    $l_comps_err = $status_request_err = $user_err = $car_err = 
    $uniqid_err = "";
 
    // Processing form data when form is submitted
    if($_SERVER["REQUEST_METHOD"] == "POST"){

    {
    $l_compsarr = $_POST['l_comps'];
    $l_comps = implode(", ",$l_compsarr);
    } 

    {
    $status_request = ($_POST["status_request"]);
    }   

    // Validate user
    $input_user = trim($_POST["user"]);
    if(empty($input_user)){
        $user_err = "Venligst indtast en bruger.";
    } elseif(!filter_var($input_user, FILTER_VALIDATE_REGEXP, 
    array("options"=>array("regexp"=>"/^[0-9a-åA-Å\s]+$/")))){
        $user_err = "Bruger er ikke korrekt.";
    } else{
        $user = $input_user;
    }

    // Validate car
    $input_car = trim($_POST["car"]);
    if(empty($input_car)){
        $car_err = "Venligst indtast bilinformationer.";
    } elseif(!filter_var($input_car, FILTER_VALIDATE_REGEXP, 
    array("options"=>array("regexp"=>"/^[0-9a-åA-Å+&@#\/%-? 
    =~_|!:,.;\s]+$/")))){
        $car_err = "Bil er ikke korrekt.";
    } else{
        $car = $input_car;
    }
    
    // Validate uniqid
    $input_uniqid = trim($_POST["uniqid"]);
    if(empty($input_uniqid)){
        $uniqid_err = "Venligst indtast uniqid.";
    } elseif(!filter_var($input_uniqid, FILTER_VALIDATE_REGEXP, 
     array("options"=>array("regexp"=>"/^[0-9a-åA-Å+&@#\/%-? 
    =~_|!:,.;\s]+$/")))){
        $uniqid_err = "Uniqid er ikke korrekt.";
    } else{
        $uniqid = $input_uniqid;
    }
    
    
    // Check input errors before inserting in database
    if(empty($l_comps_err) && empty($status_request_err) && 
    empty($user_err) && empty($car_err) && empty($uniqid_err)){
        $sql = "INSERT INTO offer_requests (l_comps, 
    status_request, user, car, uniqid, company, vat, username) 
     VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
         
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as 
    parameters
              mysqli_stmt_bind_param($stmt, "ssssssss", 
    $param_l_comps, $param_status_request, $param_user, 
    $param_car, $param_uniqid, $param_company, $param_vat, 
    $param_username);
            
            // Set parameters
    
            $param_l_comps = $l_comps;
            $param_status_request = $status_request;
            $param_user = $user;
            $param_car = $car;
            $param_uniqid = $uniqid;
    
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
            
                // Records created successfully. Redirect to 
     landing page
                header("location: /offer_requests");
                exit();
            } else{
                echo "Something went wrong. Please try again 
     later.";
            }
        }
    }
    }

    ?>
 
    <!DOCTYPE HTML>

    <html>
    <head>
        <title>Fleets - få op til 3 tilbud på jeres næste 
    leasingbil</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, 
    initial-scale=1" />
        <link rel="stylesheet"
        <link rel="stylesheet" href="assets/css/main.css" />
    </head>
    <body class="subpage">

        <!-- Header -->
            <header id="header">
                <div class="logo"><a href="index.html">Fleets.dk <span>3 tilbud på leasingbil</span></a></div>
                <a href="assets/scripts/logout.php" class="code">LOG UD</a>
            </header>

    <!-- content -->
            <div class="box">
                <div class="inner">
                    <div class="content">
                            <h4>Hej, <b><?php echo htmlspecialchars($_SESSION["name"]); ?></b>.</h4>

                        <hr />

                        <ul class="nav nav-tabs">
                          <li role="presentation"><a href="/welcome">Profil</a></li>
                          <li role="presentation"><a href="/cars_employees">Biler</a></li>
                          <li role="presentation" class="active"><a href="offer_requests">Tilbud</a></li>
                        </ul>
                            
                            <div class="table-wrapper">
                            <h4 class="pull-left">Oprettede tilbud</h4>
                            
                <table class="table table-striped">
                    <tbody>
                        <tr>
                            <th class="hidden"><b>#</b></th>
                            <th><b>Bil</b></th>
                            <th><b>Leasingselskab(er)</b></th>
                            <?php
                            // Include config file
                            require_once "assets/scripts/config.php";
                            
                            // Attempt select query execution
                             $sql = "SELECT * FROM offer_requests WHERE username = '" . ($_SESSION["username"]) . "'";
                            if($result = mysqli_query($link, $sql)){
                                if(mysqli_num_rows($result) > 0){

                                                echo "<th></th>";
                                            echo "</tr>";
                                        while($row = mysqli_fetch_array($result)){
                                            echo "<tr>";
                                                echo "<th class='hidden'>" . $row['uniqid'] . "</th>";
                                                echo "<th>" . $row['car'] . "</th>";
                                                echo "<th>" . $row['l_comps'] . "</th>";
                                                echo "<th>";
                                                    echo "<a href='read_request.php?uniqid=". $row['uniqid'] ."' title='Se tilbudsanmodning' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                                                echo "</th>";
                                            echo "</tr>";
                                        }
                                    // Free result set
                                    mysqli_free_result($result);
                                } else{
                                    echo "</br></br><p><b><i>Ingen informationer fundet.</i></b></p>";
                                }
                            } else{
                                echo "ERROR: Was not able to execute $sql. " . mysqli_error($link);
                            }
                                                 echo "</tbody>";                            
                                    echo "</table>";
                            ?>
                            <p>
                                <div href="" class="button alt small" onclick="hideCreate()">OPRET NYT TILBUD</div>
                            </p>
                        </div>
 
                        <div id="create">    
                            <h4>1 - udfyld formularen</h4>
                                <form action="" method="post">
                                        <div class="6u 12u$(xsmall) <?php echo (!empty($car_err)) ? 'has-error' : ''; ?>">
                                            <label>Bil</label>
                                            <input type="text" name="car" class="6u 12u$(xsmall)" value="<?php echo $car; ?>">
                                            <span class="help-block"><?php echo $car_err;?></span>
                                        </div>
                                        <div class="6u 12u$(xsmall) <?php echo (!empty($user_err)) ? 'has-error' : ''; ?>">
                                            <label>Bruger</label>
                                            <input type="text" name="user" class="6u 12u$(xsmall)" value="<?php echo $user; ?>">
                                            <span class="help-block"><?php echo $user_err;?></span>
                                        </div>
                                            <input type="hidden" name="company" value="<?php echo $company; ?>">
                                            <input type="hidden" name="vat" value="<?php echo $vat; ?>">
                                            <input type="hidden" name="username" value="<?php echo $username; ?>">
                                            <input type="hidden" name="uniqid" value="<?php echo uniqid(); ?>" />
                                            <input type="hidden" name="status_request" value="AFVENTER TILBUD" />
                                            </br>

                                            <h4 class="pull-left">2 - vælg op til tre leasingselskaber</h4></br></br>
                                                <div class="table-wrapper">
                                                    <table class="table table-striped">
                                                        <thead>
                                                            <tr>
                                                                <th class="hidden"></th>
                                                                <th>Vælg</th>                                             
                                                                    <?php
                                                                        // Include config file
                                                                        require_once "assets/scripts/config.php";
                                                                        // Attempt select query execution
                                                                         $sql = "SELECT * FROM l_comp";
                                                                        if($result = mysqli_query($link, $sql)){
                                                                            if(mysqli_num_rows($result) > 0){
                                                          echo "<th>Leasingselskab</th>";                    
                                                      echo "</tr>";
                                                  echo "</thead>";
                                                  echo "<tbody>";
                                                                        while($row = mysqli_fetch_array($result)){
                                                          echo "<tr>";
                                                              echo "<th>";
                                                                  echo "<div class='6u 12u$(xsmall) <?php echo (!empty(" . $l_comp_err . ")) ? 'has-error' : ''; ?> 
                                                                        <input type='checkbox' id='" . $row['l_company'] . "' name='l_comps[]' <?php echo (in_array('l_comps', $var)?'checked':''); value='" . $row['l_company'] . "'?>
                                                                        <label for='" . $row['l_company'] . "'></label>
                                                                        <span class='help-block'><?php echo " . $l_comp_err. ";?></span>";
                                                              echo "</th>";                                            
                                                              echo "<th>" . $row['l_company'] . "</th>";
                                                          echo "</tr>";}
                                                                        // Free result set
                                                                        mysqli_free_result($result);
                                                                        } else{
                                                                        echo "</br></br><p><b><i>Ingen informationer fundet.</i></b></p>";
                                                                        }
                                                                        } else{
                                                                        echo "ERROR: Was not able to execute $sql. " . mysqli_error($link);
                                                                        }
                                                                        echo "</tbody>";                            
                                              echo "</table>";
                                                                        ?>

                                                                <input type="submit" class="button alt small" value="OPRET NYT TILBUD">
                                                                <a href="/offer_requests" class="button alt small">FORTRYD</a>

                                </form>
                        </div>
                    </div>
                </div>
            </div>

        <!-- Footer -->
            <footer id="footer" class="wrapper">
                <div class="inner">
                    <div class="copyright">
                        &copy; Fleets.dk - for virksomheder </br> 
                        <a href="mailto: kontakt@fleets.dk">KONTAKT OS</a> 
                    </div>
                </div>
            </footer>

        <!-- Scripts -->
            <script src="assets/js/jquery.min.js"></script>
            <script src="assets/js/jquery.scrolly.min.js"></script>
            <script src="assets/js/jquery.scrollex.min.js"></script>
            <script src="assets/js/skel.min.js"></script>
            <script src="assets/js/util.js"></script>
            <script src="assets/js/main.js"></script>
            <script src="assets/js/overlay_create.js"></script>
            <script src="assets/js/overlay_login.js"></script>

    </body>
</html>



Aucun commentaire:

Enregistrer un commentaire