Search code examples
phphttp-redirectslug

Redirecting to page with a slug


First off, I'm not using any PHP frameworks nor do I currently have an htaccess file. I plan to add an htaccess file soon once I'm complete.

I'm trying to have my page redirect to a variant of a page using a slug after pressing a submit button.

The page's url is: abc.php?user=$username. The page has an edit button where the user can edit the contents of the table's row. I started off trying to just update an image using the code shown below:

else{  
    $username = $_GET['user'];

    $users = $db->prepare("
        SELECT * FROM users WHERE username = :username LIMIT 1
    ");
    $users->bindParam(':username', $_SESSION['user']['username']);
    $users->execute(['username' => $username]);
    $users = $users->fetch(PDO::FETCH_ASSOC);

if(isset($_FILES['files'])) {       // uploads picture to the database 
            foreach($_FILES['files']['name'] as $file => $name) {
                $filename = $name;
                try{
                    if(move_uploaded_file($_FILES['files']['tmp_name'][$file],'uploads/'.$filename)) {
                        $stmt = $db->prepare("UPDATE users SET image = :image WHERE id = :id");
                        $stmt->bindParam(":image", $filename);
                        $stmt->bindParam(":id", $_SESSION['user']['id']);
                        $stmt->execute();
                    }
                } catch(Exception $e) {
                    echo $e;
                } 
            }
        }

My problem is that when I submit the changes through the edit page, it redirects to abc.php without any slug. I have tried adding

header("Location: abc.php?user=".$username);

Question: How can I get it to redirect to the page with a slug?


Solution

  • Oh, I figured out that it was just the action in my form(not shown).

    For anyone experiencing this issue, the form should be looking like this:

    <form action="abc.php?user=<?php echo $username; ?>" method="POST">
    
    </form>