无法选择特定的ID来编辑PHP中的数据

时间:2019-03-09 16:56:02

标签: php mysql

hal1 / view.php

<?php
include "koneksi.php";
include "cek.php";

$result = $koneksi->query("select * from pengunjung");
$baris = $result->fetch_assoc();

echo "Halaman Admin<br>";
echo "Nama Anda adalah : ".$_SESSION['namauser']. "<br><br>";
echo "<li><a href = edit.php?userid=$baris[id]>[Edit Data Diri]</a></li><br>    
      <li><a href=logout.php> Logout</a></li>";
?>

edit.php

<?php
include("koneksi.php");
include("cek.php");

$id=$_GET['userid'];

$query = "select * from pengunjung where id=$id";
$result = mysqli_query($koneksi,$query);

while ($baris = mysqli_fetch_array($result))
{
    echo "<form method=post action=update.php>";
    echo "Nama : <input type=text name=nama value=$baris[1]>";
    echo "<br>";
    echo "Email : <input type=text name=email value=$baris[2]>";
    echo "<br>";
    echo "Situs : <input type=text name=situs value=$baris[3]>";
    echo "<br>";
    echo "<input type=submit name=submit value=update>";
    echo "<input type=hidden name=id value=$baris[0]>";
    echo "</form>";
}
?>

update.php

<?php
include("koneksi.php");

$id=$_POST['id'];
$nama=$_POST['nama'];
$email=$_POST['email'];
$situs=$_POST['situs'];

$query = "update pengunjung set id='$id', nama='$nama', email='$email', 
situs='$situs' where id='$id'";
$result = mysqli_query($koneksi,$query);

echo '<script language="JavaScript">alert("Data telah di update");
    document.location="edit.php"</script>';
?>

我使用登录表单,并以具有特定ID的用户身份登录。我尝试编辑登录用户的数据(nama,email,situs)。 问题是为什么它没有选择登录用户的正确数据(nama,email,situs)?每当我以其他用户身份登录并单击“编辑”时,它将始终显示数据库中第一个ID /用户的数据(nama,email,situs)。帮忙!

2 个答案:

答案 0 :(得分:1)

使用id变量

/myproject/Login/Login/Register/register.html

答案 1 :(得分:0)

hal1 / view.php

<?php
include"koneksi.php";
include "cek.php";

echo "Halaman Admin<br>";
echo "Nama Anda adalah : ".$_SESSION['namauser']. "<br><br>";
echo "  <li><a href = edit.php?userid=$_SESSION[id]>[Edit Data Diri]</a></li><br>   
        <li><a href=logout.php> Logout</a></li>";
 ?>

好的。。所以我想我通过使用会话解决了问题,在本例中为登录用户的$ _SESSION [id]。我将会话代码放在check_user_login.php文件中,所以我没有需要在hal1 / view.php中再次进行查询。

check_user_login.php

<?php
include("koneksi.php");

$username = $_POST['username'];
$password = $_POST['password'];

$query = "select * from pengunjung where username='$username' and password='$password'";
$data = mysqli_query($koneksi,$query) or die ("Couldn't execute query");
$hasil = mysqli_fetch_assoc($data);

if ($username=="" || $password=="")
{
    echo '<script language="JavaScript">alert("Username atau Password Kosong");
    document.location="index.php"</script>';
}
else
    if($username==$hasil['username'] and $password==$hasil['password'])
    {   
        session_start();
        $_SESSION['id'] = $hasil['id'];
        $_SESSION['namauser'] = $username;
        header("location:hal1/view.php");
else
{
    echo '<script language="JavaScript">alert("Username atau Password Salah");
    document.location="index.php"</script>';
}