Search code examples
javascriptphpmysqlerror-handlingsql-delete

Php delete function javascript/redirect


Looked around at other answers but still unable to resolve my problem. Users are currently on crud/view.php. Clicking the 'Delete' button will bring up a JavaScript textbox asking "Are you sure" and taking them to crud/delete.php.

View.php session start:

<?php 

session_start();

if(isset($_SESSION['u_uid']))

    $uid = $_SESSION['u_id'];

    require_once('connect.php');

    $ReadSql = "SELECT * FROM `contact` WHERE users_id=$uid ORDER BY Name";
    $res = mysqli_query($connection, $ReadSql);

?>

This is my delete button on view.php

<td> <input type="button" onClick="deleteme(<?php echo $r['u_uid']; ?>)" name="Delete" value="Delete"></td>

Followed by the JavaScript message also on view.php

 <script language="Javascript">
 function deleteme(delid) {
 if(confirm("Are you sure you want to Delete?")){
window.location.href='delete.php?del_id='  
  }
 } 
 </script>

My delete.php is where I think the problem is

 <?php
  session_start();
  if(isset($_SESSION['u_uid'])){

  require_once('connect.php');

  $query = "DELETE from contact WHERE id=".$GET['del_id']." LIMIT 1"; 
  $result = mysqli_query($con, $query);  
  header('Location: crud/view.php');  

?>

Really appreciate the help. I'm a pleb.


Solution

  •  <?php
     session_start();
     if(isset($_SESSION['u_uid'])) {
    
     require_once('connect.php');
    
     $query = "DELETE from contact WHERE id=".$_GET['u_uid']." LIMIT 1"; 
     $result = mysqli_query($con, $query);  
     header('Location: view.php');  
     }
    
     ?>