Witam
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)
{
die('Nie nawiązano połączenia: ' . $ex->getMessage()); }
?>
<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())
{
echo '<td>'.$row['animal_id'].'</td>'; echo '<td>'.$row['animal_name'].'</td>'; echo '<td>'.$row['animal_owner'].'</td>'; echo '<td><a href="animal_edit.php?animal_id=' .$row['animal_id']. '">Edytuj</a> </td>'; echo '<td><a href="animal_delete.php?animal_id=' .$row['animal_id']. '">Usuń</a> </td>'; }
$stmt->closeCursor();
?>
</table>
</body>
</html>
<?php
require ('common.php');
$query = "
INSERT INTO animal (
animal_name,
animal_owner
) VALUES (
:animal_name,
:animal_owner
)
";
$query_parameters= array( ':animal_name'=>$_POST['animal_name'],
':animal_owner'=>$_POST['animal_owner']
);
try
{
$stmt=$db->prepare($query);
$result=$stmt->execute($query_parameters);
}
catch (PDOException $ex)
{
die('Nie nawiązano połączenia: ' . $ex->getMessage()); }
?>
<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)
{
die('Nie nawiązano połączenia: ' . $ex->getMessage()); }
?>
<?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)
{
die('Nie nawiązano połączenia: ' . $ex->getMessage()); }
?>
<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">
<input type="hidden" name="animal_id" value="
<?php echo $animal_id; ?>" />
Nazwa<br/>
<input type="text" name="animal_name" value="
<?php echo $row['animal_name']; ?>" /><br>
Właściciel<br/>
<input type="text" name="animal_owner" value="
<?php echo $row['animal_owner']; ?>" /><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)
{
die('Nie nawiązano połączenia: ' . $ex->getMessage()); }
?>
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.
Ten post edytował Humcio 18.09.2014, 18:34:21