Hello,

i need help here...., i try to insert my data into mysql, but every time when i insert data into my database, its insert two record in one time, 1 row is the data that i insert and another row is empty, just the auto increment column have filled, can someone help me??

this is my code for php and sql:

      <?php
      function date2mysql($year, $month, $day) {return checkdate($month, $day, $year) ? "$year-$month-$day" :false;}
        $date = date2mysql($_POST['year'], $_POST['month'], $_POST['day']);
        if (!$date) {$error = 'Invalid date';}

       $con=mysqli_connect("localhost","hazuan","hazuan","e-maintenance");
        // Check connection
        if (mysqli_connect_errno()){echo "Failed to connect to MySQL: " . mysqli_connect_error();}

        $sql="INSERT INTO aduan (Jenis_Aset, Keterangan_Aset, No_Siri_Pendaftaran, Pengguna_Terakhir, Tarikh_Kerosakan, Perihal_Kerosakan, Nama, Ic, Syor_Pegawai_Aset, Lokasi)VALUES('$_POST[Jenis]','$_POST[Keterangan]','$_POST[Pendaftaran]','$_POST[Pengguna]','$date','$_POST[Perihal]','$_POST[Nama]','$_POST[Ic]','$_POST[Bahagian]','$_POST[Lokasi2]')";

        if (!mysqli_query($con,$sql)){die('Error: ' . mysqli_error($con));}
        echo "1 record added";
        mysqli_close($con);?>

Recommended Answers

Perhaps the insert is called when there is no posted form data. Check your logic.

Jump to Post
    '$_POST[Jenis]','$_POST[Keterangan]','$_POST[Pendaftaran]','$_POST[Pengguna]','$date','$_POST[Perihal]','$_POST[Nama]','$_POST[Ic]','$_POST[Bahagian]','$_POST[Lokasi2]'

You can't do this. You either need to concatenate your array vars into a string with (.) or use braces:

'{$_POST[Jenis]}','{$_POST[Keterangan]}','{$_POST[Pendaftaran]}','{$_POST[Pengguna]}','$date','{$_POST[Perihal]}','{$_POST[Nama]}','{$_POST[Ic]}','{$_POST[Bahagian]}','{$_POST[Lokasi2]}'

But even this is bad as you haven't cleaned your vars- you're open to sql injections. mysqli has parametised queries, which …

Jump to Post

All 8 Replies

1356a8c2bb573284ae40889b1789dbcb

This post has no text-based content.

Perhaps the insert is called when there is no posted form data. Check your logic.

What do you mean pritaeas??

after define query, you must execute that query.
use:
mysql_query($sql) or
die(mysql_error());

Then data will be stored in database.

also check array: $_POST
when submit it empty or not empty

Member Avatar
    '$_POST[Jenis]','$_POST[Keterangan]','$_POST[Pendaftaran]','$_POST[Pengguna]','$date','$_POST[Perihal]','$_POST[Nama]','$_POST[Ic]','$_POST[Bahagian]','$_POST[Lokasi2]'

You can't do this. You either need to concatenate your array vars into a string with (.) or use braces:

'{$_POST[Jenis]}','{$_POST[Keterangan]}','{$_POST[Pendaftaran]}','{$_POST[Pengguna]}','$date','{$_POST[Perihal]}','{$_POST[Nama]}','{$_POST[Ic]}','{$_POST[Bahagian]}','{$_POST[Lokasi2]}'

But even this is bad as you haven't cleaned your vars- you're open to sql injections. mysqli has parametised queries, which allows you to get away with escaping - have a look.

i already found the problem, my sql code run everytime page is loaded, thats why 1 row empty, because after page is loaded, the form is empty, so i try to make 'if else' like this for sql connection,

if ($_POST['Pendaftaran']) {$con=mysqli_connect("localhost","hazuan","hazuan","e-maintenance");}

that's mean, after this $_POST['Pendaftaran'] is submit and already have data, the connection will run.
Thank You friend, for helping me..

Member Avatar

OK, is this solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.