0

So I have a form that lets people add a list of grades, and using jQuery they are able to add up to 9 extra fields. (Meaning they can submit any number from 1 to 10 grades). What I want to know is how I can go about storing this in my database, as I do not want to convert them all into one string. My concern is that because I don't know how many grades the user is going to enter, I can't set a definitive array number to store (or can I?)

Sorry if this is not terribly well explained, I'm still relatively new to PHP and SQL!

3
  • As it is currently written it is unclear what you're asking. Can you add more information what you currently have (form, php script, database scheme, table relationships...)? Commented Mar 29, 2016 at 18:47
  • 3
    Grades should be one table linked to the owner of each grade. You can have as many rows in the grade table as you need with three columns - student identifier, course and grade. Commented Mar 29, 2016 at 18:49
  • So I have a 'grades' table which relates to a CV table, and the CV table relates to a user table. Meaning many grades within one CV/resume, and many CV/resumes for one user. For example they currently upload something like Maths, A, GCSE as one entry and English, A*, A Level as another. Commented Mar 29, 2016 at 18:56

1 Answer 1

1

A quick assumption if I am not mistaken would be: saving one user with multiple grades for multiple subjects can be achieved like this.

Firstly we get one user id from the form and put it in PHP:

$id = isset($_POST['id'])? $_POST['id']:'';

Then get multiple grades and subjects which would be sent as comma separated values:

//$id= explode(',',$_POST['id']);// For multiple users
$grade= explode(',',$_POST['grade']);
$subj= explode(',',$_POST['subject']);
$entry= explode(',',$_POST['entry']);

Now count the number of grades: $count= count($grade);

Use the count in a for loop to have insert in loop:

for ($i = 0; $i < $count; $i++) {
   try {
        $dbh->beginTransaction();   //$dbh is your PDO connection               
            $insertQ = "INSERT INTO `grades` (id, grade, subject, entry) 
                        VALUES('$id', '$grade[$i]', '$subj[$i]','$entry[$i])";             
            $dbh->query($insertQ);              
            $dbh->commit();
            } catch (Exception $e) {
                $error = $e->getMessage();

            }
}

Hope this may help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.