Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // baza
- $dbserver = 'localhost';
- $dbuser = 'root';
- $dbpassword = '';
- $dbname = 'myDatabaseName';
- $connection = mysql_connect($dbserver, $dbuser, $dbpassword);
- if (!$connection) {
- die('Could not connect: ' . mysql_error());
- }
- $output = '';
- // usuwanie
- if (isset($_GET['usun']) && !empty($_GET['usun'])) {
- $id = $_GET['usun'];
- $sql = "DELETE FROM myDatabaseTable WHERE id='{$id}'";
- mysql_select_db($dbname);
- $result = mysql_query($sql, $connection);
- if (!$result) {
- die('Could not delete data: ' . mysql_error());
- }
- header("Location: index.php");
- }
- // dodawanie
- if ($_POST && isset($_POST['dodaj'])) {
- $first_name = $_POST['first_name'];
- $last_name = $_POST['last_name'];
- $mail = $_POST['mail'];
- $sql = 'INSERT INTO myDatabaseTable ' .
- '(first_name, last_name, mail) ' .
- 'VALUES ( "' . $first_name . '", "' . $last_name . '", "' . $mail . '" )';
- mysql_select_db($dbname);
- $result = mysql_query($sql, $connection);
- if (!$result) {
- exit('Could not enter data: ' . mysql_error());
- }
- header("Location: index.php");
- }
- // edytowanie formularz
- if (isset($_GET['edytuj']) && !empty($_GET['edytuj'])) {
- $id = $_GET['edytuj'];
- $sql = "SELECT * FROM myDatabaseTable";
- mysql_select_db($dbname);
- $result = mysql_query($sql, $connection);
- if (!$result) {
- die('Could not delete data: ' . mysql_error());
- }
- $output .= "<form method='post'>";
- $output .= "<table border='0'>";
- $output .= "<thead>";
- $output .= "<td>ID</td>";
- $output .= "<td>Imię</td>";
- $output .= "<td>Nazwisko</td>";
- $output .= "<td>Email</td>";
- $output .= "<td>Akcja</td>";
- $output .= "</thead>";
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
- if ($row['id']==$id){
- $output .= "<form method='post'>";
- $output .= "<tr>";
- $output .= "<td><input type='hidden' name='id' value='{$row['id']}' /></td>";
- $output .= "<td><input type='text' name='first_name' placeholder='Wpisz imie...' value='{$row['first_name']}' require/></td>";
- $output .= "<td><input type='text' name='last_name' placeholder='Wpisz nazwisko...' value='{$row['last_name']}' require/></td>";
- $output .= "<td><input type='text' name='mail' placeholder='Wpisz email...' value='{$row['mail']}' require /></td>";
- $output .= "<td><input type='submit' name='zapisz' value='Zapisz' /></td>";
- $output .= "</tr>";
- $output .= "</form>";
- }
- else
- {
- $output .= "<tr>";
- $output .= "<td>{$row['id']}</td>";
- $output .= "<td>{$row['first_name']}</td>";
- $output .= "<td>{$row['last_name']}</td>";
- $output .= "<td>{$row['mail']}</td>";
- $output .= "<td>"
- . "<a href='?usun={$row['id']}' type='usun'>Usuń</a>"
- . " | <a href='?edytuj={$row['id']}' type='edytuj'>Edytuj</a>"
- . "</td>";
- $output .= "</tr>";
- }
- }
- }
- // edytowanie wysyłka formularza
- if ($_POST && isset($_POST['zapisz'])) {
- $id = $_GET['edytuj'];
- $first_name = $_POST['first_name'];
- $last_name = $_POST['last_name'];
- $mail = $_POST['mail'];
- $sql = "UPDATE myDatabaseTable SET "
- . "first_name = '{$first_name}', "
- . "last_name = '{$last_name}', "
- . "mail = '{$mail}' " .
- " WHERE id='{$id}'";
- mysql_select_db($dbname);
- $result = mysql_query($sql, $connection);
- if (!$result) {
- exit('Could not enter data: ' . mysql_error());
- }
- header("Location: index.php");
- }
- // wszystkie rekordy
- if (!isset($_GET['edytuj'])) {
- $sql = 'SELECT id, first_name, last_name, mail FROM myDatabaseTable ORDER BY id ASC, first_name ASC';
- mysql_select_db($dbname);
- $result = mysql_query($sql, $connection);
- if (!$result) {
- die('Could not get data: ' . mysql_error());
- }
- $output .= "<form method='post'>";
- $output .= "<table border='0'>";
- $output .= "<thead>";
- $output .= "<td>ID</td>";
- $output .= "<td>Imię</td>";
- $output .= "<td>Nazwisko</td>";
- $output .= "<td>Email</td>";
- $output .= "<td>Akcja</td>";
- $output .= "</thead>";
- while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
- $output .= "<tr>";
- $output .= "<td>{$row['id']}</td>";
- $output .= "<td>{$row['first_name']}</td>";
- $output .= "<td>{$row['last_name']}</td>";
- $output .= "<td>{$row['mail']}</td>";
- $output .= "<td>"
- . "<a href='?usun={$row['id']}' type='usun'>Usuń</a>"
- . " | <a href='?edytuj={$row['id']}' type='edytuj'>Edytuj</a>"
- . "</td>";
- $output .= "</tr>";
- }
- $output .= "<tr>";
- $output .= "<td></td>";
- $output .= "<td><input type='text' name='first_name' placeholder='Wpisz imie...' /></td>";
- $output .= "<td><input type='text' name='last_name' placeholder='Wpisz nazwisko...' /></td>";
- $output .= "<td><input type='text' name='mail' placeholder='Wpisz email...' /></td>";
- $output .= "<td><input type='submit' name='dodaj' value='Dodaj użytkownika' /></td>";
- $output .= "</tr>";
- $output .= "</form>";
- }
- echo $output;
- mysql_close($connection);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement