Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

Webserver1/Ovningar/mySQL/

admin.php
fetch_car_things.php
fetch_kompisar.php
fetch_users.php
mysql1.php
mysql2.php
mysql3.php
mysql3_satt_att_sortera.php
mysql3car.php
mysql3garage.php
mysql3owner.php
welcome.php

mysql3garage.php

138 lines UTF-8 Windows (CRLF)
<?php
include('../../incl/dbconnection.php');
/**
 * @var PDO $dbconn
 */
try {
  if (isset(
$_POST['garage'])) {
    
$garage $_POST['garage'];

    
$sqlCheck "SELECT * FROM garage WHERE name=?";

    
// Kolla om garaget redan finns i tabellen
    
$check_stmt $dbconn->prepare($sqlCheck);
    
$check_stmt->execute([$garage]);
    
$check_result $check_stmt->fetch(PDO::FETCH_ASSOC);
    if (empty(
$check_result) || $garage != $check_result['name']) {
        
$sqlInsert "INSERT INTO garage (name) VALUES (?)";

      
$insert_stmt $dbconn->prepare($sqlInsert);
      
$insert_stmt->execute([$garage]);
      echo 
"Lade till nytt garage: $garage";
    }
    else {
      echo 
"<p>Garage finns redan, ingenting gjordes.</p>";
    }
  } else {
    echo 
"Formuläret är tomt!";
  }
} catch (
PDOException $e) {
  echo 
"Exception occured: " $e;
}
?>

<!DOCTYPE html>
<html lang="sv">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>The Ultimate Garage Interface</title>
  <style>
    table {
      border: 1px solid black;
    }
    #tableContainer tr:nth-child(even) {
      background-color: lightgray;
    }
    th {
      border-bottom: 1px solid black;
    }
  </style>
</head>

<body>
  <h1>The Ultimate Garage Interface&trade;</h1>
  <form action="" method="POST">
    <table>
      <h2>Lägg till garage</h2>
      <tbody>
        <tr>
          <td><label for="garage">Namn*:</label></td>
          <td><input type="text" name="garage" required></td>
        </tr>
        <tr>
          <td>
            <input type="submit" value="Lägg till">
          </td>
        </tr>
      </tbody>
    </table>
  </form>
  <ul>
    <li>
      <a href="./mysql3car.php">
        <button>Lägg till bil -></button>
      </a>
    </li>
    <li>
      <a href="./mysql3owner.php">
        <button>Lägg till person -></button>
      </a>
    </li>
  </ul>

  <button id="showButton">Visa tabeller</button>
  <div id="tableContainer"></div>

  <script>
    const showButton = document.getElementById("showButton");
    // Show items in table by clicking button
    showButton.addEventListener("click", async () => {
      try {
        const response = await fetch('fetch_car_things.php');
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        const data = await response.json();

        const tableContainer = document.getElementById("tableContainer");

        if (data.length === 0) {
          tableContainer.innerHTML = `<p>Tabellen är tom.</p>`
        }

        // Build the table HTML
        let allTablesHTML = "";
        Object.keys(data).forEach(tableName => {
          let columns = [];
          // Create header
          let tableHTML = `<h2>${tableName}</h2><table><thead>`;
          Object.keys(data[tableName][0]).forEach(colName => {
            tableHTML += "<th>" + colName + "</th>";
            columns.push(colName);
          })
          tableHTML += "</thead><tbody>";

          // Create rows
          data[tableName].forEach(row => {
            tableHTML += "<tr>"
            columns.forEach(col => {
              tableHTML += "<td>" + row[col] + "</td>"
            })
            tableHTML += "</tr>"
          })
          tableHTML += '</tbody></table>';
          allTablesHTML += tableHTML;
        });

        tableContainer.innerHTML = allTablesHTML;
      } catch (error) {
        console.error('Kunde inte hämta tabelldata:', error);
        document.getElementById('tableContainer').innerHTML = '<p>Ett fel uppstod vid hämtning av data.</p>';
      }
    });
  </script>
</body>
</html>
<?php $dbconn null ?>