SELECT AND UPDATE - Undefined index when submitting form

2

Hello

I am building a content manager and I have a page where I return the results of the chosen country, so that I can then update each field.

By choosing each country on the Paises.php page, I send the respective parameter to the edit page Paises.php. The results of each column appear normally and so far so good. The problem is when I submit the form I get the following error:

  

Notice: Undefined index: edit in C: \ xampp \ htdocs \ BackOffice \ edit-Paises.php on line 5

Here is the code for the edit page Paises.php

<?php
require_once 'db.php';
include 'constants.php';

$edit = $_GET['edit'];

$stmt = $mysqli->prepare("SELECT country_id, country_name, flag_file, 
header_img, intro FROM countries WHERE country_id = ? ORDER BY country_id");
$stmt->bind_param('s', $edit);
$stmt->execute();
$stmt->bind_result($id, $countryName, $flag, $headerIMG, $intro);
$stmt->store_result();
if($stmt->fetch()) {
if(!$stmt) {
    echo "Failed to execute: (" . $stmt->errno . ")" . $stmt->error;
}     
$stmt->close();
}

//Form for the country update    
$paisError = "";
$flagError = "";
$imgError = "";
$introError = "";
$contError = "";

$update_country = $_GET['edit'];

if(isset($_POST['btnEdit'])) {

$país = $_POST['país'];
$intro = $_POST['intro'];
$continente = $_POST['continente'];
$bandeira = $_POST['bandeira'];
$imagem = $_POST['imagem'];

if(empty($país)) {
    $paisError = "Insira um país";
} elseif(empty($intro)) {
    $introError = "Insira uma descrição";
} elseif(empty($continente)) {
    $contError = "Escolha o respectivo continente";
}  elseif(empty($bandeira)) {
    $flagError = "Insira uma bandeira";
}  elseif(empty($imagem)) {
    $imgError = "Insira uma imagem";
}  else {

$stmt_update = $mysqli->prepare("UPDATE 'countries' SET 'country_name' = ?, 'id_continentes' = ?, 'flag_file' = ?, 'header_img' = ?, 'intro' = ? WHERE 'country_id' = '".$_GET['edit']."' ");
$stmt_update->bind_param('s', $update_country);
$stmt_update->execute();

if(!$stmt_update) {
    echo "Failed to execute: (" . $stmt->errno . ")" . $stmt->error;
}     
$stmt->close();
header('Location: thankyou.php');
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Países</title>
    <script src="js/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script src="js/imgPrev.js" type="text/javascript"></script>
    <link href="css/editPais.css" rel="stylesheet" type="text/css"/>
</head>
<body>
    <?php
    include_once 'Menu.php';
    ?>
    <h1 id="heading">Editar País</h1>
        <form action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
            <div class="editContent">
            <label for="Pais">País</label>
            <input type="text" name="país" value="<?php echo $countryName; ?>">
            <span class="error"><?php echo $paisError; ?></span>
            </div>

            <div class="editContent">
            <label for="Pais">Continente</label>
            <select type="text" name="continente">
                <?php
                $sql = "SELECT * FROM continentes ORDER BY id";
                $result = mysqli_query($mysqli, $sql);
                     while ($row = mysqli_fetch_assoc($result)) {
                     echo '<option value="'.$row['id'].'">'.$row['continente'].'</option>';
                          }
                ?>
            </select>
            <span class="error"><?php echo $contError; ?></span>
            </div>

            <div class="editContent">
            <label for="Pais">Bandeira</label>
            <img id="img-A" src="<?php echo $site_root;?>/images/16/<?php echo $flag; ?>"/>
            <input type="file" id="imgInp" name="bandeira">
            <span class="error"><?php echo $flagError; ?></span>
            </div>

            <div class="editContent">
            <label for="Pais">Imagem País</label>
            <img id="img-B" src="<?php echo $site_root;?>/images/country_header_images/<?php echo $headerIMG; ?>"/>
            <input type="file" id="imgP" name="imagem">
            <span class="error"><?php echo $imgError; ?></span>
            </div>

            <div class="editContent">
            <label for="Pais">Texto Intro</label>
            <textarea name="intro" rows="5"><?php echo $intro;  ?></textarea>
            <span class="error"><?php echo $introError; ?></span>
            </div>
            <div class="editContent">
            <button type="submit" id="btn_edit" name="btnEdit">Submit</button>
            </div>
        </form>         
</body>

The $ edit variable returns the data for the respective country. Why does this error appear if you are not involved in submitting the form?

Edit

Pass the 'edit' parameter through this code block on the Paises.php page:

<?php
         $query = "SELECT country_id, country_name FROM countries ORDER BY country_name";
         $result = mysqli_query($mysqli, $query);
         if(mysqli_num_rows($result) == 0) {
             echo '<tr><td cols="4">No rows returned</td></tr>';
         } else {
         while ($row = mysqli_fetch_assoc($result)) {
             echo '<tr><td>- '.$row['country_name'].'</td><td><a href="edit-Paises.php?edit='.$row['country_id'].'">Edit</a>|<a href="">Delete</a></td></tr>';
                }
         }
         ?>
    
asked by anonymous 11.09.2017 / 19:46

2 answers

0

This problem is occurring, because the first time you call the script you are passing the country id through the edit attribute in the URL, but when calling the script again to update the data, in the action of your form attribute edit is not being sent because the $_SERVER['PHP_SELF'] property returns only the script name, not the URI attributes.

Replace action with your form as follows:

action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"

by

action="<?PHP echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>"

    
11.09.2017 / 20:20
1

Just like you did:

<input type="text" name="país" value="<?php echo $countryName; ?>">

One must be done:

<input type="hidden" name="cod_pais" value="<?= $_GET['edit']; ?>">
<!-- note que mudei o type de text para hidden, pois não precisa exibir-->

When doing this, when submitting the form, there will also be $_POST['cod_pais'] which is the same as $_GET['edit´] ... and I noticed here now that you also put the name country with an accent. This is not good practice, use accentuation.

From then on, I think you've got what you have to do right now.

    
11.09.2017 / 20:03