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>