How can i solve this Undefined index:?

2019-09-30 08:18发布

问题:

How can I solve this error? I have to try anything, but the result is the same error. Please help me to solve this problem error

[My error][1]

And this is my Code in update.php:

    <?php
include_once 'config.php';

$employee_id=$_GET['employee_id'];
$name=$_POST['name'];
$date_of_birth=$_POST['date_of_birth'];
$gender=$_POST['gender'];
$marital_status=$_POST['marital_status'];
$nationality=$_POST['nationality'];
$present_address=$_POST['present_address'];
$city=$_POST['city'];
$country=$_POST['country'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$nip=$_POST['nip'];
$status=$_POST['status'];
$designation=$_POST['designation'];
$joining_date=$_POST['joining_date'];
$leaving_date=$_POST['leaving_date'];
$picture = basename($_FILES['picture']['name']);

if (!empty($_FILES['picture'])) {   
    $path = "admin/gambar/";
    $path = $path . basename($_FILES['picture']['name']);
    if (move_uploaded_file($_FILES['picture']['tmp_name'], $path)) {
        echo "The file " . basename($_FILES['picture']['name']) .
        " has been uploaded";
    } else {
        echo "There was an error uploading the file, please try again";
    }
}

$query = "UPDATE employee_list set name='$name', date_of_birth='$date_of_birth', gender='$gender', marital_status='$marital_status', nationality='$nationality', present_address='$present_address', city='$city', country='$country', phone='$phone', email='$email', nip=$nip, status='$status', designation='$designation', joining_date='$joining_date', leaving_date='$leaving_date', picture='$picture' where employee_id=$employee_id";
?>


Thank you :)

回答1:

You need to use isset() to avoid these errors. something like given below.

<?php
include_once 'config.php';
if (isset($_POST['employee_id'])) {

$employee_id=$_POST['employee_id'];
$name=$_POST['name'];
$date_of_birth=$_POST['date_of_birth'];
$gender=$_POST['gender'];
$marital_status=$_POST['marital_status'];
$nationality=$_POST['nationality'];
$present_address=$_POST['present_address'];
$city=$_POST['city'];
$country=$_POST['country'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$nip=$_POST['nip'];
$status=$_POST['status'];
$designation=$_POST['designation'];
$joining_date=$_POST['joining_date'];
$leaving_date=$_POST['leaving_date'];
$picture = basename($_FILES['picture']['name']);

if (!empty($_FILES['picture'])) {   
    $path = "admin/gambar/";
    $path = $path . basename($_FILES['picture']['name']);
    if (move_uploaded_file($_FILES['picture']['tmp_name'], $path)) {
        echo "The file " . basename($_FILES['picture']['name']) .
        " has been uploaded";
    } else {
        echo "There was an error uploading the file, please try again";
    }
}

$query = "UPDATE employee_list set name='$name', date_of_birth='$date_of_birth', gender='$gender', marital_status='$marital_status', nationality='$nationality', present_address='$present_address', city='$city', country='$country', phone='$phone', email='$email', nip=$nip, status='$status', designation='$designation', joining_date='$joining_date', leaving_date='$leaving_date', picture='$picture' where employee_id=$employee_id";
}
?>

Note:Use prepared query to avoid sql injection attack.



回答2:

put all your code from line #4 to end of the file in following if statement

if (!empty($_GET['employee_id'])) {

    // your code 
}

also, your code is not secure for SQL injection, XSS, CSRF attacks. You can use http://csrf.htmlpurifier.org/ library to protect your code against CSRF attacks.

Use PDO (http://php.net/manual/en/book.pdo.php) to protect SQL injection.