[trick] Membuat Update Data Secara Simultan dengan php dan mysql

Kali INI gw coba memberikan trik mengupdate data secara simultan ala phpmyadmin , OK Langsung aja >>

1. siapkan databasenya >>

update

2. langsung ke sintaknya >>

<?php
mysql_connect(“localhost”,”root”,””);
mysql_select_db(“latihan”);

$sql=mysql_query(“select * from member”);
$i=0;
echo”<form method=POST action=’?menu=pilih’><table border=1 cellpadding=4 cellspacing=0>
<tr><td>Id</td><td>Nama</td><td>Email</td><td>Pesan</td><td>Proses</td></tr>”;
while($data=mysql_fetch_array($sql)){
echo”<tr>

<td>$data[id]</td>

<td>$data[nama]</td>

<td>$data[email]</td>

<td>$data[pesan]</td>

<td><input type=checkbox name=’cek[]’ value=’$data[id]’></td>

</tr>”;
$i++;
}
echo”<tr><td colspan=5><input type=submit value=’Pilih’></td></tr></table></form><br><br>”;
?>

tampilan

3. pilih button pilih

if($_GET[‘menu’]==’pilih’){

echo”<table border=1 cellpadding=4 cellspacing=0>”;

$jum=$_POST[‘cek’];
$id=count($jum);
for($i=0;$i<$id;$i++){
$rows=mysql_query(“select * from member where id=’$jum[$i]'”);
while($query=mysql_fetch_array($rows)){
echo”<form method=POST action=’aksi.php?act=edit’><input type=hidden name=’id[$i]’ value=’$query[id]’>” ;
echo”<tr>

<td><input type=text name=’id[$i]’ value=’$query[id]’></td>

<td><input type=text name=’nama[$i]’ value=’$query[nama]’></td>

<td><input type=text name=’email[$i]’ value=’$query[email]’></td>

<td><input type=text name=’pesan[$i]’ value=’$query[pesan]’></td>

</tr>”;

}
}
echo”<tr><td colspan=4><input type=submit value=’Pilih’></td></tr></table></form>”;
}

update_simultan

4. aksi.php

<?php
mysql_connect(“localhost”,”root”,””);
mysql_select_db(“latihan”);

if($_GET[‘act’]==’edit’){
$jum=$_POST[‘id’];
$id=count($jum);
for($i=0;$i<$id;$i++){
$nama=$_POST[‘nama’][$i];
$email=$_POST[’email’][$i];
$pesan=$_POST[‘pesan’][$i];
mysql_query(“update member set id=’$jum[$i]’,nama=’$nama’,email=’$email’,pesan=’$pesan’ where id=’$jum[$i]'”);
header(‘location:index.php’);
}
}
?>

hasil

mudah bukan ..

download source code disini

tutorial advance tentang CRUD Simultan lainnya disini

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: