0

to the point.

mysqli prepared statement does not submitting data into database, and mysqli_error not returning any errors

might be because I made a mistake writing the error handler because it shows the message when mysqli_stmt_execute does not returns TRUE

EDIT

it looks like mysqli_stmt_execute() does not executes the query. why is that?

PHP-mysqli script :

// Simpan data ke dalam variable
$nama_produk=$_POST['nama_produk'];
$id_kategori=$_POST['kategori'];
$id_kategori2=$_POST['kategori2'];
$label=$_POST['label'];
$harga=$_POST['harga'];
$harga2=$_POST['harga2'];
$stok=$_POST['stok'];
$deskripsi=$_POST['deskripsi'];
$spesifikasi=$_POST['spesifikasi'];
$potongan=$_POST['potongan'];
$produk_seo      = seo_title($_POST['nama_produk']);
$tanggal=$tgl_sekarang;
$gambar=$nama_file_unik;

 $stmt=mysqli_prepare($con,"INSERT INTO produk(nama_produk,produk_seo,id_kategori,id_kategori2,label,harga,harga2,stok,spesifikasi,deskripsi,tgl_masuk,potongan,gambar) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");

 mysqli_stmt_bind_param($stmt,"sssssssssssss",$nama_produk,$produk_seo,$id_kategori,$id_kategori2,$label,$harga,$harga2,$stok,$spesifikasi,$deskripsi,$tanggal,$potongan,$gambar);

 if(mysqli_stmt_execute($stmt)){
    mysqli_stmt_error($stmt);
 } else {
     echo"<h1>Query gagal</h1>";
     mysqli_error($con);
     mysqli_errno($con);
     mysqli_stmt_error($stmt);
 }

usually there is a message saying the the number of something does match something etc but there is none of that, just a big 'Query gagal'

LF00
  • 27,015
  • 29
  • 156
  • 295
Citra45Abadi
  • 207
  • 1
  • 6
  • 21
  • `seo_title()` what does that function do? and where's the form for this? Now this `if(mysqli_stmt_execute($stmt)){ mysqli_error($con); }` makes no sense; you're checking if it executes but wanting to check for errors. – Funk Forty Niner Dec 31 '16 at 02:57
  • and where are these defined `$tgl_sekarang` - `$nama_file_unik`? Use error reporting; post your html form and the db schema for this. – Funk Forty Niner Dec 31 '16 at 02:58
  • `seo_title()` just a fuction to replace spaces in string into '-' for example string 'trigit or back' will become 'trigit-or-back' as for the `if(mysqli_stmt_execute)` yes I don't know the proper way to handle errors – Citra45Abadi Dec 31 '16 at 03:00
  • those are defined the in php file. it is too long to write if I include the irrelevant codes too – Citra45Abadi Dec 31 '16 at 03:01
  • PHP: http://php.net/manual/en/function.error-reporting.php - MySQL: http://php.net/manual/en/mysqli-stmt.error.php – Funk Forty Niner Dec 31 '16 at 03:01
  • also, if the column lengths are too short, MySQL will fail silently. As will the form if it's not using a POST array but no method or GET. You also did connect successfully with `mysqli_` yes? – Funk Forty Niner Dec 31 '16 at 03:02
  • yes connection is ok. I also use prepared statement is much simpler form input with less variables. there are some errors appearing like the number of variable does not match or something. which is clear and I can fix it myself, but this one does not showing and error message, added that I don't know how to write error handler – Citra45Abadi Dec 31 '16 at 03:04
  • I already write error_reporting(E_ALL) but the error still not showing – Citra45Abadi Dec 31 '16 at 03:05
  • I don't know what it could then, sorry. Someone posted an answer below, ask them. – Funk Forty Niner Dec 31 '16 at 03:07
  • their answer was deleted. – Funk Forty Niner Dec 31 '16 at 03:16
  • I edited my question, specifically in the code `if(mysqli_stmt_execute($stmt))` I added `mysqli_stmt_error($stmt)`. still the errors message does not appears. please check it – Citra45Abadi Dec 31 '16 at 03:16
  • wait, if I think about it, the query does not executed right? – Citra45Abadi Dec 31 '16 at 03:18
  • You need to `echo mysqli_error($con);` or `echo mysqli_stmt_error($stmt);` ... the return strings so you need `echo` to get useful output. – Michael Berkowski Dec 31 '16 at 03:40

1 Answers1

3

Try this:

// Simpan data ke dalam variable
$nama_produk=$_POST['nama_produk'];
$id_kategori=$_POST['kategori'];
$id_kategori2=$_POST['kategori2'];
$label=$_POST['label'];
$harga=$_POST['harga'];
$harga2=$_POST['harga2'];
$stok=$_POST['stok'];
$deskripsi=$_POST['deskripsi'];
$spesifikasi=$_POST['spesifikasi'];
$potongan=$_POST['potongan'];
$produk_seo      = seo_title($_POST['nama_produk']);
$tanggal=$tgl_sekarang;
$gambar=$nama_file_unik;

$stmt=mysqli_prepare($con,"INSERT INTO produk(nama_produk,produk_seo,id_kategori,id_kategori2,label,harga,harga2,stok,spesifikasi,deskripsi,tgl_masuk,potongan,gambar) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
if(false===$stmt){
die('Error with prepare: ') . htmlspecialchars($mysqli->error));
}

$bp = mysqli_stmt_bind_param($stmt,"sssssssssssss",$nama_produk,$produk_seo,$id_kategori,$id_kategori2,$label,$harga,$harga2,$stok,$spesifikasi,$deskripsi,$tanggal,$potongan,$gambar);
    if(false===$bp){
        die('Error with bind_param: ') . htmlspecialchars($stmt->error));
    }

$bp = $stmt->execute();
    if ( false===$bp ) {
        die('Error with execute: ' . htmlspecialchars($stmt->error));
    }

$stmt->close();
Shaun Adams
  • 76
  • 1
  • 5