Jump to content

delete(multiple) work,but edit(multiple) not work


bangkit

Recommended Posts

Hi,Please Help Me..
I have a problem with my script, I want to create multiple delete and multiple edit with checkbox.
i've tried to make script but the only way the delete function , edit is not
I have a table dokter and structure like this:
field==> k_dokter | nama | jk | alamat | hp | spesialis

and this is my script,

PHP Code:
<?php

 
include "../config/connection.php"?>

<body>

<form name="myform" method="post" action="">

<table class="table1">

<tr bgcolor="#0FFF00">

 <td width="2px;"><input type='checkbox' name='pilih'> </td>

 <td>Kode Dokter</td>

 <td>Nama Dokter</td>

 <td>JK</td>

 <td>Alamat</td>

 <td>Telp</td>

 <td>Spesialis</td>

</tr>

<?php

$no
=1;

$i=0;

$q=mysql_query("select * from dokter ");

while(
$sqld=mysql_fetch_array($q)){

$kd=stripslashes($sqld['k_dokter']);

$nd=stripslashes($sqld['nama']);

$jk=stripslashes($sqld['jk']);

$alamat=stripslashes($sqld['alamat']);

$hp=stripslashes($sqld['hp']);

$s=stripslashes($sqld['spesialis']);

$color=($no%2==1)? "#F7F7F7" "#E8E8E8";

?>

<tr bgcolor="<?php echo $color?>">

 <td><input type="checkbox" class="check" name="ip[<?php echo $i ?>]" value="<?php echo $sqld['k_dokter'?>" /></td>

 <td><?php echo $kd ?></td>

 <td><?php echo $nd ?></td>

 <td><?php echo $jk ?></td>

 <td><?php echo $alamat ?></td>

 <td><?php echo $hp ?></td>

 <td><?php echo $s ?></td>

 

</tr>

<?php $no++;

  
$i++;

  }

   
?>

</table>

  <input type="submit" value="delete multiple"  name="hapus"  class="button blue" >

   <input type="submit" value="Edit multiple"  name="edit"  class="button blue"   />

 <br />

</form>

<?php

/************************************************************FOR DELETE*********************************/

if(isset($_POST['hapus'])){

 
$ip=$_POST['ip'];

 foreach(
$ip as $ip1){

 
$query="delete from dokter where k_dokter='$ip1'";

 
$dd=mysql_query($query);

 }

 }


 
/***********************************FOR EDIT****************************************************/

if(isset($_POST['edit'])){



$ip=$_POST['ip'];

$con=count($ip);

if(isset(
$_POST['edit_multi']))

{


 for(
$i=0;$i<$con;$i++){

  
$id1=$_POST['id'][$i];

  
$nama1=$_POST['nama'][$i];

  
$alamat1=$_POST['alamat'][$i];

  
$hp1=$_POST['hp'][$i];

  
$spes1=$_POST['spes'][$i];

  
$sql=mysql_query("update dokter SET nama='$nama1',alamat='$alamat1',hp='$hp1',apesialis='$spes1' where k_dokter='$id1'");

  }

  if(
$sql){

 echo 
"<script>location.replace('home.php?p=dokter&b=tampil')</script>";

     } else {

 echo 
"<script>alert('GAGAL')

   location.replace('home.php?p=dokter&b=tampil')</script>"
;

   }

 }


$sql "SELECT * FROM dokter";

$results mysql_query($sql);

while(
$row mysql_fetch_assoc($results))

{

 
$id_array[] = $row["k_dokter"];

 
$nama_array[] = $row["nama"];

 
$alamat_array[] = $row["alamat"];

 
$hp_array[] = $row["hp"];

 
$s_array[] = $row["spesialis"];

 }

echo 
"<form  method='post'>";

for(
$i=0;$i<$con;$i++)

{

 
$id $id_array[$i];

 
$nama $nama_array[$i];

 
$alamat $alamat_array[$i];

 
$hp $hp_array[$i];

 
$s $s_array[$i];

 echo 
"<input type='hidden' name='id[$i]' value='$id'><br />

 <input type='text' name='nama[
$i]' value='$nama'><br />

 <input type='text' name='alamat[
$i]' value='$alamat'><br />

 <input type='text' name='hp[
$i]' value='$hp'><br />

 <input type='text' name='spes[
$i]' value='$s'>";

 }

echo 
"<p><input type='submit' name='edit_multi' value='Input'></p></form>";

}


?>
                                          
Thanks before...
 
 

 

You are making this harder than it needs to be. Never run queries in loops unless you are sure there is no other way. instead of this

 

if(isset($_POST['hapus'])){
  $ip=$_POST['ip'];
  foreach($ip as $ip1){
    $query="delete from dokter where k_dokter='$ip1'";
    $dd=mysql_query($query);
  }
}

 

Do something like this;

 

if(isset($_POST['hapus'])){
  $ipList = "'" . implode("', '" $_POST['ip']) . "'";
  $query = "DELETE FROM dokter WHERE k_dokter IN ($ipList)";
  $dd = mysql_query($query);
}

 

Note; There's a lot more you should be doing, but I've not the time to get into all of it right now

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.