-1

i have this code

<?php

$host = "localhost";
$user = "root";
$pass = "";
$dabname = "tutorial";

$login = mysqli_connect($host, $user, $pass, $dabname) or die('Could not connect to mysql server.');
mysqli_select_db($login, $dabname) or die('Could not select database.');
$nama_karyawan = $_POST['nama_karyawan'];
$umur_karyawan = $_POST['umur_karyawan'];

function proses_hoby($id_karyawan) {
    if (isset($_POST["rincian_hoby"])) {
        $hoby = $_POST["rincian_hoby"];
        reset($hoby);
        while (list($key, $value) = each($hoby)) {
            $rincian_hoby = $_POST["rincian_hoby"][$key];
            $jenis_hoby = $_POST["jenis_hoby"][$key];
            $sql_hoby = "INSERT INTO tbl_hoby(rincian_hoby,jenis_hoby,id_karyawan)
                                              VALUES('$rincian_hoby','$jenis_hoby','$id_karyawan')";
            $hasil_hoby = mysqli_query($login, $sql_hoby);
        }
    }
}

$sql = "INSERT INTO tbl_karyawan(nama_karyawan,umur_karyawan)
                      VALUES('$nama_karyawan','$umur_karyawan')";
$hasil = mysqli_query($login, $sql);
$id_karyawan = mysqli_insert_id($login);
if ($hasil) {
    proses_hoby($id_karyawan);
    echo "Data berhasil diinput";
} else {
    echo "Data Gagal diinput";
}
?>

But i do get this error

Notice: Undefined variable: login in C:\xampp\htdocs\2\proses.php on line 23

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\2\proses.php on line 23

Notice: Undefined variable: login in C:\xampp\htdocs\2\proses.php on line 23

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\2\proses.php on line 23 Data berhasil diinput

I know there are lot of similiar Thread name but i dont found that match my problem

Here is the source code https://drive.google.com/file/d/16FNr5SJeO_53_-XUJo7RIV1rLqgZwagV/view?usp=sharing

please help

any help would be very appreciated

thank you

Rotimi
  • 4,783
  • 4
  • 18
  • 27
  • You have wrong query syntax $sql="INSERT INTO tbl_karyawan('nama_karyawan','umur_karyawan') VALUES($nama_karyawan,$umur_karyawan)"; Try with that – Waqas Ahmed Apr 17 '18 at 08:52
  • No Luck still got the error message like my first post – Tehnik Nusantara Jakarta Apr 17 '18 at 08:58
  • variable scope issue. pass `$login` as a parameter to your function. vote to close as typo – Rotimi Apr 17 '18 at 09:03
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Nigel Ren Apr 17 '18 at 09:04

1 Answers1

0

You should learn about variable scopes inside php. $login is declared outside the function proses_hoby so proses_hoby can't access this var. To access $login inside the function, you need to make it global (the unclean way..) or pass $login as an function parameter.

Modern approaches would be to move the whole thing inside a class and make $login a class var. So every member of the class could access the var.

Btw. your code is prone to sql injections. You should use prepared statements.

Philipp
  • 15,377
  • 4
  • 35
  • 52