Mark C
Mark C

Reputation: 41

I can't pass ang string value into array

I have this code but I can't pass the value of array variable $component which has the input data of string. What should I do?

<?php if(isset($_POST['submit_tbl_grade'])){
                    $id = isset($_POST['id']) ? $_POST['id'] : array();
                        $component = $_POST['component'];
                        $sub_component_number = $_POST['sub_component_number'];
                        $subject = $_POST['subject'];
                        $score = $_POST['score'];

                    for($i = 0 ; $i < count($id) ; $i++){
                        $record = "INSERT INTO tbl_grade(stud_id,subject_id,component,sub_component_number,component_value) VALUES ($id[$i],$subject[$i],$component[$i],$sub_component_number[$i],$score[$i])";
                        mysqli_query($db,$record);
                    }
                   } ?>

Upvotes: 2

Views: 52

Answers (1)

Gasol
Gasol

Reputation: 2537

Please read How do I create arrays in HTML forms and print debugging to check your $_POST data first to ensure the structure of data is be known to you.

There are also two problems in the sample code:

  1. Filter input and escape output

    You must escape input data to prevent SQL Injection attacks. Check out the answer of https://stackoverflow.com/a/60496/251311 commented by @zerkms

  2. The inserting values must be proper format.

    for example, If tbl_grade.compoment is defined as `varchar, You must enclose values with a quote, Check the following example.

    INSERT INTO `tbl_grade` ( `component` ) VALUES ("hello world");
    

Upvotes: 1

Related Questions