mardi 4 août 2020

Storing multiple checkboxes value into php mysqli

Sorry if what I'll be asking is a dumb question but I have been reading and trying the available codes but none of them is working. Here is the link to where I learn about storing multiple checkboxes value

  1. How to insert multiple checkboxes value
  2. Add multiple checkboxes value in db
  3. Mysqli-PHP stores multiple checkboxes value into one record

I have a form with multiple checkboxes and I need to store the value into the database. My problem right now is that it's keep popping this error

Parse error: syntax error, unexpected '$chk' (T_VARIABLE), expecting ')'

below is my code

notification_form.php

<div class="form-group row">
  <label class="col-md-2 col-form-label">Potential Escalation: </label>
  <div class="col-md-5">
    <input type="checkbox" id="potential_escalation[]" value="Under control with available resources. No potential of escalation" />&nbsp;<text>Under control with available resources. No potential of escalation.</text>
  </div>
  <div class="col-md-5">
    <input type="checkbox" id="potential_escalation[]" value="May requires additional resources (e.g. authorities, contractors, mutual aid groups)." />&nbsp;<text>May requires additional resources (e.g. authorities, contractors, mutual aid groups).</text>
  </div>
</div>

<div class="form-group row">
  <label class="col-md-2 col-form-label"></label>
  <div class="col-md-5">
    <input type="checkbox" id="potential_escalation[]" value="Authorities may take over command and control" />&nbsp;<text>Authorities may take over command and control.</text>
  </div>
  <div class="col-md-5">
    <input type="checkbox" id="potential_escalation[]" value="May triggers significants authorities/ public community/ media interest." />&nbsp;<text>May triggers significants authorities/ public community/ media interest.</text>
  </div>

</div>

and here is the add_notification.php

<?php

session_start();

if(empty ($_SESSION['user'])){
    header ("Location:../index.php");
    exit();
}

require_once("../db.php");

