0

can anyone explain to me why i can't insert data to my DB, i feel like my code is correct but i don't know why this is happening.

this my php ( home.php) code :

<?php
include 'connection.php';
date_default_timezone_set('Asia/Jakarta');
$date = date('m/d/Y h:i:s a', time());
if ( isset($_POST['btn-additem']) ) {

  // clean user inputs to prevent sql injections
  $name = trim($_POST['inputNamaItem']);
  $name = strip_tags($name);
  $name = htmlspecialchars($name);

  $warna = trim($_POST['inputWarnaItem']);
  $warna = strip_tags($warna);
  $warna = htmlspecialchars($warna);

  $sizeavai = trim($_POST['inputSizeAvai']);
  $sizeavai = strip_tags($sizeavai);
  $sizeavai = htmlspecialchars($sizeavai);

  $S = trim($_POST['inputSzS']);
  $S = strip_tags($S);
  $S = htmlspecialchars($S);

  $M = trim($_POST['inputSzM']);
  $M = strip_tags($M);
  $M = htmlspecialchars($M);

  $L = trim($_POST['inputSzL']);
  $L = strip_tags($L);
  $L = htmlspecialchars($L);

  $XL = trim($_POST['inputSzXL']);
  $XL = strip_tags($XL);
  $XL = htmlspecialchars($XL);

  $Status = trim($_POST['inputStatus']);
  $Status = strip_tags($Status);
  $Status = htmlspecialchars($Status);

  $user = trim($userRow['username']);
  $user = strip_tags($user);
  $user = htmlspecialchars($user);

   $query = "INSERT INTO item(nama,warna,size,S,M,L,XL,status,tanggal,user) VALUES('$name','$warna','$sizeavai','$S','$M','$L','$XL','$Status','$date','$user')";
   $res = mysqli_query($conn,$query);

   if ($res) {
    echo '<div class="alert alert-success alert-dismissible fade show" role="alert">';
    echo '<strong>Success!</strong> New Item has been Added';
    echo '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
    echo '<span aria-hidden="true">&times;</span>';
    echo '</button>';
    echo '</div>';
    unset($name);
    unset($warna);
    unset($sizeavai);
    unset($S);
    unset($M);
    unset($L);
    unset($XL);
    unset($Status);
    unset($user);
   } else {
    echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">';
    echo '<strong>Fail!</strong> New Item not Added';
    echo '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
    echo '<span aria-hidden="true">&times;</span>';
    echo '</button>';
    echo '</div>';
   } 

  }
?>

I have already changed with another $_POST method but still fail to insert. This my connection.php :

<?php
// Change this to your connection info.
$DATABASE_HOST = 'localhost'; \\ default
$DATABASE_USER = 'id12885105_nvrsadmin';  \\ default
$DATABASE_PASS = 'mimin1234'; \\ default
$DATABASE_NAME = 'id12885105_nvrs'; \\ default
// Try and connect using the info above.
$conn = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
    // If there is an error with the connection, stop the script and display the error.
    exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
?>

my connection.php is using default settings by my providers, my providers gave me that code.. the login, everything is running well, but on inserting an item.. it fails

mw509
  • 1,957
  • 1
  • 19
  • 25
kar98k
  • 1
  • 1

0 Answers0