简体   繁体   中英

$_GET value is not recognized in edit page

i have a simple CRUD, everything is already working except for update, i cant seem to pass the $_GET value from my href to my update.php page. any idea what im doing wrong?

update and delete href's

<td><a href="update.php?updateid=<?php echo $row['stud_id'];?>">Edit</a></td>
<td><a href="index.php?deleteid=<?php echo $row['stud_id'];?>">Delete</a></td>

update.php

<?php
    include 'db.php';


    if(isset($_GET['updateid'])){
        $id = $_GET['updateid'];
    }

    $select = "SELECT * FROM student WHERE stud_id='".$id."'";
    $user = selectStud($select);

    if(isset($_POST['up-submit'])){
        $upfname = $_POST['up-fname'];
        $upmname = $_POST['up-mname'];
        $uplname = $_POST['up-lname'];
        $upcourse = $_POST['up-course'];

        $query = "UPDATE student SET stud_fname='".$upfname."',stud_mname='".$upmname.
        "',stud_lname='".$uplname."',stud_course='".$upcourse."' WHERE stud_id='".$id."'";

        $update = updateStud($query);

        if($update){
            echo "<script>alert('Update Success!'); window.location('index.php');</script>";
        }else{
            echo "<script>alert('Update Failed :('); window.location('index.php');</script>";
        }
    }


?>

<form method="POST" action="update.php">
    <input type="text" name="up-fname" placeholder="First Name" autocomplete="off"><br />
    <input type="text" name="up-mname" placeholder="Middle Name" autocomplete="off"><br />
    <input type="text" name="up-lname" placeholder="Last Name" autocomplete="off"><br />
    <label for="course">Course: </label>
    <select name="up-course">
        <option name="up-course" value="BSIT">BSIT</option>
        <option name="up-course" value="BSHM">BSHM</option>
        <option name="up-course" value="BSED">BSED</option>
        <option name="up-course" value="BSMT">BSMT</option>
    </select><br />

    <input type="submit" name="up-submit" value="Save Changes">
</form>

db.php

<?php 

    function getConnection(){

        $conn = new PDO("mysql:host=localhost;dbname=preskilltest","root", "");
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $conn;
    }   

    function selectStud($query){
            $pdo = getConnection();

            $stmt = $pdo->query($query);
            return $stmt->fetchAll();
    }

    function insertStud($query){
        $pdo = getConnection();

        $stmt = $pdo->query($query);
        return $stmt;

    }

    function updateStud($query){
        $pdo = getConnection();

        $stmt = $pdo->prepare($query);
        return $stmt->execute();
    }


?>

Since you can't access GET parameters (URL) when you send a POST request (form) you need to embed the GET parameters in the form.

So add the following to the form:

<input type="hidden" name="updateid" value="<?php echo $id; ?>>

And have the following on the server side's code:

 if(isset($_POST['up-submit'])){
        $id = $_POST['updateid']; //add this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM