-1

I'm still new at php (just started yesterday) and when I wanted to delete my data it says this
Notice: Undefined index: bookname in C:\wamp64\www\HelpingClass\delete.php on line 13
Notice: Undefined index: quantity in C:\wamp64\www\HelpingClass\delete.php on line 14
Notice: Undefined index: price in C:\wamp64\www\HelpingClass\delete.php on line 15

and this is my code for line 13,14,15

$bookname = $_POST['bookname'];
$quantity = $_POST['quantity'];
$price = $_POST['price'];

I even try to change those 13,14,15 lines to this

    if(isset($_POST['bookname']) && isset($_POST['bookname'])){
    echo $_POST['bookname'];
}
    if(isset($_POST['quantity']) && isset($_POST['quantity'])){
    echo $_POST['quantity'];
}
    if(isset($_POST['price']) && isset($_POST['price'])){
    echo $_POST['price'];}

the notice undefined index are gone but i still can't delete the data :(

this is my full code for delete.php

<?php
    include ('connection.php');
    session_start();
    if (!isset($_SESSION ['userid'])&& empty($_SESSION['userid'])){
        header ("location:login.php");
        exit;
    }
    else {
        $userid = $_SESSION ['userid'];
    }

    $bookid = $_GET['bookid'];
    $bookname = $_POST['bookname'];
    $quantity = $_POST['quantity'];
    $price = $_POST['price'];

    $query = mysqli_query ($conn, "DELETE FROM book SET WHERE Book_Id='$bookid';");

    if ($query == TRUE)
        {
            echo "<script language='javascript'>";
            echo "alert('Deleted.');";
            echo "window.location.href='action.php';";
            echo "</script>";
        }
        else
        {
            echo "<script language='javascript'>";
            echo "alert('Delete Failed.');";
            //echo "window.location.href='action.php';";
            echo "</script>";
        }


 ?>

I really do appreciate your help

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
nh. m
  • 3
  • 6

1 Answers1

4

There are quite a few issues in your code.

An example of parametrized version could be:

// prepare the query statement
$stmt = $mysqli_prepare($conn, "DELETE FROM book 
                                WHERE Book_Id = ?");
// bind the parameters
$stmt->bind_param("i", $bookid);
// execute the query
$stmt->execute();
//fetching result would go here (in case of SELECT queries)
$stmt->close();
  • Secondly, check if a variable exists or not, using isset function. You can use ternary operators alongside.

eg:

$bookid = isset($_GET['bookid']) ? $_GET['bookid'] : 0;
$bookname = isset($_POST['bookname']) ? $_POST['bookname'] : '';
$quantity = isset($_POST['quantity']) ? $_POST['quantity'] : 0;
$price = isset($_POST['price']) ? $_POST['price'] : 0;
  • Thirdly, your DELETE query statement syntax is wrong. you cannot use * and SET. Also, do (int) typecasting on the $bookid (assuming it to be integer here). You also don't need a semicolon (;) in the query string, when using mysqli_query function.

It should be as follows:

$query = mysqli_query ($conn, "DELETE FROM book 
                               WHERE Book_Id = '" . (int)$bookid . "'");

From MySQL Documentation, syntax of DELETE statement is as follows:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57