Skip to content

Commit 1aa611e

Browse files
committed
X
1 parent 294279e commit 1aa611e

File tree

2 files changed

+222
-96
lines changed

2 files changed

+222
-96
lines changed

samples.php

+96-96
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,103 @@
11
<?php
2-
include_once('header.php');
3-
include_once('footer.php');
4-
include_once('db.php');
5-
if(isset($_SESSION['id']))
6-
{
7-
$creatorID = $_SESSION['id'];
2+
include_once('header.php');
3+
include_once('footer.php');
4+
include_once('db.php');
5+
if (isset($_SESSION['id'])) {
6+
$creatorID = $_SESSION['id'];
7+
} else {
8+
}
9+
$sampleName = $genre = $instrument = $bpm = "";
10+
11+
if (!isset($_POST['addSample'])) {
12+
//un champ obligatoire
13+
if (!empty($_POST['sampleName'])) {
14+
$sampleName = trim($_POST['sampleName']);
15+
} else {
16+
$mistakes['sampleName'] = true;
817
}
9-
$sampleName = $genre = $instrument = $bpm = "";
10-
if (!isset($_POST['addSample']) ) {
11-
//un champ obligatoire
12-
if ( !empty($_POST['sampleName']) )
13-
{
14-
$sampleName = trim($_POST['sampleName']) ;
15-
}
16-
else
17-
{
18-
$mistakes['sampleName'] = true;
19-
}
20-
21-
if ( !empty($_POST['genre'])){
22-
$genre = trim($_POST['genre']) ;
23-
}
24-
else
25-
{
26-
$mistakes['genre'] = true;
27-
}
28-
29-
if ( !empty($_POST['instrument'])){
30-
$instrument = trim($_POST['instrument']) ;
31-
}
32-
else
33-
{
34-
$mistakes['instrument'] = true;
35-
}
36-
37-
if ( !empty($_POST['bpm'])){
38-
$bpm = trim($_POST['bpm']) ;
39-
}
40-
else
41-
{
42-
$mistakes['bpm'] = true;
43-
}
44-
45-
46-
47-
48-
//un champ obligatoire avec certaines valeurs rejetées
49-
50-
51-
52-
53-
//s'il n'y a pas d'erreur...
54-
if (empty($mistakes))
55-
{
56-
include("db.php");
57-
58-
$req=$bd->prepare('INSERT INTO samples (sampleName,genre,instrument,bpm,creatorID) VALUES (:sampleName,:genre,:instrument,:bpm,:creatorID)');
59-
$req->bindValue(':sampleName', $sampleName, PDO::PARAM_STR);
60-
$req->bindValue(':genre', $genre, PDO::PARAM_STR);
61-
$req->bindValue(':instrument', $instrument, PDO::PARAM_STR);
62-
$req->bindValue(':bpm', $bpm, PDO::PARAM_STR);
63-
$req->bindValue(':creatorID', $creatorID, PDO::PARAM_STR);
64-
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
65-
$req->execute();
66-
$req->closeCursor();
67-
header("Location:samples.php");
68-
exit();
69-
70-
}
71-
else{
72-
print_r($mistakes);
73-
}
18+
19+
if (!empty($_POST['genre'])) {
20+
$genre = trim($_POST['genre']);
21+
} else {
22+
$mistakes['genre'] = true;
23+
}
24+
25+
if (!empty($_POST['instrument'])) {
26+
$instrument = trim($_POST['instrument']);
27+
} else {
28+
$mistakes['instrument'] = true;
7429
}
7530

76-
$host = 'localhost';
77-
$dbname = 'samplitek';
78-
$usern = 'root';
79-
$passw = '';
80-
$dsn = "mysql:host=$host;dbname=$dbname";
81-
// get all users
82-
$sql = "SELECT * FROM samples";
31+
if (!empty($_POST['creatorID'])) {
32+
$creatorID = trim($_POST['creatorID']);
33+
} else {
34+
$mistakes['creatorID'] = true;
35+
}
8336

84-
try{
85-
$pdo = new PDO($dsn, $usern, $passw);
86-
$stmt = $pdo->query($sql);
37+
if (!empty($_POST['bpm'])) {
38+
$bpm = trim($_POST['bpm']);
39+
} else {
40+
$mistakes['bpm'] = true;
41+
}
42+
43+
44+
45+
46+
47+
//un champ obligatoire avec certaines valeurs rejetées
8748

88-
if($stmt === false){
89-
die("Error");
90-
}
9149

92-
}catch (PDOException $e){
93-
echo $e->getMessage();
50+
51+
52+
//s'il n'y a pas d'erreur...
53+
if (empty($mistakes)) {
54+
include("db.php");
55+
56+
$req = $bd->prepare('INSERT INTO samples (sampleName,genre,instrument,creatorID,bpm) VALUES (:sampleName,:genre,:instrument,:creatorID,:bpm)');
57+
$req->bindValue(':sampleName', $sampleName, PDO::PARAM_STR);
58+
$req->bindValue(':genre', $genre, PDO::PARAM_STR);
59+
$req->bindValue(':instrument', $instrument, PDO::PARAM_STR);
60+
$req->bindValue(':creatorID', $creatorID, PDO::PARAM_STR);
61+
$req->bindValue(':bpm', $bpm, PDO::PARAM_STR);
62+
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
63+
$req->execute();
64+
$req->closeCursor();
65+
header("Location:samples.php");
66+
exit();
67+
} else {
68+
print_r($mistakes);
69+
}
70+
}
71+
72+
$host = 'localhost';
73+
$dbname = 'samplitek';
74+
$usern = 'root';
75+
$passw = '';
76+
$dsn = "mysql:host=$host;dbname=$dbname";
77+
// get all users
78+
$sql = "SELECT * FROM samples";
79+
80+
try {
81+
$pdo = new PDO($dsn, $usern, $passw);
82+
$stmt = $pdo->query($sql);
83+
84+
if ($stmt === false) {
85+
die("Error");
9486
}
87+
} catch (PDOException $e) {
88+
echo $e->getMessage();
89+
}
9590

96-
?>
91+
?>
9792
<p>Samples Here</p>
93+
<?php echo $creatorID; ?>
94+
<?php echo $_SESSION['id']; ?>
9895
<form action="samples.php" method="post">
9996
<input type="text" name="sampleName" id="sampleName" required="" placeholder="Sample Name">
10097
<input type="text" name="genre" id="genre" required="" placeholder="Genre">
10198
<input type="text" name="instrument" id="instrument" required="" placeholder="Instrument">
102-
<input type="text" name="bpm" id="name" required="" placeholder="BPM">
99+
<input readonly type="hidden" name="creatorID" id="creatorID" <?php if (isset($creatorID)) echo 'value="', $creatorID, '"'; ?>>
100+
<input type="text" name="bpm" id="bpm" required="" placeholder="BPM">
103101
<input type="submit" name="addSample" placeholder="Upload the sample">
104102
</form>
105103

@@ -111,17 +109,19 @@
111109
<th>Genre</th>
112110
<th>Instrument</th>
113111
<th>BPM</th>
112+
<th>creatorID</th>
114113
</tr>
115114
</thead>
116115
<tbody>
117-
<?php while($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
118-
<tr>
119-
<td><?php echo htmlspecialchars($row['id']); ?></td>
120-
<td><?php echo htmlspecialchars($row['sampleName']); ?></td>
121-
<td><?php echo htmlspecialchars($row['genre']); ?></td>
122-
<td><?php echo htmlspecialchars($row['instrument']); ?></td>
123-
<td><?php echo htmlspecialchars($row['bpm']); ?></td>
124-
</tr>
116+
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
117+
<tr>
118+
<td><?php echo htmlspecialchars($row['id']); ?></td>
119+
<td><?php echo htmlspecialchars($row['sampleName']); ?></td>
120+
<td><?php echo htmlspecialchars($row['genre']); ?></td>
121+
<td><?php echo htmlspecialchars($row['instrument']); ?></td>
122+
<td><?php echo htmlspecialchars($row['bpm']); ?></td>
123+
<td><?php echo htmlspecialchars($row['creatorID']); ?></td>
124+
</tr>
125125
<?php endwhile; ?>
126126
</tbody>
127127
</table>

samplitek.sql

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 5.1.1
3+
-- https://www.phpmyadmin.net/
4+
--
5+
-- Hôte : 127.0.0.1:3306
6+
-- Généré le : jeu. 12 mai 2022 à 12:36
7+
-- Version du serveur : 5.7.36
8+
-- Version de PHP : 7.4.26
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
START TRANSACTION;
12+
SET time_zone = "+00:00";
13+
14+
15+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
16+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
17+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
18+
/*!40101 SET NAMES utf8mb4 */;
19+
20+
--
21+
-- Base de données : `samplitek`
22+
--
23+
24+
-- --------------------------------------------------------
25+
26+
--
27+
-- Structure de la table `samples`
28+
--
29+
30+
DROP TABLE IF EXISTS `samples`;
31+
CREATE TABLE IF NOT EXISTS `samples` (
32+
`id` int(11) NOT NULL AUTO_INCREMENT,
33+
`sampleName` varchar(255) NOT NULL,
34+
`genre` varchar(255) NOT NULL,
35+
`instrument` varchar(255) NOT NULL,
36+
`creatorID` int(255) NOT NULL,
37+
`bpm` int(255) NOT NULL,
38+
PRIMARY KEY (`id`),
39+
KEY `creatorID` (`creatorID`)
40+
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
41+
42+
--
43+
-- Déchargement des données de la table `samples`
44+
--
45+
46+
INSERT INTO `samples` (`id`, `sampleName`, `genre`, `instrument`, `creatorID`, `bpm`) VALUES
47+
(3, 'z', 'z', 'z', 10, 123);
48+
49+
-- --------------------------------------------------------
50+
51+
--
52+
-- Structure de la table `songs`
53+
--
54+
55+
DROP TABLE IF EXISTS `songs`;
56+
CREATE TABLE IF NOT EXISTS `songs` (
57+
`id` int(255) NOT NULL AUTO_INCREMENT,
58+
`songName` varchar(255) NOT NULL,
59+
`genre` varchar(255) NOT NULL,
60+
`bpm` int(255) NOT NULL,
61+
`creatorID` int(255) NOT NULL,
62+
PRIMARY KEY (`id`),
63+
KEY `creatorID` (`creatorID`) USING BTREE
64+
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
65+
66+
--
67+
-- Déchargement des données de la table `songs`
68+
--
69+
70+
INSERT INTO `songs` (`id`, `songName`, `genre`, `bpm`, `creatorID`) VALUES
71+
(2, 'La truelle de ma tante', 'BoumBoum', 175, 14);
72+
73+
-- --------------------------------------------------------
74+
75+
--
76+
-- Structure de la table `users`
77+
--
78+
79+
DROP TABLE IF EXISTS `users`;
80+
CREATE TABLE IF NOT EXISTS `users` (
81+
`id` int(11) NOT NULL AUTO_INCREMENT,
82+
`username` varchar(30) NOT NULL,
83+
`email` varchar(255) NOT NULL,
84+
`password` varchar(255) NOT NULL,
85+
`firstName` varchar(255) DEFAULT 'John',
86+
`lastName` varchar(255) DEFAULT 'Doe',
87+
`rights` enum('1','2','3') NOT NULL DEFAULT '1',
88+
`active` enum('0','1') NOT NULL DEFAULT '1',
89+
PRIMARY KEY (`id`),
90+
UNIQUE KEY `id` (`id`),
91+
UNIQUE KEY `username` (`username`),
92+
UNIQUE KEY `email` (`email`)
93+
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
94+
95+
--
96+
-- Déchargement des données de la table `users`
97+
--
98+
99+
INSERT INTO `users` (`id`, `username`, `email`, `password`, `firstName`, `lastName`, `rights`, `active`) VALUES
100+
(9, 'Hal', '[email protected]', '3109328f5e8d26ecfb0267ee264894d15967a6ea', 'John', 'Doe', '2', '1'),
101+
(10, 'Admin', '[email protected]', '3844ace49162de4df4b17d7d3a5e5388e2f51990', 'John', 'Doe', '3', '1'),
102+
(12, 'Bobo', '[email protected]', 'b736efda7342c257b42af16d6f7b8da01d5aa165', 'John', 'Doe', '1', '1'),
103+
(13, 'Matteo', '[email protected]', '7a85f4764bbd6daf1c3545efbbf0f279a6dc0beb', 'Matteo', 'Alcantarini', '1', '1'),
104+
(14, 'Monkey', '[email protected]', 'fc6fae10db2bd0b625077d7c6d1b9a96925fd2b7', 'John', 'Doe', '1', '1'),
105+
(15, 'Toto', '[email protected]', '0b9c2625dc21ef05f6ad4ddf47c5f203837aa32c', 'John', 'Doe', '1', '1');
106+
107+
--
108+
-- Contraintes pour les tables déchargées
109+
--
110+
111+
--
112+
-- Contraintes pour la table `samples`
113+
--
114+
ALTER TABLE `samples`
115+
ADD CONSTRAINT `samples_ibfk_1` FOREIGN KEY (`creatorID`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
116+
117+
--
118+
-- Contraintes pour la table `songs`
119+
--
120+
ALTER TABLE `songs`
121+
ADD CONSTRAINT `songs_ibfk_1` FOREIGN KEY (`creatorID`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
122+
COMMIT;
123+
124+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
125+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
126+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 commit comments

Comments
 (0)