You do not have permission to view the full content of this post. Log in or register now.
add include connection and start session
<?php
include('connection.php');
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$uname = $_POST['uname']; // Assuming form method is POST
$dateCreated = date('Y-m-d H:i:s');
// Check if user already exists
$checkUserQuery = "SELECT * FROM user WHERE username = '$uname'";
$checkUserResult = mysqli_query($connection, $checkUserQuery);
if (mysqli_num_rows($checkUserResult) > 0) {
// User exists, update total_usage_time and date_last_used
$updateQuery = "UPDATE user SET total_usage_time = total_usage_time + 1, date_last_used = '$dateCreated' WHERE username = '$uname'";
mysqli_query($connection, $updateQuery);
} else...
add include connection and start sessionPlease note that you need to create aregister.php
file on your server to handle the registration logic and database connection. This PHP file should handle the database operations and return a response to the JavaScript fu
add include connection and start session
<?php
include('connection.php');
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$uname = $_POST['uname']; // Assuming form method is POST
$dateCreated = date('Y-m-d H:i:s');
// Check if user already exists
$checkUserQuery = "SELECT * FROM user WHERE username = '$uname'";
$checkUserResult = mysqli_query($connection, $checkUserQuery);
if (mysqli_num_rows($checkUserResult) > 0) {
// User exists, update total_usage_time and date_last_used
$updateQuery = "UPDATE user SET total_usage_time = total_usage_time + 1, date_last_used = '$dateCreated' WHERE username = '$uname'";
mysqli_query($connection, $updateQuery);
} else {
// User doesn't exist, insert new user
$insertQuery = "INSERT INTO user (username, date_created, total_usage_time, date_last_used) VALUES ('$uname', '$dateCreated', 0, '$dateCreated')";
mysqli_query($connection, $insertQuery);
}
}
?>
connection.php
to handle the database connection. The session_start()
function is used to start a new session or resume the existing session. The PHP code checks if the HTTP request method is POST and then proceeds to retrieve the username from the form input. It then checks if the user already exists in the database by executing a SELECT query. If the user exists, it updates the total_usage_time
and date_last_used
fields. If the user doesn't exist, it inserts a new record with the username, date created, and initializes total_usage_time
to 0.Bahala na by kenaniahfix submit button to submit data in database and proceed to next page named welcome.php and some code error in this code snippet:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Kids</title>
<link rel="shortcut icon" href="src/icon.png" type="image/x-icon">
</head>
<style>
body {
background-image: url(src/introg.gif);
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
background-size: cover;
overflow: hidden;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
}
table {
margin: 0 auto;
border-collapse: collapse;
width: 100%;
height: 100%;
font-size: 100px;
}
td {
width: 100px;
text-align: center;
}
Media screen and (max-width: 600px) {
table, tr, td {
display: block;
}
td {
width: 100%;
}
}
.center {
position: absolute;
width: 100%;
top: 270px;
height: 480px;
}
.play, label {
font-family: 'Moon Kids Personal Use', cursive;
}
.button-75 {
background-color: #ff797900;
border: 0;
border-radius: 10px;
color: #fff;
cursor: pointer;
font-size: 80px;
font-weight: bolder;
height: 54px;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
letter-spacing: .4px;
line-height: 1;
padding-top: 3px;
text-decoration: none;
font-family: 'Moon Kids Personal Use', cursive;
}
.button-75:active {
outline: 0;
}
.button-75:hover {
outline: 0;
}
.button-75 span {
-webkit-transition: all 150ms;
-o-transition: all 150ms;
transition: all 150ms;
}
.button-75:hover span {
-webkit-transform: scale(.9);
-ms-transform: scale(.9);
transform: scale(.9);
opacity: .75;
}
.text {
display: inline-block;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
margin-inline: auto;
}
</style>
<body>
<div class="center">
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td class="play"><Button class="button-75" onclick="openModal()"><span class="text">Play</span></Button></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
<link rel="stylesheet" href="modal/style.css">
<div id="modal" class="modal">
<div class="modal-content">
<form id="pinform">
<span class="close">×</span><center><span class="picwel">
<img src="src/waving.gif" alt="admin"></span></center><br>
<label for="uname">What's your name?</label>
<input type="text" id="uname" name="uname" required autocomplete="off"/>
<input type="button" class="button" value="Submit" onclick="registerUser()" />
</form>
</div>
</div>
<?php
include('Connection.php');
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$uname = $_POST['uname'];
$dateCreated = date('Y-m-d H:i:s');
$checkUserQuery = "SELECT * FROM user WHERE username = '$uname'";
$checkUserResult = mysqli_query($connection, $checkUserQuery);
if (mysqli_num_rows($checkUserResult) > 0) {
// update
$updateQuery = "UPDATE user SET total_time_usage = total_time_usage + 1, date_last_used = '$dateCreated' WHERE username = '$uname'";
mysqli_query($connection, $updateQuery);
} else {
// add
$insertQuery = "INSERT INTO user (username, date_created, total_time_usage) VALUES ('$uname', '$dateCreated', 0)";
mysqli_query($connection, $insertQuery);
}
}
?>
</body>
<script>
// Function to open the modal
function openModal() {
document.getElementById("modal").style.display = "block";
}
// Function to close the modal
function closeModal() {
document.getElementById("modal").style.display = "none";
}
// Register user function
function registerUser() {
const username = document.getElementById("uname").value;
const xhr = new XMLHttpRequest();
xhr.open("POST", "register.php", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
if (response.success) {
alert("User registered successfully!");
} else {
alert("Failed to register user. Please try again.");
}
}
};
const data = {
username: username
};
xhr.send(JSON.stringify(data));
}
document.addEventListener("DOMContentLoaded", function() {
// Close the modal when the close button is clicked
document.querySelector(".close").addEventListener("click", function() {
closeModal();
});
// Close the modal when clicking outside the modal
window.addEventListener("click", function(event) {
if (event.target == document.getElementById("modal")) {
closeModal();
}
});
});
</script>
</html>