Undefined index: user error while submitting a POST form

1

Html

<html>
<body>
<form action="encomenda_apagar.php" method="POST">
 <br><br>
 <label class="sr-only" for="inputHelpBlock"></label>
 <b>INSIRA O SEU USERNAME:</b> <br><br>
 <input type="text" name="user">
 <br>
 <center><input type="submit" value="Eliminar"></center>
 <br>
 </form>
 </body>
 </html>

PHP

<script language="javascript">
<!--
function myFunction(a) {
    alert(a);
}

</script>

<?php

ini_set ('default_charset','utf-8');

$link = mysqli_connect("localhost", "root", "", "bd_calcadocharme");

$id=$_GET['user'];

$sql="DELETE FROM cliente WHERE user='$id'";

$result = mysqli_query($link,$sql);

if ($result && mysqli_affected_rows($link)){
    echo "<script> myFunction('Cliente elimado com sucesso'); </script>";
        header('refresh:0 ; url=escrever.html');
} elseif ($result && !mysqli_affected_rows($link)){
    echo "<script> myFunction('Esse cliente nao existe !'); </script>";
        header('refresh:0 ; url=escrever.html');
} elseif (!$result){
    echo "<script> myFunction('Erro na query!'); </script>";
        header('refresh:0 ; url=escrever.html');
}

?>
  

Errors: Notice: Undefined index: user in F: \ XAMPP \ htdocs \ PAPBRUNO \ PAPBRUNO \ order_apagar.php on line 15

And I do not understand why.

When I try to delete someone who does not exist, the "query error" message appears and MUST APPEAR "This client does not exist" And even when I put someone that exists, it does not delete

    
asked by anonymous 15.06.2015 / 01:05

3 answers

3

Your form is using method="POST" , just change $id=$_GET['user']; to $id=$_POST['user'];

I also recommend that you avoid passing variables directly to your query, in case you are using mysqli so you can try using bind_param , example:

$id = $_POST['user'];

$link = mysqli_connect('localhost', 'root', '', 'bd_calcadocharme');

if (false === $link) {
    printf('Erro de conexão: %s\n', mysqli_connect_error());
    exit;
}

$stmt = mysqli_prepare($link, 'DELETE FROM cliente WHERE user=?');
mysqli_stmt_bind_param($stmt, 'i', $id);

/* Executa a query */
mysqli_stmt_execute($stmt);

//Fecha stmt
mysqli_stmt_close($stmt);

//Fecha conexão
mysqli_close($link);
    
15.06.2015 / 06:33
2

As Matheus Velloso spoke, change the

<form action="encomenda_apagar.php" method="POST">

To

<form action="encomenda_apagar.php" method="GET">
    
15.06.2015 / 06:19
1

Your form is in the POST parameter and you are waiting, according to your script, for a $ _GET parameter. So it's giving an undefined.

    
15.06.2015 / 04:49