Jump to content

Need help with PHP and MySQL

xQubeZx

Hello,

 

I'm having troubles with some data I'm sending from a javascript to my MySQL database trough PHP. The problem is that i only want the data once every minute but I'm getting like five every second wich is quite annoying. I have tried to fix it but I just don't seems to suceed. Some help would be greatly appriceiated. 

 

Here is my code:

 

The Javascript

(function ($, document, undefined) {$(function () {          setInterval(function(){     $.getJSON("https://dweet.io/get/latest/dweet/for/xQubeZ_temperature", function (data) {    	/*Clears old data*/	$('#thing').empty();	$('#created').empty();	$('#temperature').empty();	$('#humidity').empty();			var $thing,        $created,		$temperature,		$humidity,		/*Gives data a value*/	    $thing = $('#thing');    $created = $('#created');    $temperature = $('#temperature');	$humidity = $('#humidity');		    $thing.text(data.with[0].thing);    $created.text(data.with[0].created);	$temperature.text(data.with[0].content.temperature);	$humidity.text(data.with[0].content.humidity);		// Post the data to the php backend    $.post(        "database_insert.php", //File to send data too         {            created: data.with[0].created,            temperature: data.with[0].content.temperature,            humidity: data.with[0].content.humidity        },        function(returnData) {            // if you want to handle what the php returns        },        'json'    );		/*Time before reset*/		}, 5000);	  });});})(jQuery, this);

And the PHP:

<?php$servername = "localhost";$username = "root";$password = "passwd";$dbname = "test_arduino";// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);}// prepare and bind$stmt = $conn->prepare("INSERT INTO dht (temperature, humidity, date) VALUES (?, ?, ?)");$stmt->bind_param("sss", $temp, $hum, $date);// set parameters and execute$temp = $_POST["temperature"];$hum = $_POST["humidity"];$date = $_POST["created"];$stmt->execute();$stmt->close();$conn->close();?>

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

Didn't you see the

/*Time before reset*/
    
    }, 5000);

part of the code? 5000 ms = 5s.

If you want one minute do 60 * 1000 as that argument.

Considering there is even a comment, I'm smelling a strong copy paste :D

CPU: Ryzen 3 3600 | GPU: Gigabite GTX 1660 super | Motherboard: MSI Mortar MAX | RAM: G Skill Trident Z 3200 (2x8GB) | Case: Cooler Master Q300L | Storage: Samsung 970 EVO 250G + Samsung 860 Evo 1TB | PSU: Corsair RM650x | Displays: LG 27'' G-Sync compatible 144hz 1080p | Cooling: NH U12S black | Keyboard: Logitech G512 carbon | Mouse: Logitech g900 

Link to comment
Share on other sites

Link to post
Share on other sites

Didn't you see the

/*Time before reset*/

    

    }, 5000);

part of the code? 5000 ms = 5s.

If you want one minute do 60 * 1000 as that argument.

Considering there is even a comment, I'm smelling a strong copy paste :D

Forgott to mention i tried to get it working with that code but it doesnt seem to work since i still gets like 5 every second in my database instead of one per five seconds as it should be with that code. 

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

Forgott to mention i tried to get it working with that code but it doesnt seem to work since i still gets like 5 every second in my database instead of one per five seconds as it should be with that code. 

Are you sure you weren't getting a cached version of the JS? It IS the right way to use the function.

CPU: Ryzen 3 3600 | GPU: Gigabite GTX 1660 super | Motherboard: MSI Mortar MAX | RAM: G Skill Trident Z 3200 (2x8GB) | Case: Cooler Master Q300L | Storage: Samsung 970 EVO 250G + Samsung 860 Evo 1TB | PSU: Corsair RM650x | Displays: LG 27'' G-Sync compatible 144hz 1080p | Cooling: NH U12S black | Keyboard: Logitech G512 carbon | Mouse: Logitech g900 

Link to comment
Share on other sites

Link to post
Share on other sites

Are you sure you weren't getting a cached version of the JS? It IS the right way to use the function.

Not sure, I'm still learning this stuff so I'm not that good. How can I check if it is cached or not? 

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

 

Not sure, I'm still learning this stuff so I'm not that good. How can I check if it is cached or not? 

Change it to 60*1000

Open it in Chrome

Press F12

Press CTRL + O

Type the name of the JS file

See if it is still 5000 or 60*1000

CPU: Ryzen 3 3600 | GPU: Gigabite GTX 1660 super | Motherboard: MSI Mortar MAX | RAM: G Skill Trident Z 3200 (2x8GB) | Case: Cooler Master Q300L | Storage: Samsung 970 EVO 250G + Samsung 860 Evo 1TB | PSU: Corsair RM650x | Displays: LG 27'' G-Sync compatible 144hz 1080p | Cooling: NH U12S black | Keyboard: Logitech G512 carbon | Mouse: Logitech g900 

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

Change it to 60*1000

Open it in Chrome

Press F12

Press CTRL + O

Type the name of the JS file

See if it is still 5000 or 60*1000

 

It is changed, but does it matter if the javascript is inside a .php file with html, php and some other stuff?

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

It is changed, but does it matter if the javascript is inside a .php file with html, php and some other stuff?

It shouldn't matter. I suggest you place a breakpoint on the line that executes the POST request to the PHP file and see how often is it called. That way you can see if the problem is in the javascript at all. If you don't know how, google search "how to debug JS in chrome".

CPU: Ryzen 3 3600 | GPU: Gigabite GTX 1660 super | Motherboard: MSI Mortar MAX | RAM: G Skill Trident Z 3200 (2x8GB) | Case: Cooler Master Q300L | Storage: Samsung 970 EVO 250G + Samsung 860 Evo 1TB | PSU: Corsair RM650x | Displays: LG 27'' G-Sync compatible 144hz 1080p | Cooling: NH U12S black | Keyboard: Logitech G512 carbon | Mouse: Logitech g900 

Link to comment
Share on other sites

Link to post
Share on other sites

It shouldn't matter. I suggest you place a breakpoint on the line that executes the POST request to the PHP file and see how often is it called. That way you can see if the problem is in the javascript at all. If you don't know how, google search "how to debug JS in chrome".

Well it seems to be called alot of times, don't know why thought

FX-8350 GTX760 16GB RAM 250GB SSD + 1TB HDD

 

"How many roads must a man walk down?" "42"

Link to comment
Share on other sites

Link to post
Share on other sites

Well it seems to be called alot of times, don't know why thought

take stuff away until it works as expected

for instance, take away the whole ajax call and see how often the function is called

 

is there some other script running? this one you posted should behave correctly, maybe the problem is elsewhere, there could be some old code you forgot to delete

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×