0

I've searched the internet and found something about using ajax for setting information in my database. I found this page, but it shows how to GET info from your database.

Is it also possible to SET information in the database?

I use this for a schedule.

in my DB I have a table that contains the following columns:

  • Startdate
  • Enddate
  • Productdescription
  • Expected production
  • actual Production
  • Difference

When they create the schedule they only know the first four. The other two need to be filled in automatically. I do this when the production is finished:

I've made a small start about checking if the production is done:

setInterval(function()
{
    var currentTime = new Date();
    var endProductionTimeMachine1 = <?php echo json_encode($TempomaticRow1[0]['Einddatum']) ?>; // get the enddate from the database via php
    var ActualProductionToday = 2100; //just an example value

    //Convert mysql date to javascript date
    var t = endProductionTimeMachine1.split(/[- :]/);
    var endProdTimeMachine1 = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);

    if(currentTime == endProdTimeMachine1)
    {
         //set information in the DB        
    }
},1000);

So I would like to know if I can call a php page that inserts information into my database with a parameter from this page.

1
  • Getting and setting information are dependent on the query (SELECT, INSERT, UPDATE, DELETE) and all can be performed based on an AJAX call to PHP code. Commented Feb 15, 2016 at 15:03

2 Answers 2

1

Yes, you can.

You can do it with jquery using a post method through ajax.

So, now that you have your vars you have to add a code like this:

$.ajax({
  method: "POST",
  url: "some.php", //the endpoint in php where your save method is
  data: { currentTime: currentTime, endProductionTimeMachine: endProductionTimeMachine1 ...and so on}
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

and at your php just do this...

if(isset($_POST['currentTime']){
    $currentTime =$_POST['currentTime'];
}
... and so on

After you get all your vars be sure you use (if you are not using a framework) a mysql injection cleanup.

I suggest using mysqli_real_escape_string , after having all your variables and all at your PHP side, you can create your query to set/update info at your db.

More info of Ajax here

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way how I can see what he's doing on the PHP page? When I simply open the php page it's empty (since the data it sends isn't set)
you can check your logs, if something failed or was success, it will appear there
if I write a message (for showing if $stmt->execute() === true)) how can I see that in my logs? or how do I log it? I can't see what happends on my php page now....
you can try logging your queries, check this link jigarjain.com/blog/saving-all-db-queries-in-codeigniter
1

Use that page and modify the AJAX call so that it triggers a PHP script which will then update your database. Send all information necessary to the server. In order to change the database, make use of "prepared statements" either via MySQLi or PDO.
Never send a SQL query from a client to a server! Always only send the values and have the server build the query!

For a complete example, you can also take a look at this: http://www.plus2net.com/php_tutorial/display-record-edit.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.