-2

I have a form that brings in content from a database and I want the user to be able to select a student and delete it from the database. I get a error saying UNDEFINED INDEX. I am new to php. Thanks in advance for any help here is my code

  <section> 
                    <form action="include/borrar2.php" method="post">
                    <center>
                        <?php
                            $sql="select * from registro";
                            $res=mysql_query($sql);
                            echo "<table border='1' class='tabla'>";
                            echo"<thead>";
                            echo"<tr>";     

                                echo"<th class='op'>"."ID"."</th>";
                                echo"<th class='op'>"."Detalle"."</th>";
                                echo"<th class='op'>"."Costo"."</th>";
                                echo"<th class='op'>"."&nbsp;"."</th>";
                                echo"<th class='op'>"."&nbsp;"."</th>";                                                         
                            echo"</tr>";
                        echo"</thead>";
                            while ( $row = mysql_fetch_array($res)) {
                                echo "<tr>";                                
                                echo "<td name='id'>".$row['id']."</td>";
                                echo "<td>".$row['detalle']."</td>";
                                echo "<td>".$row['costo']."</td>";
                                echo "<td class='opcionT op' >"."<a href=''>"."<input type='submit' value='Actualizar'  class='btn2'>"."</a>"."</td>";
                                echo "<td class='opcionT op' >"."<a href=''>"."<input type='submit' value='Eliminar' name="eliminar" class='btn2'>"."</a>"."</td>";                                                             
                                echo "</tr>";
                            }
                            echo "</table>";

                        ?>  
                    </center>
                </form>
                </section>

php

if (isset($_POST['eliminar'])) {
    $idrow = $_POST['id'];
$sql="DELETE FROM `agenda` WHERE `id` = $idrow";
$res=mysql_query($sql);
if($res){
    echo '<script>alert("Datos Registrados..")</script>';
    echo "<script>location.href='../index.php'</script>";
}else{
    echo "Error :(" ;
}
}
Natasha
  • 93
  • 2
  • 9

1 Answers1

1

You're trying to pass the id via a td element. That won't work.

echo "<td name='id'>".$row['id']."</td>";

You need to put it in a form element, even if that element is hidden.

I'd suggest that you use a GET approach to this instead. By passing the id to your deleting php script:

echo "<a href='delete.php?id={$row['id']}'>Delete this Student</a>";

Then in your php script, you'd just change to use the $_GET['id']:

if (!empty($_GET['id'])) {
    $idrow = intval($_GET['id']);
    $sql="DELETE FROM `agenda` WHERE `id` = $idrow";
    $res=mysql_query($sql);
    if($res){
        echo '<script>alert("Datos Registrados..")</script>';
        echo "<script>location.href='../index.php'</script>";
    }else{
        echo "Error :(" ;
    }
}

I know you're new to PHP, but you'd be best to look at using PDO or MySQLi Prepared Statements when running SQL queries, especially DELETE, due to the fact that mysql_* functions are deprecated and insecure.

Darren
  • 13,050
  • 4
  • 41
  • 79
  • Also look into sanitizing your input if you are going to use $_GET within any type of SQL queries. – Tarquin Jul 27 '15 at 01:15