0

I am newbie in PHP code. I have create a input text to search the wanted data from database and also a delete button to delete the selected data from page. All function works fine but after I click the delete button the selected data was deleted and came out this error.

Notice:Undefined index: search in C:\xampp\htdocs\FinalProject\search.php on line 114

Here is my code:

search.php

<?php 
$con =mysqli_connect("localhost","root","","reservation");
$set =$_POST['search'];
if ($set) {
$show = "SELECT * FROM reserve WHERE Username = '$set'";
$result = mysqli_query($con, $show);
while ($row=mysqli_fetch_array($result)){ ?>
<tr>
            <td><?php echo $row['ID']; ?></td>
            <td><?php echo $row['Username']; ?></td>
            <td><?php echo $row['Person']; ?></td>
            <td><?php echo $row['Book_date']; ?></td>
            <td><?php echo $row['Book_time']; ?></td>
            <td><?php echo $row['Table_no']; ?></td>
            <td><?php echo $row['Cus_acc']; ?></td>
            <td><?php echo $row['Cus_food']; ?></td>
            <td><?php echo $row['Cus_drink']; ?></td>
            <td><?php echo $row['Cus_request']; ?></td>
            <td><a class="del_btn" href="reserve_del.php?del=<?php echo 
$row['ID']; ?>">Delete</a></td>
</tr>
<?php } ?>
<?php } ?>

reseacrh_del.php

<?php
$con = mysqli_connect('localhost','root','','reservation');
if (isset($_GET['del'])) {
        $ID = $_GET['del'];
        mysqli_query($con, "DELETE FROM reserve WHERE ID=$ID");
        $_SESSION['msg'] = "Delete Successful.";
        header("location: search.php");
    }       
?>

Some one help me please.. Thank you

Amit Gupta
  • 2,771
  • 2
  • 17
  • 31
Aladabun
  • 27
  • 1
  • 7
  • you are getting this notice because after deleting when you redirect to your search page, it requires `$_POST['search']` variable, which is in this case is missing – pravindot17 Dec 11 '17 at 11:26
  • lazy way : `$set = @$_POST['search'];` – Faytraneozter Dec 11 '17 at 11:27
  • Your queries are not secure because you are feeding them raw user-submitted data. Use prepared statements with placeholders for security reasons. – mickmackusa Dec 11 '17 at 20:42

2 Answers2

0

Test the existance of data in $_POST variable before to affect $_POST['search'] on $test :

    <?php 
$con =mysqli_connect("localhost","root","","reservation");

if (!empty($_POST)) {
$set =$_POST['search'];
$show = "SELECT * FROM reserve WHERE Username = '$set'";
$result = mysqli_query($con, $show);
while ($row=mysqli_fetch_array($result)){ ?>
<tr>
            <td><?php echo $row['ID']; ?></td>
            <td><?php echo $row['Username']; ?></td>
            <td><?php echo $row['Person']; ?></td>
            <td><?php echo $row['Book_date']; ?></td>
            <td><?php echo $row['Book_time']; ?></td>
            <td><?php echo $row['Table_no']; ?></td>
            <td><?php echo $row['Cus_acc']; ?></td>
            <td><?php echo $row['Cus_food']; ?></td>
            <td><?php echo $row['Cus_drink']; ?></td>
            <td><?php echo $row['Cus_request']; ?></td>
            <td><a class="del_btn" href="reserve_del.php?del=<?php echo 
$row['ID']; ?>">Delete</a></td>
</tr>
<?php } ?>
<?php } ?>
Goms
  • 2,424
  • 4
  • 19
  • 36
0

Check this will help you: Here I am passing your username on delete page, and from there redirecting on the search page, where have to either post and get the same variable in condition search.php

<?php 
$con =mysqli_connect("localhost","root","","reservation");
$set = !empty($_POST['search']) ? $_POST['search'] : (!empty($_GET['search']) ? $_GET['search'] : null);
if ($set) {
$show = "SELECT * FROM reserve WHERE Username = '$set'";
$result = mysqli_query($con, $show);
while ($row=mysqli_fetch_array($result)){ ?>
<tr>
            <td><?php echo $row['ID']; ?></td>
            <td><?php echo $row['Username']; ?></td>
            <td><?php echo $row['Person']; ?></td>
            <td><?php echo $row['Book_date']; ?></td>
            <td><?php echo $row['Book_time']; ?></td>
            <td><?php echo $row['Table_no']; ?></td>
            <td><?php echo $row['Cus_acc']; ?></td>
            <td><?php echo $row['Cus_food']; ?></td>
            <td><?php echo $row['Cus_drink']; ?></td>
            <td><?php echo $row['Cus_request']; ?></td>
            <td><a class="del_btn" href="reserve_del.php?del=<?php echo 
$row['ID']; ?>&username=<?php echo $row['Username']; ?>">Delete</a></td>
</tr>
<?php } ?>
<?php } ?>

reseacrh_del.php

<?php
$con = mysqli_connect('localhost','root','','reservation');
if (isset($_GET['del'])) {
        $ID = $_GET['del'];
        mysqli_query($con, "DELETE FROM reserve WHERE ID=$ID");
        $_SESSION['msg'] = "Delete Successful.";
        header("location: search.php?search=".$_GET['username']);
    }       
?>
pravindot17
  • 1,199
  • 1
  • 15
  • 32