duanan1228 2016-09-14 23:31
浏览 53
已采纳

获取ID以编辑记录PHP + Oracle

I have php pages that let me view, add, delete records in database, but I can't make it work to edit record. I have problem to get id to edit_h.php. When i enter id number manually in ("UPDATE uzytkownik SET LOGIN = :login WHERE id = :id") it works fine. I am stuck with this problem for a bit now. Thanks for any help in advice.

Here's my code:

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条回答 默认 最新

  • dougua3706 2016-09-14 23:48
    关注

    Two possibilities here:

    You have id stored in session already, so you could just get it from there:

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

    Or you pass it along the various scripts:

    // 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!
    

    Note, that it's not such a good idea to pass a plain id through scripts, as it could easily be hacked. So option 1 would be the better one (depending on how you get the id in first place)!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog