0

im making an insert function on php, everything is ok, there is no error but the data doesnt show up in the database, here is my php file

<?php 
include 'koneksi/koneksi.php';

if(isset($_POST['Submit'])) {
    $id_koperasi = $_POST['id'];
    $nama_koperasi = $_POST['nama'];
    $alamat = $_POST['alamat'];
    $telp = $_POST['telp'];
    $hp = $_POST['hp'];
    $nama_cp = $_POST['kontak'];
    $email = $_POST['email'];
    $nama_cp = $_POST['kontak'];
    $tanggal = $_POST['tgl'];
    $ket_fu = $_POST['ket'];
    $hasil_pembahasan = $_POST['hasil'];
    $status = $_POST['stat'];


    $query = "INSERT INTO t_koperasi(id,id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status) VALUES ('',
            '$id_koperasi',
            '$nama_koperasi',
            '$alamat',
            '$telp',
            '$hp',
            '$nama_cp',
            '$tanggal',
            '$ket_fu',
            '$hasil_pembahasan',
            '$status')"
    ;

    if (mysqli_query($con,$query)) {
        header("location:index.php");
    }else {
        error_log( "This code has errors!" );

    }
}

include 'views/v_form.php';

?>

and this is my database t_koperasi structure

enter image description here

  • Your code is open to [SQL injection](https://stackoverflow.com/q/332365/2469308) related attacks. Please learn to use [Prepared Statements](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Madhur Bhaiya Oct 07 '18 at 06:41
  • where do you create the `$con` object ? – Madhur Bhaiya Oct 07 '18 at 06:42
  • on the connection.php file –  Oct 07 '18 at 06:44
  • And how are you passing the $con object to this file. You need to pass it via some function parameter. – Madhur Bhaiya Oct 07 '18 at 06:45
  • using include 'koneksi/koneksi.php' –  Oct 07 '18 at 06:48
  • Objects donot get passed by just including a file. You need to wrap this into a class, and using proper functions to pass objects around. – Madhur Bhaiya Oct 07 '18 at 06:50
  • If `id` is an autoincrement field, it would normally be missed out of the INSERT to make it assign the next value. – Nigel Ren Oct 07 '18 at 06:51
  • i already used the including file method before and it works, i really didnt know what is happening –  Oct 07 '18 at 06:52
  • i tried delete the id on the php and it still doesnt work, if i dont put the id on the php file it will just give me error on the column –  Oct 07 '18 at 06:53
  • What error? It is useful to include some error text to see what is going wrong rather than just guessing. – Nigel Ren Oct 07 '18 at 06:54
  • Column count doesn't match value count at row 1 –  Oct 07 '18 at 06:55
  • that is what happening when i doesnt put the id on the php file on values –  Oct 07 '18 at 06:55
  • Did you also remove the value in `VALUES ('',` - this `''` value needs to be removed. – Nigel Ren Oct 07 '18 at 06:56
  • oh... i know what is happening, i forgot to put the email on php file, thank you guys –  Oct 07 '18 at 06:57

3 Answers3

2

The id is auto increasing, so you should remove id column in you insert sql. Also, the method you are using is dangerous, you should not totally trust what pass to you by other users in you website page, instead, you need to add filter functions.

yusher
  • 226
  • 1
  • 5
0

You Query Should Look Like this. At Least in postgresql and MySql

$query = "INSERT INTO t_koperasi(id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status) VALUES (
        '".$id_koperasi."',
        '".$nama_koperasi."',
        '".$alamat."',
        '".$telp."',
        '".$hp."',
        '".$nama_cp."',
        '".$tanggal."',
        '".$ket_fu."',
        '".$hasil_pembahasan."',
        '".$status."')"
;

You do not need to insert value into ID .Because ID expected to be the primary key with default value (auto increment).

Hope This works for you!

0

Simple remove id and ' ' . i hope work fine.

or

You just copy this code and past your project

<?php 
include 'koneksi/koneksi.php';

if(isset($_POST['Submit'])) {
$id_koperasi = $_POST['id'];
$nama_koperasi = $_POST['nama'];
$alamat = $_POST['alamat'];
$telp = $_POST['telp'];
$hp = $_POST['hp'];
$nama_cp = $_POST['kontak'];
$email = $_POST['email'];
$nama_cp = $_POST['kontak'];
$tanggal = $_POST['tgl'];
$ket_fu = $_POST['ket'];
$hasil_pembahasan = $_POST['hasil'];
$status = $_POST['stat'];


$query = "INSERT INTO t_koperasi(id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status) VALUES ('$id_koperasi',
        '$nama_koperasi',
        '$alamat',
        '$telp',
        '$hp',
        '$nama_cp',
        '$tanggal',
        '$ket_fu',
        '$hasil_pembahasan',
        '$status')"
;

if (mysqli_query($con,$query)) {
    header("location:index.php");
}else {
    error_log( "This code has errors!" );

}
}  include 'views/v_form.php';
proghasan
  • 425
  • 3
  • 20