php - Multiple delete through checkout

326

I am stuck in one of my application module. I have to set multiple delete options in my application. I have used the below code. The designing is perfectly fine. All what I want to add multiple delete options in it.
Below is the code which I have tried out:

<html>
<head>
<title>Strad Hosting Limited -Web Hosting</title>

<script language="javascript">
function validate()
{
    var chks = document.getElementsByName('checkbox[]');
    var hasChecked = false;
    for (var i = 0; i < chks.length; i++)
    {
        if (chks[i].checked)
        {
            hasChecked = true;
            break;
        }
    }
    if (hasChecked == false)
    {
        alert("Please select at least one.");
        return false;
    }
    return true;
}
</script>

</head>
<body >

<?php
    $con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales");
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT * FROM Persons");

    echo "<form name='form1' method='post' action='' onSubmit='return validate();'>";
    echo "<table border='1' style='
            background-color: white;'>
            <tr>
                <th></th>
                <th>id</th>
                <th style='padding-left: 11px;'>Lead Generated Date   </th>
                <th>name</th>
                <th>email</th>
                <th>contacts</th>
                <th>requirement</th>
                <th style='display:none;'>Company name</th>
                <th style='display:none;'>Address</th>
                <th>Next Follow-Up date</th>
                <th>Final_Details</th>
                <th style='display:none;'>Status</th>
                <th style='padding-left: 12px; display:none;'>Lead Closed Date</th>
                <th>EDIT</th>
                <th>Follow-up History</th>
                <th>Add Follow-up</th>
                <th>Delete Record</th>
            </tr>";

    while($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['date'] . "</td>";
        echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "<td>" . $row['contacts'] . "</td>";
        echo "<td>" . $row['requirement'] . "</td>";

        echo "<td style='display:none;'>" . $row['company_name'] . "</td>";
        echo "<td style='display:none;'>" . $row['address'] . "</td>";
        echo "<td>" . $row['startdate'] . "</td>";
        echo "<td>" . $row['final_details'] . "</td>";
        echo "<td style='display:none;'>" . $row['status'] . "</td>";
        echo "<td style='display:none;'>" . $row['lead_close'] . "</td>";
        echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>";
        echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>";
        echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>";
        echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
        echo "</tr>";
    }
    echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>";

    $count=mysqli_num_rows($result);
    if(isset($_POST['delete'])){
        for($i=0;$i<count($_POST['checkbox']);$i++){
            $del_id=$_POST['checkbox'][$i];
            $sql = "DELETE FROM Persons WHERE id='$del_id'";
            echo $sql;
            $result2 = mysqli_query($con,$sql);
        }

        // if successful redirect to delete_multiple.php
        if($result2)
        {
            echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">";
        }   
    }

    mysqli_close($con);
    echo "</table>";
    echo "</form>";
?>

<br><br>
<a href="index2.php">[ Back To Home ]</a>

</body>
</html>

I am stuck, the query is not working I think. I don't know what I am missing.

771

Answer

Solution:

Try replacing

$sql = "DELETE FROM Persons WHERE id='$del_id'";

With

$sql = "DELETE FROM Persons WHERE id=$del_id";

I'm assuming the ID is a number so the single quotes aren't needed.

People are also looking for solutions to the problem: javascript - File not received at server using FormData

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.