saving information into a MySQL database

  1. I am facing an issue where I am trying to write data to MySQL. When I input the data and press the submit button, the console log message from the function indicates that everything is okay. However, upon checking the database, there is nothing saved. Can someone assist me with this problem?
  2. Additionally, I need to retrieve data from the database, combine it with new input data, and then save it to the database.

Here is the 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 the JavaScript 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 here is the PHP file code for connection:

<?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"')")
?>

The MySQL columns are zlatni_medvjed, crna_kraljica, gricka_vjestica, and dva_klasa.

Answer №1

Your insert statement is incorrect as the format is wrong - make sure you are not using quotes incorrectly and remember to terminate the statement with a semi-colon.

mysql_query("
    INSERT INTO beer_stock(`golden_bear`, `black_queen`, `witch_snack`, `two_classes`)
    VALUES('{$golden_bear}','{$black_queen}','{$witch_snack}','{$two_classes}')
    ");

It's important to note that this sql code is susceptible to sql injection and it's recommended to update your code to use mysqli or PDO along with Prepared Statements

In regards to the second question, for an

update beer_stock ... set field=field+new data.... where id=1
etc ~you might not require the initial select statement

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I use JavaScript to trigger a button click event inside an iframe element

Is there a way to retrieve the inner HTML contents of a clicked element in an iframe loaded content using JavaScript? This code uses jQuery: var elements = document.getElementsByTagName('iframe'); [].forEach.call(elements, function(elem ...

Extracting HTML elements between tags in Node.js is a common task faced

Imagine a scenario where I have a website with the following structured HTML source code: <html> <head> .... <table id="xxx"> <tr> .. </table> I have managed to remove all the HTML tags using a library. Can you suggest w ...

What is the best way to remove or clean up existing Controllers?

After successfully navigating from page A to B using route.resolve, everything works as expected. However, upon returning from page B back to A, I encounter an issue with mismatched anonymous define() module. Even though both pages A and B have different ...

Activate the function only once the display has finished rendering all items from ng-repeat, not just when ng-repeat reaches its last index

Currently, I am generating a list using ng-repeat and each iteration is rendering a component tag with a unique id based on the $index value. The implementation looks like this: <div ng-if="$ctrl.myArr.length > 0" ng-repeat="obj in $ctrl.myArr"> ...

Adding a simulated $state object to an angular unit test

I'm facing some challenges with Angular unit testing as I am not very proficient in it. Specifically, I am struggling to set up a simple unit test. Here is my Class: class CampaignController { constructor($state) { this.$state = $state; ...

Working with radio buttons in a loop using PHP

I am facing an issue with a loop that iterates 3 times. In the loop, there's an HTML form containing radio buttons and I am processing the input using PHP. However, when echoing the form data, it does not display the correct values. Is there something ...

What is the best way to obtain a list of all the modules that are currently accessible in AngularJS

When declaring an Angular module, I specify its dependencies as follows: const myModule = angular.module("MyModuleName", ["Dep1", "Dep2", "Dep3"]); Each dependency comes with its own set of dependencies, directives, controllers, etc. Is there a way to qu ...

It seems that Firefox is ignoring the word-wrap style when the class of a child element is changed

Take a look at this: var iconIndex = 0; var icons = ['check', 'chain-broken', 'flag-o', 'ban', 'bell-o']; $('button:eq(0)').click(function() { iconIndex = (iconIndex + 1) % icons ...

Setting the child elements of a CSS div to have the same width and be larger than the

Welcome everyone to my inaugural post! I hope I've followed the right steps. I'm encountering an issue where I have child divs that need to have equal widths. The #content can sometimes exceed the browser window's width (hence the 3000px), ...

Creating an Automated Backup Solution for Your Website using Bash/Shell Scripting

I am completely new to shell scripting and have been scouring the internet for guidance on how to create a backup script for my website. Unfortunately, I haven't been able to find anything that I can understand. I have a Synology Diskstation server a ...

Guide to testing my-sql database routes in an express application

Trying to test my NodeJS REST API has been a challenge due to most of my routes involving calls to a MySQL database. I've considered mocking or stubbing the database using Sinon, but have not had much success. Making real database calls in tests is no ...

Error message: Unsupported grant type in Laravel PassportExplanation: The specified

Struggling with a Laravel Passport issue for the past 4 days. Here is the code snippet I am using to validate login credentials and authenticate users (token-based): I have diligently followed all the steps required for Passport integration. The API I&ap ...

Error encountered with NG6 Angular sass files

Lately, I've been experimenting with NG6 Angular and it's growing on me. However, I hit a roadblock when attempting to switch from its default stylus to SASS, which is my preferred style in other projects. I have all the necessary dependencies in ...

Angular Material - Header Selection Bug

Trying to implement a Select Header is giving me the error message "Error: md-input-container can only have one child input, textarea or select element!" This is the code causing the issue: <md-input-container> <label>Vegetables</l ...

PHP - session expires upon page refresh

I'm in the process of creating a login system for my website and I've run into an issue with updating the navigation bar once a user has logged in. Every time I refresh the page, it seems like the session gets lost and the navigation bar doesn&ap ...

Generating various fields within a single row

I am in the process of creating a dynamic form that should generate two new fields in the same line when the user clicks on the plus icon. Currently, the code I have only creates a single field. I attempted to duplicate the code within the function, but i ...

Resolving Issues: AngularJS Service Utilizing GET JSON Data

I've been working on a basic AngularJS project that reads data from an external JSON file. Despite multiple attempts, I'm unable to figure out why the application isn't functioning properly. Any suggestions or insights would be greatly appre ...

What's the deal with Mongoose and preloading data?

There is an issue I am facing with regards to retrieving a document from mongodb. In my node.js server configuration, I have the following call: app.get('/ruimtes/:afkortingCampus', function (req, res) { Ruimtes.find({'campusAfkorting&apo ...

Can I change the name of an item in ngRepeat?

When repeating in a view: ng-repeat="item in list" In some scenarios, the 'item' looks like this: { "name": "john", "id": 1 } While in others, it appears as: { "value": { "name": "john", "id": 1 } } Is there a way to rena ...

Are you in need of a flexbox list that can be scrolled

How can I create a scrollable ul element? Currently, it appears that the ul flexbox extends beyond the browser window. html <div class="container"> <div class="header"> Untitled </div> <ul/> <li>1</li> ...