Mam następujący problem. Napisałem skrypt do zarządzania prostą bazą danych. Wyświetlanie danych, usuwanie i dodawanie działa bez problemów, ale edycja już nie działa. Szczerze mówiąc nie ma pojęcia co jest źle napisane.
<?php $username="root"; $password="haslo"; $host="localhost"; $dbname="test_sys_log"; $options= array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); try { $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); // echo 'Połączenie nawiązane'; } catch (PDOException $ex) { die('Nie nawiązano połączenia: ' . $ex->getMessage()); } //Mquotes IF(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) { FUNCTION undo_magic_quotes_gpc(&$array) { foreach($array AS &$value) { IF(is_array($value)) { undo_magic_quotes_gpc($value); } else { $value = stripslashes($value); } } } undo_magic_quotes_gpc($_POST); undo_magic_quotes_gpc($_GET); undo_magic_quotes_gpc($_COOKIE); } ?>
<?php require ('common.php'); $query="SELECT animal_id, animal_name, animal_owner FROM animal"; try{ $stmt=$db->prepare($query); $stmt->execute(); } catch (PDOException $ex) { } ?> <html> <head> <title>Zobacz rekordy</title> </head> <body> <h1>Wyświetl rekordy</h1> <p> <a href="animal_add.php">Dodaj zwierze</a></p> <table border="1"> <tr> <th>ID</th> <th>Animal</th> <th>Owner</th> <th></th> <th></th> </tr> <?php while($row=$stmt->fetch()) { } $stmt->closeCursor(); ?> </table> </body> </html>
<?php require ('common.php'); $query = " INSERT INTO animal ( animal_name, animal_owner ) VALUES ( :animal_name, :animal_owner ) "; ':animal_name'=>$_POST['animal_name'], ':animal_owner'=>$_POST['animal_owner'] ); try { $stmt=$db->prepare($query); $result=$stmt->execute($query_parameters); } catch (PDOException $ex) { } ?> <html> <head> <title>Dodaj rekord</title> </head> <body> <h1>Dodaj zwierze</h1> <form action="animal_add.php" method="post"> Nazwa zwierzęcia:<br/> <input type="text" name="animal_name" value=""/> <br/> Właściciel:<br/> <input type="text" name="animal_owner" value=""/> <br/> <input type="submit" value="Dodaj"/> </form> </body> </html>
<?php require ('common.php'); $id=$_GET['animal_id']; // Sprawdz $query="DELETE FROM animal WHERE animal_id=$id"; try { $stmt=$db->prepare($query); $stmt->execute(); } catch (PDOException $ex) { } ?>
<?php require ('common.php'); $id=$_GET['animal_id']; //Sprawdz //echo $id; $query="SELECT * FROM animal WHERE animal_id=$id"; try { $stmt=$db->prepare($query); $stmt->bindParam(':animal_id', $id); $stmt->execute(); } catch(PDOException $ex) { } ?> <html> <head> <title>Edycja rekordów</title> </head> <body> <?php for ($i=0; $row=$stmt->fetch(); $i++) { ?> <form action="animal_edit_script.php" method="post"> Nazwa<br/> Właściciel<br/> <input type="submit" value="Zapisz" /> </form> <?php } ?> </body> </html>
<?php require ('common.php'); $animal_id=$_POST['animal_id']; $animal_name=$_POST['animal_name']; $animal_owner=$_POST['animal_owner']; $query="UPDATE animal SET animal_name=:animal_name, animal_owner=:animal_owner WHERE animal_id=:animal_id "; try { $stmt=$db->prepare($query); $stmt->bindParam('animal_name',$animal_name); $stmt->bindParam('animal_owner',$animal_owner); $stmt->bindParam('animal_id',$animal_id); $stmt->execute(); } catch(PDOException $ex) { } ?>
W tym ostatnim kodzie pewnie "coś" jest źle, po paru godzinach prób udało mi się tylko za pomocą skryptu edycji czyścić dane z rekordów, chociaż sam nie wiem jak... .
Gdyby ktoś mógł mi z tym pomóc będę naprawdę wdzięczny.