Click here to Skip to main content
14,028,484 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I am new to php and (Its probably a silly question ).I am trying to make a CRUD application.I am getting error when I try to get the value "$countryName"
Notice: Undefined variable: countryName 
if(isset($_GET["edited"])){
$country=$helper->fetchCountry($_GET["countryId"]);
$countryName=$country["countryName"];
  $helper->updateCountry($countryName);
    helper::redirect("?updated=1");

}
<form action="" method="post">
               <div class="form-group">
                   <label>Ülke Adı</label>
                   <input type="text" class="form-control" name="editName"  value="<?php echo $countryName;?>"> HERE
               </div>
               <div class="form-group" align="center">
                   <button  class="btn btn-primary" name="btnEdit">Edit</button>
               </div>
           </form>
this what I have tried so far

What I have tried:

<?php
include "system/config.php";
include "system/helper.php";

$conn=new baglanti();
$helper=new helper();
?>
<!doctype html>
<html lang="en">
<head>
  
</head>
<body>
<?php
$data=$helper->fetchAllData();

if(isset($_POST["btnAdd"])){
   $name=strip_tags($_POST["Name"]);
   $helper->addData($name);
   helper::redirect("?inserted=1");



}
if(isset($_GET["edited"])){
$country=$helper->fetchCountry($_GET["countryId"]);
$countryName=$country["countryName"];
  $helper->updateCountry($countryName);
    helper::redirect("?updated=1");

}
?>
<div class="container">

    <div class="row">
       <div class="col-md-8">

           <form action="" method="post">
               <div class="form-group">
                   <label>Ülke Adı</label>
                   <input type="text" class="form-control" name="editName"  value="<?php echo $countryName;?>">
               </div>
               <div class="form-group" align="center">
                   <button  class="btn btn-primary" name="btnEdit">Edit</button>
               </div>
           </form>

           <form action="" method="post">
               <div class="form-group">
                   <label>Ülke Adı</label>
                   <input type="text" class="form-control" name="Name">
               </div>
               <div class="form-group" align="center">
                   <button  class="btn btn-success" name="btnAdd">Ekle</button>
               </div>
           </form>
       </div>
        <table class="table table-striped">
            <tr>
                <td>Ülke Adi</td>
                <td>Düzenle</td>
                <td>Sil</td>
            </tr>
             <?php
             if(count($data)>0){
                 foreach ($data as $key=>$value){
                     ?>
                     <tr>
                         <td><?php echo $value["countryName"] ;?></td>
                         <td><a href="?edited=1&countryId=<?php echo $value["countryId"];?>">Düzenle</a> </td>
                         <td><a href="?deleted=1&countryId=<?php echo $value["countryId"];?>">Sil</a> </td>

                     </tr>
                     <?php
                 }

             }else{
                 ?>
                 <tr>
                     <div class="row">
                         <td colspan="3" class="">No Data</td>
                     </div>
                 </tr>
            <?php
             }

             ?>
        </table>
    </div>
</div>
</body>
</html>
Posted
Updated 11-Feb-19 1:11am
v2
Comments
Richard MacCutchan 8-Feb-19 9:21am
   
Probably because the line:
$countryName=$country["countryName"];

did not return any data. You need to do some debugging to find out why.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

1. Is that $_GET["edited"] or $_POST["edited"]? How it should be? I see the form's method is "post". Then it should be $_POST["edited"]. Try to change this and see if it works.

2. Also there's a chance for PHP to skip this portion if it's not set, meaning "edited" is not set. So, add the else part for isset($_GET["edited"]) like below:

else
{
$countryName = "";
}

This is to set a blank as default, if not edited.

3. Also I can't understand from your code, how the "edited" mode is handled to identify this record is edited? Anyway the above two options can work.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190419.4 | Last Updated 11 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100