Click here to Skip to main content
15,920,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm new for the php. I created a multiple forms when i click submit button show "submitted successfully".And I create multi query for insert data to different five tables.

when creating database connection I have displayed error as "undefined variable in...".After I fix that Problem I have displayed another error "undefined index in...".then I fixed it but again shows "undefined variable in..." error.

So I used following code for fix both undefined index and undefined variable error.but now when I submit data that tables are not update well. two tables are correctly filled.(contact_details table,extra_curricular_activity table).One table filled without two fields(academic_details table,Z_Score field and A_Grade1 field).other two tables are empty all fields.but add new row when the click submit button.(course details table,personal_details table)

That empty fields of the database and error fields are the same..so how can I fixed this

What I have tried:

this is the code i was use to fix both undefined index and undefined variable error

PHP
$profile_picture = isset( $_POST['profile_picture'] )? $_POST['profile_picture']: false;
$student_id = isset( $_POST['student_id'] )? $_POST['student_id']: false;
$admitted_date = isset( $_POST['admitted_date'] )? $_POST['admitted_date']: false;
$academic_year = isset( $_POST['academic_year'] )? $_POST['academic_year']: false;
$course = isset( $_POST['course'] )? $_POST['course']: false;
$first_name = isset( $_POST['first_name'] )? $_POST['first_name']: false;
$middle_name = isset( $_POST['middle_name'] )? $_POST['middle_name']: false;
$last_name = isset( $_POST['last_name'] )? $_POST['last_name']: false;
$dob = isset( $_POST['dob'] )? $_POST['dob']: false;
$gender = isset( $_POST['gender'] )? $_POST['gender']: false;
$nic = isset( $_POST['nic'] )? $_POST['nic']: false;
$nationality = isset( $_POST['nationality'] )? $_POST['nationality']: false;
$religion = isset( $_POST['religion'] )? $_POST['religion']: false;
$race = isset( $_POST['race'] )? $_POST['race']: false;
$a_grade1 = isset( $_POST['a_grade1'] )? $_POST['a_grade1']: false;
$z_score = isset( $_POST['z_score'] )? $_POST['z_score']: false;
Posted
Updated 31-May-18 23:37pm
v2

1 solution

You have to ensure that all variables which should be used for database access has been passed. If any required variable is missing, show an error message and exit (don't perform any database operations then).

Depending on your requirements it might be also necessary to check for empty variables or valid content.

Note also that your current code creates variables of different types: A boolean FALSE if the parameter does not exist and a string otherwise (which might be an empty string if the parameter is empty).

We can not tell you which paramaters are required and which are optional for the different tasks. Only you can know that. But for creating a new recordset most - if not all - are usually required while for showing existing recordsets only a few might be required.

If your form supports multiple operations (like add, view, delete), you have to check first for that (assuming here passed as 'operation') and then for the required parameters:
PHP
if (isset( $_POST['operation'] )) {
    if ($_POST['operation'] === 'view') {
        // If you can for example view recordset(s) by id or name 
        //  and have implemented functions for that:
        if (isset($_POST['student_id'])) {
            view_by_id();
        }
        else if (isset($_POST['first_name']) && isset($_POST['last_name'])) {
            view_by_name();
        }
    }
    else if ($_POST['operation'] === 'add')
    {
        // Call the add() function which checks for the required parameters.
        // That are usually all database fields
        //  (except the database ID when that is auto increment)
        add();
    }
} else {
    // Error message or print the input form here if that is intended when
    //  calling this without parameters
}
I have used functions in my above example because that makes the code better readable. Add the parameter checks on top of the functions. The functions may also return a status value.

For viewing by ID and deleting you need only the unique ID:
PHP
function delete()
{
    if (!isset($_POST['student_id'])) {
        // Error: Required ID parameter not present
    }
    else if ($_POST['student_id'] === '') {
        // Error: ID parameter is an empty string
    }
    else if (!is_numeric($_POST['student_id'])) {
        // Error: ID parameter is not a numeric value
    }
    else {
        // Can pass $_POST['student_id'] as SQL parameter for the ID field
        $id = intval($_POST['student_id']);
        //$query = 'DELETE FROM table WHERE student_id=' . $id . ';';
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900