2016-09-14 73 views
0

我有php頁面,可以查看,添加,刪除數據庫中的記錄,但是我無法使其工作來編輯記錄。我有問題要id到edit_h.php。當我在(「更新uzytkownik SET LOGIN =:登錄WHERE ID = :ID」)手動輸入ID號時,它可以正常工作。我現在一直堅持這個問題。感謝您的任何建議幫助。獲取編輯記錄的ID PHP + Oracle

這裏是我的代碼:

edit.php

<?php 
session_start(); 


?> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Edytowanie uzytkownikow</title> 
</head> 

<body> 


<form action="edit_h.php" method="post"> 
    Login:<br> 
    <input type="text" name="login"> 
    <br> 
    <input type="submit" value="Edytuj"> 
</form> 

</body> 

</html> 

edit_h.php

<?php 

include_once "polacz.php"; 




$id = $_GET['id']; 
$login = $_POST['login']; 

$con = polacz(); 



$stid = oci_parse($con, "UPDATE uzytkownik SET LOGIN = :login WHERE id = :id"); 

oci_bind_by_name($stid,':login',$login); 
oci_bind_by_name($stid,':id',$id); 





if (oci_execute($stid)) 
{ 
    header("Location: view.php"); 
} 
else 

{ 
    echo "blad"; 
} 

view.php

<?php 
include_once "polacz.php"; 
session_start(); 
if (!isset($_SESSION['id'])) 
{ 
    header("Location: login.php"); 
    die(); 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Logowanie</title> 
<style> 
table { 
    border-collapse: collapse; 
    width: 100%; 
} 

th, td { 
    text-align: left; 
    padding: 8px; 
} 

tr:nth-child(even){background-color: #f2f2f2} 

th { 
    background-color: #af504c; 
    color: white; 
} 
</style> 
</head> 

<body> 

<?php 

echo "Witaj ".$_SESSION['login']; 

echo '<br><a href="logout.php">WYLOGUJ SIĘ</a><br>'; 

$con = polacz(); 


if (isset($_GET['sort'])) 
{ 
    $sort = (int)$_GET['sort']; 
    if ($sort == 1) 
    { 
     $sort = 2; 
    } 
    else 
    { 
     $sort = 1; 
    } 
} 
else 
{ 
    $sort = 1; 
} 
$dbsort =array(1=>'ASC',2=>'DESC'); 
$stid = oci_parse($con,"SELECT id, login FROM uzytkownik ORDER BY login ".$dbsort[$sort]); 
oci_execute($stid); 

echo "<table>"; 
echo "<tr><th>ID</th><th><a href=\"view.php?sort=$sort\">Login<th>Usun</th><th>Edytuj</th> 

</th></tr>"; 
while (($row = oci_fetch_array($stid, OCI_ASSOC)) != false) 
{ 

    $id = $row['ID']; 
    $login = $row['LOGIN']; 


    echo "<tr><td>$id</a></td><td>$login</td> 
    <td><a href=\"delete.php?id=$id\">Usun</a> 
    <td><a href=\"edit.php?id=$id\">Edytuj</a></tr>"; 
} 
echo "</table>"; 
echo '<br><a href="add.php">Dodaj uzytkownika</a><br>'; 

?> 

</body> 

</html> 
+0

那麼,你期望'$ _GET ['id']'應該來自edit_h.php?不過,你可以在'edit.php'中找到它,所以你需要把它作爲隱藏的輸入包含在其中。 – Jeff

回答

0

兩種可能性在這裏:

你必須存儲在會話id了,所以你可以只從那裏得到它:

// in edit_h.php - make sure you start session before! 
$id = $_SESSION['id']; 

或者你把它傳遞各種腳本:

// edit.php 
<form action="edit_h.php" method="post"> 
    Login:<br> 
    <input type="text" name="login"> 
    <br> 
    <input type="submit" value="Edytuj"> 
    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"> 
</form> 

// edit_h.php 
$id = $_POST['id']; // note, that this is stored in POST now! 

注意,它不是這樣的好主意通過腳本傳遞一個普通的id,因爲它很容易被黑客攻擊。所以選項1會更好(取決於你如何獲得ID)!