获取ID以编辑记录PHP + Oracle

时间:2016-09-14 23:31:06

标签: php oracle

我有php页面,可以让我查看,添加,删除数据库中的记录,但我无法使其编辑记录。我有问题获取edit_h.php的id。当我在(" UPDATE uzytkownik SET LOGIN =:登录WHERE id = :id ")中手动输入ID号时,它可以正常工作。我现在坚持这个问题了。感谢您提供任何建议方面的帮助。

这是我的代码:

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>

1 个答案:

答案 0 :(得分: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的第一名)!