I can't get the users name to get updated or to deleted the user. Whatever i try doesn't work. When i click on the update button it just goes to the index page and nothing happens and the same happens when i click on the delete button too.

this is the codes i have on my server.php page:

<?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');
  }

?>

And this is what i have on my update page:

<?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>

and this is what i have on my delete page:

<?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>

I have tried to check on youtube videoes and read many articles but i just cant seem to get anywhere so i am totaly stuck and appreciate all the help i can get. Hope that you can help me

0 Answers