1

Need to be able to enter data into an MySQL table, on page load.

example:

 $q_edituser = "INSERT INTO hk_logs (type, time, who_done) 
                VALUES('Logged In','". time() ."','{$_SESSION['user']['username']}')";

but can I make it "execute" on page load?

1 Answer 1

1

Since the page loads sequentially and if you are working in PHP, you could create a database connection on that page and have it at the top of the page as a php query that connects to the db, and performs the action.

Or you could have it in a separate php file, have a form on your HTML page with this php page as its action and on page load submit the form via either ajax or straight JS on either document.ready if using jQuery or window.onload if using js. Note that simply passing to the other form page will either need a header redirection back or be done with AJAX to not remove the user from the page.

initialPage.html
//HTML stuff
<form id="someForm" action="editUser.php" method="POST">
    <input type="hidden" name="logUser" value="login"/>
</form>


//js in initialPage.html
$(document).ready(function(){
    $('#someForm').submit();
})

//editUser.php
 session_start();
//appropriate database connection validation etc
if(isset($_POST('logUser')){
//php stuff
$q_edituser = "INSERT INTO hk_logs (type, time, who_done) VALUES('Logged In','". time() ."','{$_SESSION['user']['username']}')";
//other PHP stuff
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks. I have no knowledge of js at all really, could you give an example of the code to do that?
Still not working, is there not a simple way to just insert data into a database when the page loads?

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.