• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How To Make Delete Multiple Rows Using PHP

NoureddineRe

API Integrator
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
103
Likes
42
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Good Day!!!

In this tutorial, we are going to learn on How To Make Delete Multiple Rows Using PHP. The features of this project is to add a data then, delete multiple data using check box as our selector. Hope you find this useful.

Directions:

For Javascript - Select All Rows

  1. <

    script language=

    "JavaScript"

    >
  2. function

    sel(

    source)
  3. {
  4. checkboxes =

    document.getElementsByName

    (

    'check[]'

    )

    ;
  5. for

    (

    var

    i in

    checkboxes)
  6. checkboxes[

    i]

    .checked

    =

    source.checked

    ;
  7. }
  8. </

    script>

For PHP - Add Data

  1. if

    (

    isset

    (

    $_POST

    [

    'submit'

    ]

    )

    )
  2. {
  3. $userid

    =

    $_POST

    [

    'userid'

    ]

    ;
  4. $name

    =

    $_POST

    [

    'name'

    ]

    ;
  5. $username

    =

    $_POST

    [

    'username'

    ]

    ;
  6. $email

    =

    $_POST

    [

    'email'

    ]

    ;
  7. $address

    =

    $_POST

    [

    'address'

    ]

    ;
  8. $id1

    =

    $_SERVER

    [

    'REMOTE_ADDR'

    ]

    ;
  9. $insert

    =

    mysql_query

    (

    "insert into user (userid,name,username,email,address) values ('$userid

    ','$name

    ','$username

    ','$email

    ','$address

    ')"

    )

    ;
  10. if

    (

    $insert

    )
  11. {
  12. echo

    "<script>alert('User has been added'); window.location='index.php'</script>"

    ;
  13. }

  14. }

