0

Hello everyone, I still new in php and now get problem with my code the problem is it cannot save my insert-data into mysql even my connection is success, I don't why and what is wrong with that code, need your advice. Thanks

<?php
$sql_insert= "INSERT INTO pegawai (id_pegawai, nip, nm_pegawai, ttl_pegawai, gender_pegawai, agama_pegawai, alamat_pegawai, tgl_masuk, jenis_pekerjaan, status_pegawai, pangkat_pns, ijazah_terakhir,thn_lulus) VALUES ('$id_pegawai','$nip','$nm_pegawai','$new_ttl','$gender','$agama', $alamat','$masuk','$jenis_pekerjaan','$stts','$pangkat','$ijazah','$jurusan','$thn')";
mysql_query($kon, $sql_insert);
?>
miken32
  • 42,008
  • 16
  • 111
  • 154
christ_tp
  • 13
  • 6
  • 1
    If you're new, you should not be using functions like `mysql_query` that are 10 years old and unsupported. Use modern PDO database access. See http://stackoverflow.com/q/12859942/1255289 – miken32 Apr 04 '17 at 04:56
  • Use either PDO or mysqli. You don't show what error you are getting, but I did notice that you have 12 columns specified, and 13 values. They have to match. – Sloan Thrasher Apr 04 '17 at 04:58
  • Thank @miken32, the main problem I've tried make test.php script which have same problem the connection is success but it cannot save data, for connect mysql I'm using this code : – christ_tp Apr 05 '17 at 04:42

1 Answers1

1

As miker32 explained mysql_query has deprecated you can use mysqli_query instead and we are using prepare bind_baram so it's a little bit safer:

$kon= mysqli_connect("localhost","your_username","your_password","your_dbname");

if (!$kon) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql_insert= mysqli_prepare("INSERT INTO pegawai (id_pegawai, nip, nm_pegawai, ttl_pegawai, gender_pegawai, agama_pegawai, alamat_pegawai, tgl_masuk, jenis_pekerjaan, status_pegawai, pangkat_pns, ijazah_terakhir,thn_lulus) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");

mysqli_stmt_bind_param($sql_insert, 'issssssssssss', $id_pegawai,$nip,$nm_pegawai,$new_ttl,$gender,$agama, $alamat,$masuk,$jenis_pekerjaan,$stts,$pangkat,$ijazah,$jurusan,$thn);

mysqli_query($kon,$sql_insert);
Amr Aly
  • 3,871
  • 2
  • 16
  • 33