0

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);?>
4
Contributors
8
Replies
33
Views
4 Years
Discussion Span
Last Post by diafol
0

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

Then data will be stored in database.

0
    '$_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.

0

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..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.