Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

PHP Query

Hello,

This is PHP.

Help me understand this code:

if($selectDB == true){
    $query = "INSERT INTO recruitment 
            (ID, nama, jenis_kelamin, tanggal_lahir, nohp, email, alamat, provinsi, kota, zip, status_nikah, jumlah_tanggungan, agama, file_attachment, cover_letter)
          VALUES (NULL, '$nama', '$jenis_kelamin', '$tanggal_lahir', '$nohp', '$email', '$alamat', '$provinsi', '$kota', '$zip', '$status_nikah','$jumlah_tanggungan','$agama','$file_attachment', '$cover_letter');";

        // "INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
        // VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');";


    $simpan = mysqli_query($connect, $query);
    echo "save - ".$simpan;

    if(!$simpan){
        echo 'failed';
    }else{
            echo 'success';
    }
}

I wonder why $simpan = '' ?

1simpan - failed?

What did I do wrong?

0 likes
8 replies
lostdreamer_nl's avatar

soo many things wrong here.... (no offence).

"INSERT INTO recruitment 
            (ID, nama, jenis_kelamin, tanggal_lahir, nohp, email, alamat, provinsi, kota, zip, status_nikah, jumlah_tanggungan, agama, file_attachment, cover_letter)
          VALUES (NULL, '$nama', '$jenis_kelamin', '$tanggal_lahir', '$nohp', '$email', '$alamat', '$provinsi', '$kota', '$zip', '$status_nikah','$jumlah_tanggungan','$agama','$file_attachment', '$cover_letter');";

This is full of mysql injection possibilities....

$simpan = mysqli_query($connect, $query);

This is returning false, but you cannot know why unless you check for the error:

$simpan = mysqli_query($connect, $query) or die(mysqli_error($connect));

That should show you the error, probably a non existing or misnamed field.

If you're going to be using plain mysqli_query, you should really validate / cleanup and escape your variables.

davy_yg's avatar
Level 27

Then I get this error:

1

Notice: Undefined variable: nama in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: jenis_kelamin in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: tanggal_lahir in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: nohp in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: email in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: alamat in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: provinsi in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: kota in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: zip in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: status_nikah in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: jumlah_tanggungan in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: agama in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: file_attachment in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: cover_letter in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

simpan - gagal 

I wonder why undefined variable since the variables are the columns name in the database.

davy_yg's avatar
Level 27

I still wonder where I get the undefined variable?

LiamHammett's avatar

Because you don't have those variables defined in your PHP file? Are you wanting to insert the raw values "$nama" and so on, or interpolate a variable into your query?

lostdreamer_nl's avatar

I think it's time to go through the defaults of PHP first ;) to learn about variables

Then some basic query examples:

After that some basic "How not to let bad people hack my server via basic sql injection"

And somewhere along the line, start learning a framework (laravel)....

3 likes
davy_yg's avatar
Level 27

I cannot understand why select.php is being processed.

form.php

<form method="post" action="select.php">

When I load form.php the error appears.

Notice: Undefined variable: nama in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: jenis_kelamin in C:\xampp\htdocs\website_ids\IDS\select.php on line 49

Notice: Undefined variable: tanggal_lahir in C:\xampp\htdocs\website_ids\IDS\select.php on line 49
lostdreamer_nl's avatar

Aah I get it now....

You are coming from a VERY old version of PHP, where GLOBALS were still a thing.....

Change all your variables :

$nama => '$_POST['nama']
$jenis_kelamin => , $_POST['jenis_kelamin']
// etc.

Please or to participate in this conversation.