-2

I need to display data from a form in another form based on selection list/dropdown.

I keep getting an error, so I've included the code:

if($result === FALSE) {
    die(mysql_error()); // Display a more meaningful error
}
while($row = mysql_fetch_array($result))
{
    echo $row['NamaPelanggan'];
    $row = mysql_fetch_array($sql);
    $KodPelanggan = $row['KodPelanggan'];
    $AlamatPelanggan = $row['AlamatPelanggan'];
    $Bandar = $row['Bandar'];
    $Negeri = $row['Negeri'];
    $NoTelefon = $row['NoTelefon'];
    $emel = $row['emel'];
}

But now, after I include that code, it shows Query was empty. What does it mean? The original code is

if(isset($_POST['Submit'])) {
    $sql = mysql_query ("INSERT INTO projek(idPel, nama_pegawai, jawatan_pegawai, notel_pegawai, hp_pegawai, nofaks_pegawai, emel_pegawai, kod_projek, norujukan, jenis_perolehan, tarikh_beli, harga_dokumen, kategori_projek, keterangan_projek, dok_proposal, status_projek, ada_invois, anggaran_kos, nilai_projek, kos_sebenar, margin_projek, info_plus, tarikh) 
    VALUES('$_POST[NamaPelanggan]','$_POST[nama_pegawai]','$_POST[jabatan_pegawai]','$_POST[notel_pegawai]','$_POST[hp_pegawai],'$_POST[nofaks_pegawai]','$_POST[emel_pegawai]','$_POST[kod_projek]','$_POST[norujukan]','$_POST[tarikh_beli]','$_POST[tarikh_tutup]','$_POST[harga_dokumen]','$_POST[kategori_projek]','$_POST[keterangan_projek]','$_POST[dok_proposal]','$_POST[status_projek]','$_POST[ada_invois]','$_POST[anggaran_kos]','$_POST[nilai_projek]','$_POST[kos_sebenar]','$_POST[margin_projek]','$_POST[info_plus]','$_POST[tarikh]')") or die(mysql_error());

    header('Location: borang_projek.php');
}
else if(isset($_POST['NamaPelanggan'])) {
    $NamaPelanggan=$_POST['NamaPelanggan'];
    $sql = mysql_query("SELECT * FROM pelanggan WHERE id='$NamaPelanggan'");
    $result = mysql_query($sql);
    if($result === FALSE) {
        die(mysql_error()); // Display a more meaningful error
    }
    while($row = mysql_fetch_array($result)) {
        echo $row['NamaPelanggan'];
        $row = mysql_fetch_array($sql);
        $KodPelanggan = $row['KodPelanggan'];
        $AlamatPelanggan = $row['AlamatPelanggan'];
        $Bandar = $row['Bandar'];
        $Negeri = $row['Negeri'];
        $NoTelefon = $row['NoTelefon'];
        $emel = $row['emel'];
    }
}
else {
    $NamaPelanggan="0";
    $KodPelanggan = "";
    $AlamatPelanggan = "";
    $Bandar = "";
    $Negeri = "";
    $NoTelefon = "";
    $emel = "";
}

How can I fix this?

Mike Precup
  • 4,028
  • 18
  • 39
shira
  • 1
  • 4
  • 3
    Show where you declare and assign `$sql` – John Conde Aug 07 '14 at 01:51
  • 1
    If you're a beginner then you have the chance to begin with PDO or mysqli right from the start. Leave those deprecated mysql_* functions alone, see the [red box](http://www.php.net/manual/en/function.mysql-query.php). Use prepared statements with placeholders and bind your input values to those. – VMai Aug 07 '14 at 01:53
  • 2
    Could you explain why you invoke `mysql_query` twice? Also, what is the point in declaring `$sql` variable followed by immediate redirect? – zerkms Aug 07 '14 at 01:54
  • I dont understand the use of PDO @VMai – shira Aug 07 '14 at 01:56
  • That's normal at the beginning. But it's useful to learn using PDO. Did you understand the use of the mysql_* functions before you used them the first time? – VMai Aug 07 '14 at 02:01
  • i think i not understand it 100%, that is my problem now. @VMai – shira Aug 07 '14 at 02:03
  • 1
    @shira Please take the time to read http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and you'll know, why it's good to do it right just from the beginning. I'm sure you will learn it. Many people have learned it the hard way. – VMai Aug 07 '14 at 02:08
  • @VMai MySQLi and PDO are the same things? – shira Aug 07 '14 at 02:13
  • No, they are different. MySQLi is MySQL only [API](http://en.wikipedia.org/wiki/API). PDO is an abstraction layer that can not only used with MySQL but with other databases too. – VMai Aug 07 '14 at 02:16
  • i should learn it by now. Thanks a lot @VMai – shira Aug 07 '14 at 02:22

2 Answers2

0

i saw a lot of error in this code..

$sql = mysql_query ("INSERT INTO projek(idPel, nama_pegawai, jawatan_pegawai, notel_pegawai, hp_pegawai, nofaks_pegawai, emel_pegawai, kod_projek, norujukan, jenis_perolehan, tarikh_beli, harga_dokumen, kategori_projek, keterangan_projek, dok_proposal, status_projek, ada_invois, anggaran_kos, nilai_projek, kos_sebenar, margin_projek, info_plus, tarikh)     

VALUES('$_POST[NamaPelanggan]','$_POST[nama_pegawai]','$_POST[jabatan_pegawai]','$_POST[notel_pegawai]','$_POST[hp_pegawai],'$_POST[nofaks_pegawai]','$_POST[emel_pegawai]','$_POST[kod_projek]','$_POST[norujukan]','$_POST[tarikh_beli]','$_POST[tarikh_tutup]','$_POST[harga_dokumen]','$_POST[kategori_projek]','$_POST[keterangan_projek]','$_POST[dok_proposal]','$_POST[status_projek]','$_POST[ada_invois]','$_POST[anggaran_kos]','$_POST[nilai_projek]','$_POST[kos_sebenar]','$_POST[margin_projek]','$_POST[info_plus]','$_POST[tarikh]')") or die(mysql_error());

it should be

$sql = mysql_query ("INSERT INTO `projek`(`idPel`, `nama_pegawai`, `jawatan_pegawai`, `notel_pegawai`, `hp_pegawai`, `nofaks_pegawai`, `emel_pegawai`, `kod_projek`, `norujukan`, `jenis_perolehan`, `tarikh_beli`, `harga_dokumen`, `kategori_projek`, `keterangan_projek`, `dok_proposal`, `status_projek`, `ada_invois`, `anggaran_kos`, `nilai_projek`, `kos_sebenar`, `margin_projek`, `info_plus`, `tarikh`)

in your values, the post name should inside the quote, you have incorrect systax, do this sample code

VALUES('".$_POST['NamaPelanggan']."','".$_POST['nama_pegawai']."'etc);
//the name of $_POST should inside of  quote..do you realize now what your error?

and in your else it should be

$sql = "SELECT * FROM pelanggan WHERE id='$NamaPelanggan'";
$result = mysql_query($sql);
CodeSlayer
  • 1,284
  • 1
  • 12
  • 34
-1

I think your problem is in the else if where you call mysql_query twice.

$sql = mysql_query("SELECT * FROM pelanggan WHERE id='$NamaPelanggan'");
$result = mysql_query($sql); 

The correct one should be:

$result = mysql_query("SELECT * FROM pelanggan WHERE id='$NamaPelanggan'");
Vincent
  • 545
  • 10
  • 22
  • Thanks, now it show "Unknown column 'id' in 'where clause'" . Can you help me to explain this? – shira Aug 07 '14 at 02:04
  • @shira That means your table pelanggan has no column with the name `id`. And it's a good idea to have the query in a separate variable. That's much easier to debug. And you're learning to debug right now. – VMai Aug 07 '14 at 02:10
  • I already replace `id` with `idPel` which is already in my table. But still cannot display the data from the database on the form – shira Aug 07 '14 at 02:23