0

As a beginner in PHP i am finding difficulty in editing data.

In the following code i have a page where it lists details of the users.

The 'Delete' button successfully deletes a user and the details associated with it and the 'Edit' button redirects to the edit page.

In the edit page an user's default values are listed but when i make changes and click 'Edit' it throws the error 'Undefined index:[path] on line whatever'.

Following is the code to my 'adminview.php' page where one can successfully see the list of users and their details.

<?php 
require_once 'dbreg.php';

if(isset($_GET['del']) == 'yes'){

//echo $_GET['del']; exit;
   $userid = $_GET['uid'];
   $sqlquery = "DELETE FROM regform WHERE id=".$userid."";
   mysql_query($sqlquery);
   header("Location: http://localhost/whatever/adminview.php");
}

if(isset($_GET['edit']) == 'yes'){

//echo $_GET['del']; exit;
   $userid = $_GET['uid'];

   header("Location: http://localhost/whatever/adminedit.php");
}

?>

<!DOCTYPE html>
<head>
    <title>User Listing</title>
</head>
<body>
    <table border="1">
        <tr>
            <td>Firstname</td>
            <td>Lastname</td>
            <td>Gender</td>
            <td>Address</td>
            <td>Country</td>
            <td>Actions</td>
        </tr>

        <?php
        $query = mysql_query("SELECT * FROM regform");
        //use the if line below or directly the while, here only the while is used
        //if($query > 0) {
        while ($result = mysql_fetch_assoc($query)) {
                      $userId = $result['id'];

                   ?>
            <tr>

                <td><?php echo $result['firstname'];?></td>
                <td><?php echo $result['lastname'];?></td>
                <td><?php echo $result['gender'];?></td>
                <td><?php echo $result['address'];?></td>
                <td><?php echo $result['country'];?></td>               
                <td><a href="#">View</a> /<a href="http://localhost/whatever/adminedit.php?uid=<?php echo $userId; ?> && edit='yes'"> Edit</a> / <a     href="adminview.php?uid=<?php echo $userId; ?>&&del='yes'">Delete </a>
                </td>
            </tr>

            <?php
            //}
        }


        ?>

    </body>
    </html>

and this is my page of 'adminedit.php' which is the edit page where the table shows the already inserted data. But, as i change them and click edit, it displays ' Undefined index: uid in \xampp\htdocs\whatever\adminedit.php on line 12'.

Will be highly obliged if helped. Thanks

<?php

require_once "dbreg.php";

//if(isset($_GET['id']) == 'success') do this to get the id of all the users
if(isset($_GET['msg']) == 'success'){

    echo "Data has been successfully updated";

}

$sqlquery = "SELECT username, passkey, email, firstname, lastname, gender, address, country, hobbies FROM regform WHERE id = '".$_GET['uid']."'";
$resultquery = mysql_query($sqlquery);
$row = mysql_fetch_assoc($resultquery);


if(!empty($_POST)){
    $fname = $_POST['firstname'];
    $lasttname = $_POST['lasttname'];
    $usergender = $_POST['usergender'];
    $address = $_POST['address'];
    $country = $_POST['country'];

    $updateQuery = "UPDATE regform SET firstname='".$fname."', lastname='".$lasttname."', gender='".$usergender."', address='".$address."', country='".$country."' WHERE id='".$_GET['uid']."'";


    if(mysql_query($updateQuery)){
  //echo "Data has been successfully updated";
        header("Location: http://localhost/classwork2/adminedit.php?msg='success'");
    }
    else{
        echo "Data has not updated";
    }


}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Edit Details</title>
</head>
<body>
    <table border="1">
        <form name="edit" id="edit" method="post" action="adminedit.php" enctype="multipart/form-data">
            <tr>
                <td>
                    <label>Firstname</label>
                </td>
                <td>
                    <input type="text" name="firstname" id="firstname" value="<?php echo $row['firstname'];?>">
                </td>   
            </tr>
            <tr>
                <td>
                    <label>Lastname</label>
                </td>
                <td>
                    <input type="text" name="lasttname" id="lastname" value="<?php echo $row['lastname'];?>">
                </td>
                <tr>
                    <td>
                        <label>Gender</label>
                    </td>
                    <td>
                        <input type="radio" name="usergender" value="male" <?php if($row['gender'] == 'male'){?> checked<?php }?>>Male</input>
                        <input type="radio" name="usergender" value="female" <?php if($row['gender'] == 'female'){?> checked<?php }?>>Female</input>
                        <input type="radio" name="usergender" value="other" <?php if($row['gender'] == 'other'){?> checked<?php }?>>Other</input>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Address</label>
                    </td>
                    <td>
                        <textarea name="address"><?php echo $row['address'];?></textarea> 
                    </td>
                </tr>
                <tr>
                    <td>
                        <label>Country</label>
                    </td>
                    <td>

                        <select name="country"> 
                            <option value="Abc" <?php if($row['country'] == 'Abc' ){?> selected<?php }?>>India</option>
                            <option value="Bcd" <?php if($row['country'] == 'Bcd' ){?> selected<?php }?>>Brazil</option>
                            <option value="Cde" <?php if($row['country'] == 'Cde' ){?> selected<?php }?>>Japan</option>
                        </select>

                    </td>
                </tr>   
                <tr>
                    <td>
                        <input type="submit" value="Edit">
                    </td></tr>          
                </form>
            </body>
            </html>
Abhrapratim Nag
  • 101
  • 2
  • 10
  • 2
    BTW `isset` never returns `yes` – u_mulder Oct 01 '16 at 19:56
  • You also are open to SQL injections. – chris85 Oct 01 '16 at 20:10
  • try using `if (isset($_GET['edit']))` instead. `isset()` returns a boolean, not a string. http://php.net/manual/en/function.isset.php – Nathan F. Oct 01 '16 at 20:32
  • can you tell me where @NathanFiscaletti – Abhrapratim Nag Oct 01 '16 at 20:44
  • @AbhrapratimNag You're currently using `if(isset($_GET['msg']) == 'success'){`. Change that out to `if (isset($_GET['edit']) && $_GET['edit'] == 'success')` – Nathan F. Oct 02 '16 at 00:15
  • **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Oct 02 '16 at 02:16

0 Answers0