phphtmlmysqlangularjs

write data to MySQL


  1. Here is my problem I tried to write data to MySQL but when I do input and press submit button, got console log message from function which mean everything is ok, but when I look to db have nothing to see. Can anyone help me?
  2. Second thing what I need to do is SELECT data from that db, then that data + new data from input = data save to db.

here is html code:

<div class="body-content bg-1">
<div class="col-sm-12 col-xs-12" ng-controller="UnosUSkladisteCtrl">
    <div class="container">
    <div class="alert alert-info alert-dismissable"><strong>Info!</strong> {{data.message}}</div>
       <div class="center">
            <h1>Ulaz robe u skladište</h1>
        </div>

        <p ng-controller="LoginCtrl">Dobro došao <b>{{deName}}</b>  | <a id="logout" href ng-click="logout()">Odjava</a></p>

    </div>
    
        <a href="#/dashboard"><div class="nav-button center col-sm-4 col-xs-4">Povratak</div></a>
        
        
    <div>
         <form class="form-horizontal col-xs-12" col-sm-12" name="signUpForm" ng-submit="submitFormSignUp()" novalidate>

                <!-- Zlatni medvjed -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="zlatni_medvjed">Zlatni medvjed boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="zlatni_medvjed" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                <!-- Crna kraljica -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="crna_kraljica">Crna kraljica boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="crna_kraljica" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                <!-- Grička vještica -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="gricka_vjestica">Grička vještica boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="gricka_vjestica" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                <!-- Dva klasa -->
                <div class="form-group" ng-class="">
                    <label class="col-sm-4 col-xs-12 control-label no-padding-right " for="dva_klasa">Dva klasa boca 0.5l</label>
                    <div class="col-sm-4 col-xs-12">
                        <span class="block input-icon input-icon-right">  
                            <input ng-model="dva_klasa" placeholder="Količina boca 0.5l" type="number"  class="form-control">                        
                        </span>   
                    </div>
                </div>
                
                <!-- SUBMIT BUTTON -->
                <label class="col-sm-4 control-label no-padding-right"></label>
                    <div class="col-sm-4">
                        <button ng-click="insertdata()" type="submit" class="btn btn-primary btn-lg btn-block">Unesi količine u skladište</button>
                    </div>
            </form> 
    </div>  
    
</div>

Here is js file code:

 angular.module('angularLoginApp')
.controller('UnosUSkladisteCtrl', function($scope,$http) {

    $scope.insertdata = function(){
        $http.post("database/unos-piva.php", {'zlatni_medvjed':$scope.zlatni_medvjed, 'crna_kraljica':$scope.crna_kraljica, 'gricka_vjestica':$scope.gricka_vjestica, 'dva_klasa':$scope.dva_klasa })
        .success(function(data,status,headers,config){
            console.log("Podaci uspiješno spremljeni");
            alert("Nove količine piva su dodane u skladište");
        });
    }
    $scope.data = {message: "Molimo vas da točno navedete što unosite u skladište"};

});

and this is PHP file code to connect:

<?php
$data = json_decode(file_get_contents("php://input"));
$zlatni_medvjed = mysql_real_escape_string($data->zlatni_medvjed);
$crna_kraljica = mysql_real_escape_string($data->crna_kraljica);
$gricka_vjestica = mysql_real_escape_string($data->gricka_vjestica);
$dva_klasa = mysql_real_escape_string($data->dva_klasa);

mysql_connect("localhost","root","");
mysql_select_db("medvedgrad");
mysql_query("INSERT INTO stanje_piva(`zlatni_medvjed`, `crna_kraljica`, `gricka_vjestica`,`dva_klasa`)VALUES('"$zlatni_medvjed"','"$crna_kraljica"','"$gricka_vjestica"','"$dva_klasa"')")
?>

mysql columns zlatni_medvjed, crna_kraljica, gricka_vjestica, dva_klasa


Solution

  • The format of the insert statement is wrong - you are incorrectly using quotes ( both single and double ) and the statement was not terminated with a semi-colon.

    mysql_query("
        INSERT INTO stanje_piva(`zlatni_medvjed`, `crna_kraljica`, `gricka_vjestica`,`dva_klasa`)
        VALUES('{$zlatni_medvjed}','{$crna_kraljica}','{$gricka_vjestica}','{$dva_klasa}')
        ");
    

    That said, this sql is vulnerable to sql injection and you are using the now deprecated mysql_* class - upgrade your code to mysqli or PDO and learn how to use Prepared Statements

    As for the second question do an update stanje_piva ... set field=field+new data.... where id=1 etc ~ you would not need the initial select statement