-2

After searching for problems identical to mine and found no solution I decided to ask here.

I have 2 tables in 1 db, one is "produk" the other is "sparepart" both have the exact same number and name of columns.

I wrote the exact same code of HTML and PHP, just changed the table name so the data will be stored to their respective table.

the problem is the data from produk.php is stored in produk table but, the data from sparepart.php is not stored in sparepart table after query.

I tried checking for errors, but no error generated and instead it echoes "success".

please take a look at my simplified code :

HTML

<?php echo "<header><h3>TAMBAHKAN PRODUK SPAREPART</h3></header>
  <form method=POST action='$aksi?module=sparepart&act=input' enctype='multipart/form-data'>
    <div class='module_content'>
      <table id='rounded-corner'>
        <tr>
          <td width=70>Nama Produk</td>     
          <td> : <input type=text name='nama_produk' size=60></td>
        </tr>
        <tr>
          <td>Kategori</td>  
          <td> : 
            <select name='kategori'>
              <option value=0 selected>- Pilih Kategori -</option>";
              $tampil=mysql_query("SELECT * FROM kategori ORDER BY nama_kategori");
              while($r=mysql_fetch_array($tampil)){
                echo "<option value=$r[id_kategori]>$r[nama_kategori]</option>";
              }

            echo "</select>
          </td>
        </tr>
        <tr>
          <td>Kategori 2</td>  
          <td> : 
            <select name='kategori2'>
              <option value=0 selected>- Pilih Kategori2 -</option>";
              $tampil2=mysql_query("SELECT * FROM kategori2 ORDER BY nama_kategori2");
              while($r2=mysql_fetch_array($tampil2)){
                echo "<option value=$r2[id_kategori2]>$r2[nama_kategori2]</option>";
              }
            echo "</select>
          </td>
        </tr>
        <tr>
          <td>Harga </td>     
          <td> : <input type=text name='harga' size=10></td>
        </tr>
        <tr>
          <td>Stok</td>     
          <td> : <input type=text name='stok' size=5></td>
        </tr>
        <tr>
          <td>Diskon</td>     
          <td> : <input type=text name='diskon' size=5></td>
        </tr>
        <tr>
          <td>Potongan</td>     
          <td> : <input type=tetxt name='potongan' size=10></td>
        </tr>
        <tr>
          <td valign=top>Deskripsi</td>  
          <td> <textarea name='deskripsi' style='width: 600px; height: 350px;'></textarea></td>
        </tr>
        <tr>
          <td>Gambar</td>      
          <td> : <input type=file name='fupload' size=40> 
              <br>Tipe gambar disarankan JPG/JPEG dan ukuran lebar maks: 400 px
          </td>
        </tr>
        <tr>
          <td colspan=2>
            <input type=submit class='button' value=Simpan>
            <input type=button class='button' value=Batal onclick=self.history.back()>
          </td>
        </tr>
      </table>
    </form>";?>

and the PHP :

<?php 
$module=$_GET[module];
$act=$_GET[act];

$produk_seo = seo_title($_POST['nama_produk']);

if (!empty($lokasi_file)){
   // Cek file type
   if (($tipe_file =="image/jpeg" OR $tipe_file=="image/gif" OR
        $tipe_file=="image/png" OR $tipe_file=="image/wbmp" )){
            UploadImage($nama_file_unik);
            $query1 ="INSERT INTO sparepart(nama_produk,
                                produk_seo,
                                id_kategori,
                                id_kategori2,
                                berat,
                                harga,
                                diskon,
                                stok,
                                deskripsi,
                                tgl_masuk,
                                potongan,
                                gambar) 
                        VALUES('$_POST[nama_produk]',
                               '$produk_seo',
                               '$_POST[kategori]',
                               '$_POST[kategori2]',
                               '$_POST[berat]',
                               '$_POST[harga]',
                               '$_POST[diskon]',
                               '$_POST[stok]',
                               '$_POST[deskripsi]',
                               '$tgl_sekarang',
                               '$_POST[potongan]',
                               '$nama_file_unik')";
       $sql = mysql_query($query1);
       if (!sql) {
           die('there is an error');
           mysql_errno($sql).":".mysql_error($sql);
       } else {
           echo "success bro!";
       }
   }
   else
   {
      $query2 = "INSERT INTO sparepart(nama_produk,
                                produk_seo,
                                id_kategori,
                                id_kategori2,
                                berat,
                                harga,
                                diskon
                                stok,
                                deskripsi,
                                potongan,
                                tgl_posting) 
                        VALUES('$_POST[nama_produk]',
                               '$produk_seo',
                               '$_POST[kategori]',
                               '$_POST[kategori2]',
                               '$_POST[berat]',                                 
                               '$_POST[harga]',
                               '$_POST[diskon]',
                               '$_POST[stok]',
                               '$_POST[deskripsi]',
                               '$_POST[potongan]',
                               '$tgl_sekarang')";
      $sql=mysql_query($query2);
      if(!sql) {
          die('there is an error');
          mysql_errno($sql).":".mysql_error($sql);
      } else {
          echo "success bro!";
      }
}

I want to know why between the 2 same codes, only 1 works and the other does not echo any error but not working.

I am using mysqli_* in my real project. I just felt more comfortable using Mysql_* when writing this question so thank you for warning me about mysql_* being deprecated.

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Citra45Abadi
  • 207
  • 1
  • 6
  • 21
  • 1
    stop using mysql_query, it's deprecated.. plus your query is vulnerable to sql injection. parametarize it. – Gogol Aug 03 '16 at 09:13
  • 2
    in fact this code gives me the creep :S – Gogol Aug 03 '16 at 09:13
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Aug 03 '16 at 09:14
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Aug 03 '16 at 09:14
  • 1
    `die('there is an error'); mysql_errno($sql).":".mysql_error($sql);` will stop executing code BEFORE referring to the errors. Print the error before you use `die()`. – Gerald Schneider Aug 03 '16 at 09:15
  • 1
    __I am using mysqli_* in my real project__ Yea right! Well IF thats true, show us the real project code. **LOL** – RiggsFolly Aug 03 '16 at 09:15
  • Some sensible code indentation would be a good idea. It help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Aug 03 '16 at 09:18
  • You should try to debug code with [error reporting](http://php.net/manual/en/function.error-reporting.php) – TIGER Aug 03 '16 at 09:18
  • @Tiger If he put the existing error reporting BEFORE the `die()` then he may see some useful information, but as it stands this code is just a mess! Maybe because he rewrote the real code that uses the `mysqli_` API to use `mysql_` API! **FOFL** – RiggsFolly Aug 03 '16 at 09:19
  • You are right @RiggsFolly that will gives more info, He should have to make error reporting on in development mode that can also helps a lot :) – TIGER Aug 03 '16 at 09:23
  • Where do you set `$lokasi_file`? – RiggsFolly Aug 03 '16 at 09:28

2 Answers2

1

use the die() method at the end, thats why you are not getting the error.

if (!sql) {
     /* die('there is an error');  */  // was like this before
     echo mysql_errno($sql).":".mysql_error($sql);
     die('there is an error'); // make it like this
 } 
Aravind Pillai
  • 739
  • 7
  • 21
1
$a=$_POST['kategori'];
$b=$_POST['kategori2'];

$query=mysql_query("INSERT INTO sparepart VALUES('$a','$b')");

if(!query) 
{
        die('there is an error');

} 
else 
{
       echo "success bro!";
}

do Something like this note:- use '' in POST.

Krishna Patel
  • 275
  • 5
  • 19