Skip to content

Commit

Permalink
BROKEN CODE (HTML, JS) TO CONNECT TO MYSQL SERVER
Browse files Browse the repository at this point in the history
  • Loading branch information
joypan1 committed Feb 6, 2024
1 parent 299e8ff commit c02f9cc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
39 changes: 39 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<title>Teams</title>
<link rel="stylesheet" href="index.css" />
<script src="https://unpkg.com/dexie/dist/dexie.js"></script>

<script>
// Check that service workers are supported
if ("serviceWorker" in navigator) {
Expand Down Expand Up @@ -55,4 +56,42 @@ <h2>Teams</h2>
<script src="index.js"></script>
</body>

<!-- CHATGPT CODE FOR CONNECTING TO MYSQL SERVER-->

<script>
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');

const app = express();
app.use(bodyParser.json());

// MySQL database connection configuration
const connection = mysql.createConnection({
host: 'localhost',
user: 'jamesmur_scout',
password: 'Longmetal7127',
database: 'jamesmur_scouting'
});

// API endpoint for syncing data from client to server
app.post('/sync', (req, res) => {
const teamName = req.body.teamName;
connection.query('INSERT INTO teams (name) VALUES (?)', [teamName], (error, results) => {
if (error) {
console.error(error);
res.status(500).send('Error syncing data');
} else {
res.status(200).send('Data synced successfully');
}
});
});

// Start the server
const port = 3000;
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
</script>

</html>
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,31 @@ window.onload = getTodos;
const deleteTodo = async (event, id) => {
await db.todos.delete(id);
await getTodos();
};
};

// CHATGPT CODE TO CONNECT TO MYSQL SERVER

function saveTeam(teamName) {
db.teams.add({ name: teamName }).then(() => {
syncTeamWithServer(teamName);
});
}

function syncTeamWithServer(teamName) {
fetch('/sync', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ teamName: teamName })
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to sync data');
}
return response.json();
})
.catch(error => {
console.error(error);
});
}

0 comments on commit c02f9cc

Please sign in to comment.