2
Hello
I am building a content manager and I have a page where I return the results of the chosen country, so I can then update each field.
When choosing each country on the Countries.php page, I send the parameter to the Edit-Countries.php page. 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 of the Edit-Paises.php page
<?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 of the respective country. Because this error appears if it is not involved in the form submission?
Edit
I pass the 'Edit' parameter through this block of code on the Countries page.php:
<?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>';
}
}
?>
I don’t see in any corner you setting the value of the parameter
edit
. Nor in the form nor how query stops.– Jefferson Quesado
I saw:
país
(with the same accent),continente
,bandeira
,imagem
andintro
. All parameters passed by the form– Jefferson Quesado
Dear @Jeffersonquesado I edited my question above. The Edit parameter comes from the page Countries.php, to show the results in editPaises.php. So far so good, except when sending the form.
– Fabio
Which link is the
form
directs? I particularly don’t know what the$_SERVER['PHP_SELF']
returns. If it returns only the path, without the param query that is passed by the previous page, then we have to parameter$_GET
is only filled in the first time you open the page, but not when submitting to the form. That’s why I said I didn’t see the$_GET
through your form– Jefferson Quesado
An alternative that I have seen in some JSP projects (and that could be used in your project) is the form being submitted to a URL that will process the data, with possible database insertions, to then redirect to the successful or error page (or, success and error are the same page, pass the appropriate parameters for its presentation)
– Jefferson Quesado
I have done this by putting the processing of the form on another page but by submitting the form the page returns white. I’m probably not doing this line of my query right: "WHERE country_id = ?" I’ll have to identify a way for the code to recognize which parents are editing.
– Fabio
Regarding the form action, the $_SERVER['PHP_SELF'] is to continue on the same page and process the form.
– Fabio
"The problem is when I submit the form I get the following error", the answer is: as you do not save the
$_GET['edit']
nowhere, by resubmitting the form in the POST method, the content of Edit no longer exists, so there is an indefinite Indice. To solve this, you can treat using aIF isset($_GET['edit'])
or resubmit via Form viaPOST
, so the content should be recovered through the$_POST['edit']
– Don't Panic