I have a Delete.php page that deletes records based on their ID.
When there is an ID, i.e., Delete.php?id=3610, all is well, and it functions as expected.
If I just go to "Delete.php" and that's it - no ID, it generates:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
From the little I understand, it is doing this because I am trying to pass a nonexistent variable into my query.
I have been trying to put if (empty($_POST['id'])) { } in different places, which removes the error, but breaks something else.
Here is my code:
  <?php
require_once 'functions.php';
$conn = mysqli_connect("localhost", "user", "pass",'db');
writeHead("Delete Track");
if (isset($_POST['delete'])) {
    $trkid = $_POST['trkid'];
    $query = "DELETE FROM track WHERE TrackID=$trkid";
    mysqli_query($conn, $query) or die(mysqli_error($conn));
        if (mysqli_affected_rows($conn)>0) {
            header("Location: Display.php?action=deleted&id=$trkid&status=deleted");
            exit();
        }
        echo "<p class='error'>Unable to update record</p>";
} else {
    if (!isset($_GET['id'])) {
        echo "<p class='error'>No Track ID provided.<br><a href='Display.php'>Return to display page.</a><p>";
    }
    $trkid=$_GET['id'];
    $query = "SELECT * FROM track WHERE TrackID=$trkid";
    $result = mysqli_query($conn,$query);
    if (!$result) {
        die(mysqli_error($conn));
    }
    if (mysqli_num_rows($result)> 0) {
        $row = mysqli_fetch_assoc($result);
        $Name=$row['Name'];
        $Album=$row['AlbumId'];
        $Composer=$row['Composer'];
        $Milli=$row['Milliseconds'];
        $Bytes=$row['Bytes'];
        $UnitPrice=$row['UnitPrice'];
    } else {
        echo "<p class='error'>Unable to retrieve Track $trkid.<br><a href='Display.php'>Return to display page.</a>";
    }
} 
?>
    <p>Track Information:</p>
    <p><?php echo "<b>ID: $trkid <br>Title: $Name</b>"; ?></p>
        <form method="post" action="Comp3Delete.php">
        <p>
            <input type="hidden" name="trkid" value="<?php echo $trkid; ?>">
            <input type="submit" name="delete" class="btn" value="Confirm Delete">
        </p>
    </form>
    <p>Return to <a href="Comp3Display.php">Track Table Display</a></p>
    <?php writeFoot(); ?>
 
     
    