我如何使我的网站更新或删除用户?

时间:2019-05-03 12:41:22

标签: php

我无法获取用户名来更新或删除用户。无论我尝试什么都行不通。当我单击更新按钮时,它会转到索引页面,什么也没有发生,并且当我也单击删除按钮时,也会发生相同的情况。

这是我在server.php页面上的代码:

<?php
session_start();

// initializing variables
$idmedlemmer = "";
$brukernavn = "";
$email    = "";
$fornavn  = "";
$etternavn = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect("localhost", "root", "", "mymusic");

// Update User
if (isset($_POST['update'])) {
  // receive all input values from the form
  $brukernavn = mysqli_real_escape_string($db, $_POST['brukernavn']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($brukernavn)) { array_push($errors, "Feltet kan ikke være tomt"); }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT brukernavn FROM medlemmer LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['brukernavn'] === $brukernavn) {
      array_push($errors, "Brukernavn eksisterer allerede");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {

    mysqli_query($db, "UPDATE medlemmer SET brukernavn='$brukernavn' WHERE idmedlemmer=$idmedlemmer");
    $_SESSION['message'] = "Brukernavnet har blitt oppdatert";
    header('location: index.php');
  }
}

// ... 
// ... 

// Delete User
if (isset($_GET['brukernavn']))
if (isset($_GET['slett'])) {

    $query = sprintf("DELETE FROM medlemmer WHERE idmedlemmer='%s'");
    mysqli_query($db, $query);
    $_SESSION['brukernavn'] = $brukernavn;
    $_SESSION['success'] = "Bruker har blitt slettet";
    header('location: Login.php');
  }

?>

这就是我的更新页面上的内容:

<?php include('server.php')?>
<?php 
  if (isset($_GET['update'])) {
    $brukernavn = $_GET['update'];
    $record = mysqli_query($db, "SELECT * FROM medlemmer WHERE brukernavn=$brukernavn");

    if (count($record) == 1 ) {
      $n = mysqli_fetch_array($record);
      $name = $n['brukernavn'];
    }
  }
?>
<html>
<head>    
    <title>Edit Data</title>
    <link rel="stylesheet" type="text/css" href="stilark.css">
  <meta charset="utf-8">
</head>

<body>    
    <form name="form1" method="post" action="index.php">
        <table border="0">
            <tr> 
                <td>Name</td>
                <td><input type="text" name="brukernavn" value="<?php echo ($_SESSION['brukernavn']); ?>"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="idmedlemmer" value="<?php echo $idmedlemmer;?>"></td>
                <td><input type="submit" name="update" value="update"></td>
            </tr>
        </table>
    </form>

这就是我在删除页面上显示的内容:

<?php include('Server.php')?>
<!DOCTYPE html>
  <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="stilark.css">

<head>    
    <title>Slett Bruker</title>
</head>

<body>
    <a href="index.php">Tilbake</a>
    <br/><br/>

    <form name="form1" method="post" action="index.php">
        <table border="0">
            <tr>
                <td><input type="hidden" name="idmedlemmer" value="<?php echo $_GET['idmedlemmer'];?>"></td>
                <td><input type="submit" name="slett" value="slett"></td>
                <a href="server.php?del=<?php echo $row['id']; ?>" class="del_btn">Delete</a>
            </tr>
        </table>
</body>
</html>

我尝试查看youtube视频并阅读了许多文章,但我似乎什么都没走,所以我完全陷于困境,并感谢我能获得的所有帮助。希望你能帮助我

0 个答案:

没有答案