|
1 | 1 | <?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; |
8 | 17 | }
|
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; |
74 | 29 | }
|
75 | 30 |
|
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 | + } |
83 | 36 |
|
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 |
87 | 48 |
|
88 |
| - if($stmt === false){ |
89 |
| - die("Error"); |
90 |
| - } |
91 | 49 |
|
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"); |
94 | 86 | }
|
| 87 | +} catch (PDOException $e) { |
| 88 | + echo $e->getMessage(); |
| 89 | +} |
95 | 90 |
|
96 |
| - ?> |
| 91 | +?> |
97 | 92 | <p>Samples Here</p>
|
| 93 | +<?php echo $creatorID; ?> |
| 94 | +<?php echo $_SESSION['id']; ?> |
98 | 95 | <form action="samples.php" method="post">
|
99 | 96 | <input type="text" name="sampleName" id="sampleName" required="" placeholder="Sample Name">
|
100 | 97 | <input type="text" name="genre" id="genre" required="" placeholder="Genre">
|
101 | 98 | <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"> |
103 | 101 | <input type="submit" name="addSample" placeholder="Upload the sample">
|
104 | 102 | </form>
|
105 | 103 |
|
|
111 | 109 | <th>Genre</th>
|
112 | 110 | <th>Instrument</th>
|
113 | 111 | <th>BPM</th>
|
| 112 | + <th>creatorID</th> |
114 | 113 | </tr>
|
115 | 114 | </thead>
|
116 | 115 | <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> |
125 | 125 | <?php endwhile; ?>
|
126 | 126 | </tbody>
|
127 | 127 | </table>
|
0 commit comments