0

I'm learning to use ajax. I'm facing problems while fixing the issues because there is no error notifications like PHP or MySQL. Till now, I have learn how to manipulate Database using CURD. Everything went well but MySQL itself still do nothing in this part. Please give me some idea, because I don't know what's the problem with this. I think I have not made any mistake.

$("#prodButton").on('click', function(){
    if (jamStock< jamReq || fruitsStock< fruitsReq || sugarStock< sugarReq){
        alert("ingredients is not enough");
    } else {
        $.ajax({
            method: "POST",
            url: "production.php",
            data: {
                jam: jamStock- jamReq,
                fruits: fruitsStock- fruitsReq,
                sugar: sugarStock - sugarReq,
            },
            success: function(data) {
                alert("Data has been changed");
                location.reload();
            }
        });
    }
    return false;
});

oh well about the production.php:

<?php
include 'connect.php';
if (isset($_POST['jam'])) {
    $sql = "UPDATE ingre SET
         jam =" . $_POST['jam'] . "
        , fruits =" . $_POST['fruits'] . "
        , sugar=" . $_POST['sugar'] . "
         WHERE 1";
    mysql_query($sql) or die(mysql_error());
} else {
    die("browser unable to load");
}
?>
Obink
  • 229
  • 2
  • 13
  • please add the PHP code. – Afaan Bilal Aug 03 '17 at 05:08
  • @AfaanBilal ya i forgot about that, just edited. – Obink Aug 03 '17 at 05:09
  • try this mysqli_query($con, $sql) – Sudhakar Annadurai Aug 03 '17 at 05:10
  • "UPDATE `ingre` SET `jam` =" . $_POST['jam'] . " , `fruits` =" . $_POST['fruits'] . " , `sugar`=" . $_POST['sugar'] . " WHERE 1"; – Sudhakar Annadurai Aug 03 '17 at 05:11
  • @sudhakar im using PHP 5.3 and mysql is not deprecated, and yes i'm using mysql. it's not an issue because i'm still learning ajax, and mysql is much faster to type than pdo or mysqli. – Obink Aug 03 '17 at 05:11
  • ok, let me check give me some time – Sudhakar Annadurai Aug 03 '17 at 05:12
  • You should learn to use secure programming from the very start. Please use prepared statements and *never* directly put your input un-sanitized into an SQL query. – Afaan Bilal Aug 03 '17 at 05:14
  • @AfaanBilal i usually using this as my security >>>>>> function array_sanitize(&$item){ $item = htmlentities(strip_tags(mysql_real_escape_string($item))); } <<<<<<< and this >>>>>>function sanitize($data){ return htmlentities(strip_tags(mysql_real_escape_string($data))); } <<<<< – Obink Aug 03 '17 at 05:17
  • in production.php which part is reflect in result ? if OR Else ? – maulik Aug 03 '17 at 05:18
  • @maulik i'm sorry i'm don't get you. but i guess, i have no result statement in there. The result is in the index.php after url:production.php? – Obink Aug 03 '17 at 05:20
  • did you get any error in console ? – JYoThI Aug 03 '17 at 05:20
  • @JYoThI no no, not at all. that's why i asks in here, i don't know what is my mistake. – Obink Aug 03 '17 at 05:21
  • did you noticed that you missed `single` quotes in this line `include connect.php';` – JYoThI Aug 03 '17 at 05:22
  • @JYoThI hahaha sorry sorry, i was change my file name, because to connect it is using my name. LoL – Obink Aug 03 '17 at 05:24
  • `mysql_*` is deprecated use `mysqli_* or pdo` – JYoThI Aug 03 '17 at 05:26
  • @JYoThI im using PHP 5.3 and in that php version, mysql is not deprecated, and yes i'm using mysql. it's not an issue because i'm still learning ajax, and mysql is much faster to type than pdo or mysqli. – Obink Aug 03 '17 at 05:28
  • is your problem solved ? – JYoThI Aug 03 '17 at 05:29
  • @JYoThI not yet, but if u want me to try to convince me about mysql. i can doing query in phpmyadmin using mysql. – Obink Aug 03 '17 at 05:31
  • For testing purpose change all your code into respectively `GET` method and call the file directly in browser with `query string parameters` and make sure that your `php code` is fine or not – JYoThI Aug 03 '17 at 05:33
  • in first place echo "check"; exit(); before & after include 'connect.php'; So you get notify is all ok on your include 'connect.php' file.! – maulik Aug 03 '17 at 05:36
  • @JYoThI which is the method under ajax too, change to GET? – Obink Aug 03 '17 at 05:40
  • @maulik i just did that and it's not echoing, nothing to display, it just get back to my index file do nothing like before. – Obink Aug 03 '17 at 05:41
  • it means issue is in your url: "production.php" ... your file is yet not call for ajax. set ABSPATH in URL . – maulik Aug 03 '17 at 05:42
  • Don't touch those ajax part . simple change your `PHP code` and call the file directly in browser like this `localhost/folder/file.php?jam=valu&fruits=value&sugar=value` – JYoThI Aug 03 '17 at 05:43
  • @JYoThI all of the ingredients, with their values on database? – Obink Aug 03 '17 at 05:46
  • or add error handling part in ajax like this `success: function(data) { alert("Data has been changed"); location.reload(); }, error : function(xhr,error_string,error_r){ console.log(error_string); console.log(error_r); }` – JYoThI Aug 03 '17 at 05:48
  • @JYoThI i did ur error handling and shows nothing. – Obink Aug 03 '17 at 05:51
  • are you sure looking in browser console tab . ? – JYoThI Aug 03 '17 at 05:52
  • @JYoThI i'm using chrome and i do right click inspect or f12 to open the console, is that right? – Obink Aug 03 '17 at 05:52
  • yeah your right . Then we will debug it add alert in else part `if (jamStock< jamReq || fruitsStock< fruitsReq || sugarStock< sugarReq){ alert("ingredients is not enough"); } else { alert('coming to else part'); ... }` – JYoThI Aug 03 '17 at 05:54
  • @JYoThI, ya the alert shows up. and ? – Obink Aug 03 '17 at 06:11

