Read 193 times | Created 2013-01-25 15:02:12 | Updated 2013-01-25 18:02:26 | | |

 

<?php
/*********************************
FILENAME  : nama.php
CREATE BY  : cahya dsn
PURPOSE   : edit 1 field table
CREATE DATE : 2013-01-25
*********************************/
/*
#table creation
use test;
DROP TABLE IF EXISTS `tbnama`;
CREATE TABLE IF NOT EXISTS `tbnama` (
  `nama` varchar(30) NOT NULL,
  UNIQUE KEY `nama` (`nama`)
) ENGINE=MyISAM;
INSERT INTO `tbnama`(`nama`) VALUES
('amir'),('budi'),('citra'),('dewi');
*/
//database configuration
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='test';
//database connection
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$cmd=isset($_POST['cmd'])?$_POST['cmd']:(isset($_GET['cmd'])?$_GET['cmd']:'');
if(empty($cmd)){
  $query="SELECT nama FROM tbnama";
  $result=$db->query($query);
  echo '<table>';
  while($row=$result->fetch_object()){
    echo '<tr><td><a href="'.$_SERVER['PHP_SELF'].'?cmd=update&n='.$row->nama.'">'.$row->nama.'</td></tr>';
  }
  echo '</table>';
}elseif($cmd=='update'){
?>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Nama sebelumnya <input type="text" name="data[]" 
                         value="<?php echo isset($_GET['n'])?$_GET['n']:'';?>"
                         readonly="readonly"/><br>
  Nama perubahan <input type="text" name="data[]" value=""/><br>
  <input type="hidden" name="cmd" value="edit" />
  <input type="submit" value="ubah" />
  </form>
<?
}elseif($cmd=='edit'){
	$data=$_POST['data'];
	$query="UPDATE tbnama SET nama='".$data[1]."' WHERE nama='".$data[0]."'";
	$db->query($query);
	$db->close();
	header('location:'.$_SERVER['PHP_SELF']);
}
?>