if(isset ($_POST)){

$username= $_SESSION['user']['username'];
$incident_title = mysqli_real_escape_string($conn, $_POST['incident_title']);
$incident_description = mysqli_real_escape_string($conn, $_POST['incident_description']);
$incident_owner = mysqli_real_escape_string($conn, $_POST['incident_owner']);
$witness = mysqli_real_escape_string($conn, $_POST['witness']);
$incident_date = mysqli_real_escape_string($conn, $_POST['incident_date']);
$incident_year =  mysqli_real_escape_string($conn, $_POST['incident_year']);
$incident_month = mysqli_real_escape_string($conn, $_POST['incident_month']);
$monsoon = mysqli_real_escape_string($conn, $_POST['monsoon']);
$holiday = mysqli_real_escape_string($conn, $_POST['holiday']);
$incident_time = mysqli_real_escape_string($conn, $_POST['incident_time']);
$time_hourly = mysqli_real_escape_string($conn, $_POST['time_hourly']);
$time_category = mysqli_real_escape_string($conn, $_POST['time_category']);
$incident_location = mysqli_real_escape_string($conn, $_POST['incident_location']);
$checkbox1 = mysqli_real_escape_string($conn, $_POST['potential_escalation']
    $chk="";  
    foreach($checkbox1 as $chk1)  
       {  
          $chk.= $chk1.",";  
       }  );
$latitude = mysqli_real_escape_string($conn, $_POST['latitude']);
$longitude = mysqli_real_escape_string($conn, $_POST['longitude']);
$incident_worksite = mysqli_real_escape_string($conn, $_POST['incident_worksite']);
$country = mysqli_real_escape_string($conn, $_POST['country']);
$state = mysqli_real_escape_string($conn, $_POST['state']);  
$onshoreoffshore = mysqli_real_escape_string($conn, $_POST['onshoreoffshore']);
$vessel = mysqli_real_escape_string($conn, $_POST['vessel']);
$incident_type = mysqli_real_escape_string($conn, $_POST['incident_type']);
$incident_category = mysqli_real_escape_string($conn, $_POST['incident_category']);
$accident_type = mysqli_real_escape_string($conn, $_POST['accident_type']);
$day_lost_time = mysqli_real_escape_string($conn, $_POST['daylosttime']);
$estimated_cost = mysqli_real_escape_string($conn, $_POST['estimatedcost']);
$actual_cost = mysqli_real_escape_string($conn, $_POST['actualcost']);
$spill_vol = mysqli_real_escape_string($conn, $_POST['spillvolume']);
$production_loss = mysqli_real_escape_string($conn, $_POST['productionloss']);
if($_POST['productionunit']=="OTHER")
    {
        $production_unit = mysqli_real_escape_string($conn, $_POST['productionunit2a']);
    }
    else
    {
        $production_unit = mysqli_real_escape_string($conn, $_POST['productionunit']);
    }
$downtime = mysqli_real_escape_string($conn, $_POST['downtime']);
$impact_to = mysqli_real_escape_string($conn, $_POST['impactto']);
$sector = mysqli_real_escape_string($conn, $_POST['sector']);
$department = mysqli_real_escape_string($conn, $_POST['department']);
$business_group = mysqli_real_escape_string($conn, $_POST['business_group']);  
$business_section = mysqli_real_escape_string($conn, $_POST['business_section']);
$opus = mysqli_real_escape_string($conn, $_POST['opus']);  
$domestic_international = mysqli_real_escape_string($conn, $_POST['project_name']);
$project_name = mysqli_real_escape_string($conn, $_POST['project_name']);
$project_phase = mysqli_real_escape_string($conn, $_POST['project_phase']);
$project_start_date = mysqli_real_escape_string($conn, $_POST['project_start_date']);
$project_end_date= mysqli_real_escape_string($conn, $_POST['project_end_date']);
$staff_status = mysqli_real_escape_string($conn, $_POST['staff_status']);

if($_POST['project_type']=="ifothers")
    {
        $project_type = mysqli_real_escape_string($conn, $_POST['project_type2']);
    }
    else
    {
        $project_type = mysqli_real_escape_string($conn, $_POST['project_type']);
    }

if($_POST['contractor']=="ifothers")
    {
        $contractor = mysqli_real_escape_string($conn, $_POST['contractor2']);
    }
    else
    {
        $contractor = mysqli_real_escape_string($conn, $_POST['contractor']);
    }

$sub_contractor = mysqli_real_escape_string($conn, $_POST['sub_contractor']);
$name = mysqli_real_escape_string($conn, $_POST['injuriousname']);
$nationality = mysqli_real_escape_string($conn, $_POST['nationality']);
$age_number = mysqli_real_escape_string($conn, $_POST['age']);
$age_range = mysqli_real_escape_string($conn, $_POST['agerange']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);
$work_trade = mysqli_real_escape_string($conn, $_POST['worktrades']);
$years_of_experience = mysqli_real_escape_string($conn, $_POST['yearsexp']);
$imm_supervisor_name = mysqli_real_escape_string($conn, $_POST['supervisor']);
$desc_of_injury = mysqli_real_escape_string($conn, $_POST['injuries_description']);
$body_part = mysqli_real_escape_string($conn, $_POST['bodypart']);
$body_part_group = mysqli_real_escape_string($conn, $_POST['bodypartgroup']);
$hours_in_shift = mysqli_real_escape_string($conn, $_POST['hoursinshift']);
$days_in_tour = mysqli_real_escape_string($conn, $_POST['daysintour']);
$immediateaction = mysqli_real_escape_string($conn, $_POST['immediateaction']);
$person_responsible = mysqli_real_escape_string($conn, $_POST['person_responsible']);
$completion_date = mysqli_real_escape_string($conn, $_POST['completion_date']);
$report_status = mysqli_real_escape_string($conn, $_POST['report_status']);
$status ='Notification';

$stmt = $conn->prepare("INSERT INTO iir_incidentmain(username, incident_title, incident_description, incident_owner, witness, incident_date, incident_year,
    incident_month, monsoon, holiday, incident_time, time_hourly, time_category, incident_location, potential_escalation, latitude, longitude, incident_worksite, country, state, onshoreoffshore, vessel, incident_type, incident_category, accident_type, day_lost_time, estimated_cost, actual_cost, spill_voll, production_loss, production_unit, downtime, impact_to, sector, department, business_group, business_section, 
    opus, domestic_international, project_name, project_phase, project_start_date, project_end_date, project_type, contractor, sub_contractor, name,nationality, age_number, age_range, gender, work_trade, years_of_experience, imm_supervisor_name, desc_of_injury, body_part, body_part_group, hours_in_shift, days_in_tour,immediateaction, person_responsible, completion_date, report_status, status, staff_status)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

    $stmt->bind_param('ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss', $username, $incident_title, $incident_description,$incident_owner, $witness, $incident_date, $incident_year,
    $incident_month, $monsoon, $holiday, $incident_time, $time_hourly, $time_category, $incident_location, $chk, $latitude, $longitude, $incident_worksite, $country, $state, $onshoreoffshore, $vessel, $incident_type, $incident_category, $accident_type, $day_lost_time, $estimated_cost, $actual_cost, $spill_voll, $production_loss, $production_unit, $downtime, $impact_to, $sector, $department, $business_group, $business_section, $opus, $domestic_international, $project_name, $project_phase, $project_start_date, $project_end_date, $project_type, $contractor, $sub_contractor, $name, $nationality, $age_number, $age_range, $gender, 
    $work_trade, $years_of_experience, $imm_supervisor_name, $desc_of_injury, $body_part, $body_part_group, $hours_in_shift, $days_in_tour,immediateaction, person_responsible, completion_date, report_status, status, staff_status);
    
    if($stmt->execute()){
        
        $_SESSION['AddPostSuccess']=true;
        header("Location:notification_list.php");
        exit();     
    }else {
        
        echo "Error". "<br>". $conn->error;
    }

    $stmt->close();
    
    
}else {
    header("Location:notification_form.php");
    
    exit();
}

Note that I have many more input but I just provide a chunck of my form code which shows the checkbox which I have problems with.

Thank you in advance for the guidance and help.




Aucun commentaire:

Enregistrer un commentaire