2 Answers2

0

-when you write query for insert or update record in table its important to use these signs for name of columns `, and its important for string values ( like varchar, char, dates, text..) to use single quotes When to use single quotes, double quotes, and backticks in MySQL

$sql = "UPDATE `ingre` 
      SET
     `jam` ='{ $_POST['jam']}', `fruits` = '{$_POST['fruits']}', sugar='{$_POST['sugar']}' WHERE 1";

-when using ajax its important to return something (catch in success and chech what happend) to controle success or fail ajax

-and one more thing, I use this "form" for ajax to control response

$.ajax({

    url: ,
    method: '',
    data: {

    }
}).done(function (data) {

}).fail(function () {

}).always(function () {

});
});
Zeljka
  • 376
  • 1
  • 10
0

Add alert(data); in script & echo in php file & check what you get in response alert message.

$("#prodButton").on('click', function(){
    if (jamStock< jamReq || fruitsStock< fruitsReq || sugarStock< sugarReq){
        alert("ingredients is not enough");
    } else {
        $.ajax({
            method: "POST",
            url: "production.php",
            data: {
                jam: jamStock- jamReq,
                fruits: fruitsStock- fruitsReq,
                sugar: sugarStock - sugarReq,
            },
            success: function(data) {
                alert(data);
                alert("Data has been changed");
                location.reload();
            }
        });
    }
    return false;
});

<?php
include 'connect.php';
echo "connection working<br>";
if (isset($_POST['jam'])) {
  echo "True<br>";
    $sql = "UPDATE ingre SET
         jam =" . $_POST['jam'] . "
        , fruits =" . $_POST['fruits'] . "
        , sugar=" . $_POST['sugar'] . "
         WHERE 1";
    mysql_query($sql) or die(mysql_error());
} else {
  echo "False<br>";
    die("browser unable to load");
}
?>
maulik
  • 919
  • 7
  • 22
  • it didn't work, and i just get back to index.php without any change or error notification. – Obink Aug 03 '17 at 05:44