Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. <?php
  2.  
  3. // define the database
  4. define("DB_SERVER","localhost");
  5. define("DB_USERNAME","username");
  6. define("DB_PASSWORD","password");
  7. define("DB_NAME","name");
  8.  
  9. // settle the connection for MySQLi
  10. $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME) or die ("Cannot connect to database");
  11.  
  12. // read input from user
  13. $product_code = isset($_POST["product-code"]);
  14. $product_name = isset($_POST["product-name"]);
  15. $product_stock = isset($_POST["product-stock"]);
  16.  
  17. // reading input from user
  18. if(isset($_POST["add_product"])) {
  19. if (empty($_POST["product_code"]) || empty($_POST["product_name"]) || empty($_POST["product_stock"])) {
  20. echo "Kolom kode produk, nama produk, dan persediaan produk harus diisi";
  21. } else {
  22.  
  23. // test input function for validating
  24. function test_input($data) {
  25. $data = trim($data);
  26. $data = stripslashes($data);
  27. $data = htmlspecialchars($data);
  28. return $data;
  29. }
  30.  
  31. // validate the data that the user input by test_input function
  32. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  33. $product_code = test_input($_POST["product_code"]);
  34. $product_name = test_input($_POST["product_name"]);
  35. $product_stock = test_input($_POST["product_stock"]);
  36. }
  37.  
  38. // protect SQL Injecetion
  39. $product_code = mysqli_real_escape_string($connection, $product_code);
  40. $product_name = mysqli_real_escape_string($connection, $product_name);
  41. $product_stock = mysqli_real_escape_string($connection, $product_stock);
  42.  
  43. // check and insert into database
  44. $query = "insert into inventory (no_product, nama_product, stock_product) VALUES ('$product_code', '$product_name', '$product_stock')";
  45. $kueri = "select * from inventory where no_product = '$product_code' and nama_product = '$product_name' and stock_product = '$product_stock'";
  46. $result = mysqli_query($connection, $kueri) or die (mysql_error());
  47. $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  48. if (mysqli_num_rows($result) == 1) {
  49. if ($row["no_product"] == $product_code) {
  50. echo "Data sudah ada<br />";
  51. }
  52. } else {
  53. $execute = mysqli_query($connection, $query) or die (mysql_error());
  54. echo "Data sudah ditambahkan";
  55. }
  56.  
  57. // close connection access
  58. $connection -> close();
  59. }
  60. }
  61.  
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement