Example #1
0
function getSingleCarData($edit_id)
{
    //echo "id on ".$edit_id;
    $mysqli = new mysqli($GLOBALS["servername"], $GLOBALS["server_username"], $GLOBALS["server_password"], $GLOBALS["database"]);
    $stmt = $mysqli->prepare("SELECT number_plate, color FROM car_plates WHERE id=?");
    //asendan ? märgi
    $stmt->bind_param("i", $edit_id);
    $stmt->bind_result($number_plate, $color);
    $stmt->execute();
    //tekitan objketi
    $car = new Stdclass();
    //saime ühe rea andmeid
    if ($stmt->fetch()) {
        //saan saan siin alles kasutada bind_result muutujaid
        $car->number_plate = $number_plate;
        $car->color = $color;
    } else {
        // ei saanud rida andmeid kätte
        // sellist id'd ei ole olemas
        // see rida võib olla kustutatud
        //saadan kaasa id
        $car_object = getSingleCarData($_GET["edit"]);
        var_dump($car_object);
    }
    return $car;
    $stmt->close();
    $mysqli->close();
}
require_once "edit_functions.php";
//kas kasutaja uuendab andmeid
if (isset($_POST["update"])) {
    updateCar($_POST["id"], $_POST["number_plate"], $_POST["color"]);
}
//id mida muudame
if (!isset($_GET["edit"])) {
    // ei ole aadressieal ?edit=midagi
    // suunan table.php lehele
    header("location: table.php");
} else {
    // saada kätte kõige uuemad andmed selle id kohta
    //numbrimärk ja värv
    //küsime andmebaasist andmed id järgi
    //saadan kaasa id
    $car_object = getSingleCarData($_GET["edit"]);
    var_dump($car_object);
}
?>
<h2>Muuda autot</h2>
  <form action="<?php 
echo htmlspecialchars($_SERVER["PHP_SELF"]);
?>
" method="post" >
	<input type="hidden" name="id" value="<?php 
echo $_GET["edit"];
?>
" > 
  	<label for="number_plate" >auto nr</label><br>
	<input id="number_plate" name="number_plate" type="text" value="<?php 
echo $car_object->number_plate;
Example #3
0
<?php

require_once "edit_functions.php";
//kasutaja muudab andmeid
if (isset($_GET["update"])) {
    //auto id, auto number, auto värv
    updateCarData($_GET["car_id"], $_GET["number_plate"], $_GET["color"]);
}
//kas muutuja on aadressireal
if (isset($_GET["edit_id"])) {
    //trükin aadressirealt muutuja
    echo $_GET["edit_id"];
    //küsin andmed
    $car = getSingleCarData($_GET["edit_id"]);
    var_dump($car);
} else {
    //kui muutujat ei ole
    //ei ole mõtet siia lehele tulla
    //suunan tagasi
    header("Location: table.php");
}
?>
<!-- Salvestamiseks kasutan table.php rida 15 -->
<form action="edit.php" method="get">
	<input name="car_id" type="hidden" value="<?php 
echo $_GET["edit_id"];
?>
">
	<input name="number_plate" type="text" value="<?php 
echo $car->number_plate;
?>