For PHP - Delete Multiple Rows of Data

  1. if

    (

    isset

    (

    $_POST

    [

    'delete'

    ]

    )

    )
  2. {
  3. $check

    =

    $_POST

    [

    'check'

    ]

    ;
  4. $count

    =

    count

    (

    $check

    )

    ;
  5. for

    (

    $i

    =

    0

    ;

    $i

    <

    $count

    ;

    $i

    ++

    )

    {
  6. $del_id

    =

    $check

    [

    $i

    ]

    ;
  7. $delete

    =

    mysql_query

    (

    "delete from user where id='$del_id

    '"

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  8. }
  9. if

    (

    $delete

    )
  10. {
  11. echo

    "<script>alert('User has been deleted'); window.location='index.php'</script>"

    ;
  12. }

  13. }

Full Source Code

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    Delete Multiple Rows</

    title

    >
  5. <script

    language

    =

    "JavaScript"

    >
  6. function sel(source)
  7. {
  8. checkboxes = document.getElementsByName('check[]');
  9. for(var i in checkboxes)
  10. checkboxes.checked = source.checked;

    [*]}

    [*]</

    script

    >

    [*]<style

    type

    =

    "text/css"

    >

    [*]body {

    [*]width:100%;

    [*]font-family:"Trebuchet MS";

    [*]margin:0;

    [*]padding:0;

    [*]}

    [*]

    [*]h2 a {

    [*]text-decoration:none;

    [*]color:#06F;

    [*]}

    [*]

    [*]#container {

    [*]width:800px;

    [*]margin:0 auto;

    [*]margin-top:30px;

    [*]}

    [*]</

    style

    >

    [*]</

    head

    >

    [*]<body

    >

    [*]

    [*]<?php

    [*]$db =

    mysql_connect(

    'localhost'

    ,'root'

    ,''

    )

    or die (

    "Unable to connect to Database Server."

    )

    ;

    [*]mysql_select_db (

    'demo'

    , $db)

    or die (

    "Could not select database."

    )

    ;

    [*]if(

    isset(

    $_POST[

    'submit'

    ]

    )

    )

    [*]{

    [*]$userid=

    $_POST[

    'userid'

    ]

    ;

    [*]$name=

    $_POST[

    'name'

    ]

    ;

    [*]$username=

    $_POST[

    'username'

    ]

    ;

    [*]$email=

    $_POST[

    'email'

    ]

    ;

    [*]$address=

    $_POST[

    'address'

    ]

    ;

    [*]$id1 =

    $_SERVER[

    'REMOTE_ADDR'

    ]

    ;

    [*]$insert=

    mysql_query(

    "insert into user (userid,name,username,email,address) values ('$userid','$name','$username','$email','$address')"

    )

    ;

    [*]if(

    $insert)

    [*]{

    [*]echo "<script>

    alert('User has been added'); window.location='index.php'</

    script

    >

    ";

    [*]}

    [*]

    [*]}

    [*]if(isset($_POST['delete']))

    [*]{

    [*]$check=$_POST['check'];

    [*]$count=count($check);

    [*]for($i=0;$i<$count;$i++)

    {

    [*]$del_id =

    $check[

    $i]

    ;

    [*]$delete=

    mysql_query(

    "delete from user where id='$del_id'"

    )

    or die(

    mysql_error(

    )

    )

    ;

    [*]}

    [*]if(

    $delete)

    [*]{

    [*]echo "<script>

    alert('User has been deleted'); window.location='index.php'</

    script

    >

    ";

    [*]}

    [*]

    [*]}

    [*]?>

    [*]

    [*]

    [*]<form

    method

    =

    "post"

    name

    =

    "form"

    action

    =

    ""

    >

    [*]<table

    border

    =

    "1"

    style

    =

    "border:4px groove #CCC; float:left; margin-left:80px;"

    cellpadding

    =

    "4"

    cellspacing

    =

    "4"

    >

    [*]<tbody

    >

    [*]<tr

    >

    [*]<td

    colspan

    =

    "2"

    style

    =

    "text-align:center; font-weight:bold;"

    >

    Add User</

    td

    >

    [*]</

    tr

    >

    [*]<tr

    >

    [*]<td

    >

    [*]<span

    style

    =

    "color:blue;"

    >

    User ID</

    span

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<input

    name

    =

    "userid"

    style

    =

    "font-size:15px;"

    placeholder=

    "User ID..."

    type

    =

    "text"

    id

    =

    "userid"

    required /

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]<tr

    >

    [*]<td

    >

    [*]<span

    style

    =

    "color:blue;"

    >

    Name</

    span

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<input

    name

    =

    "name"

    style

    =

    "font-size:15px;"

    placeholder=

    "Name..."

    type

    =

    "text"

    id

    =

    "name"

    required /

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]<tr

    >

    [*]<td

    >

    [*]<span

    style

    =

    "color:blue;"

    >

    Username</

    span

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<input

    name

    =

    "username"

    type

    =

    "text"

    style

    =

    "font-size:15px;"

    placeholder=

    "Username..."

    id

    =

    "username"

    required /

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]<tr

    >

    [*]<td

    >

    [*]<span

    style

    =

    "color:blue;"

    >

    Email</

    span

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<input

    name

    =

    "email"

    type

    =

    "email"

    style

    =

    "font-size:15px;"

    placeholder=

    "Email..."

    id

    =

    "email"

    required /

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]<tr

    >

    [*]<td

    >

    [*]<span

    style

    =

    "color:blue;"

    >

    Address</

    span

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<input

    name

    =

    "address"

    style

    =

    "font-size:15px;"

    placeholder=

    "Address..."

    type

    =

    "text"

    id

    =

    "address"

    required /

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]<tr

    >

    [*]<td

    align

    =

    "center"

    colspan

    =

    "2"

    >

    [*]<input

    type

    =

    "submit"

    style

    =

    "color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;"

    name

    =

    "submit"

    value

    =

    "Add Employee"

    id

    =

    "submit"

    /

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]</

    tbody

    >

    [*]</

    table

    >

    [*]</

    form

    >

    [*]

    [*]<!---Viewing-->

    [*]<form

    method

    =

    "post"

    >

    [*]<table

    border

    =

    "1"

    style

    =

    "border:4px groove #CCC; float:right; margin-right:80px;"

    cellpadding

    =

    "0"

    cellspacing

    =

    "0"

    id

    =

    "container"

    >

    [*]<tr

    align

    =

    "center"

    >

    [*]<td

    >

    [*]<input

    type

    =

    "checkbox"

    onClick

    =

    "return sel(this);"

    /

    >

    &nbsp;&nbsp;&nbsp;

    <span

    >

    Select All</

    span

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<strong

    >

    User ID</

    strong

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<strong

    >

    Name</

    strong

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<strong

    >

    Username</

    strong

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<strong

    >

    Email</

    strong

    >

    [*]</

    td

    >

    [*]<td

    >

    [*]<strong

    >

    Address</

    strong

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]

    [*]<?php

    [*]$id1 =

    $_SERVER[

    'REMOTE_ADDR'

    ]

    ;

    [*]$result=

    mysql_query(

    "select * from user order by id ASC "

    )

    or die(

    mysql_error(

    )

    )

    ;

    [*]while(

    $user_info=

    mysql_fetch_array(

    $result)

    )

    {

    [*]?>

    [*]

    [*]<tr

    align

    =

    "center"

    >

    [*]<td

    >

    [*]<input

    type

    =

    "checkbox"

    name

    =

    "check[]"

    value

    =

    "<?php echo $user_info['id']; ?>

    " id="all" />

    [*]</

    td

    >

    [*]<td

    >

    [*]<?php echo $user_info[

    'userid'

    ]

    ; ?>

    [*]</

    td

    >

    [*]<td

    >

    [*]<?php echo $user_info[

    'name'

    ]

    ; ?>

    [*]</

    td

    >

    [*]<td

    >

    [*]<?php echo $user_info[

    'username'

    ]

    ; ?>

    [*]</

    td

    >

    [*]<td

    >

    [*]<?php echo $user_info[

    'email'

    ]

    ; ?>

    [*]</

    td

    >

    [*]<td

    >

    [*]<?php echo $user_info[

    'address'

    ]

    ; ?>

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]<?php }

    ?>

    [*]<tr

    >

    [*]<td

    colspan

    =

    "6"

    >

    [*]<p

    align

    =

    "center"

    >

    [*]<input

    type

    =

    "submit"

    style

    =

    "color:#fff; background-color:#CCC; border:2px groove chocolate; border-radius:4px; font-size:15px;"

    name

    =

    "delete"

    value

    =

    "Delete"

    /

    >

    [*]</

    p

    >

    [*]</

    td

    >

    [*]</

    tr

    >

    [*]

    [*]</

    table

    >

    [*]

    [*]

    [*]</

    form

    >

    [*]

    [*]

    [*]</

    body

    >

    [*]</

    html

    >



So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,498

348,113

348,122

Top