Postingan lainnya
For Update PHP
Dear Master PHP,
Mohon bantuan nya. Saya ingin update menggunakan for. Kenapa belum berhasil ya? Mohon di bantu Master PHP.
<?php
break;
// PROSES EDIT DATA PRODUK //
case 'edit':
$d = mysql_fetch_array(mysql_query("SELECT * FROM trn_monitor WHERE id_mon='$_GET[id]'"));
if (isset($_POST['update'])) {
$jumData=$_POST['jumData'];
for ($i =0; $i<$jumData ; $i++) {
$idmon=$_POST['txtId'];
$descus[] = $_POST['txtDesc'.$i];
}
mysql_query("UPDATE detail_monitor SET desc_user='$descus[$i]'
WHERE id_mon='$idmon'");
echo "<script>window.location='?pg=kategori&act=view'</script>";
}
$sqlstr="select * FROM trn_monitor d1, detail_monitor d2
WHERE d1.id_mon = d2.id_mon and d1.id_mon ='$_GET[id]'";
$result = mysql_query ($sqlstr) or die ("Kesalahan pada perintah SQL!");
?>
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1> Data Project </h1>
<ol class="breadcrumb">
<li><a href="?pg=dashboard"><i class="fa fa-dashboard"></i> Beranda</a></li>
<li class="active"><a href="?pg=kategori&act=view">Data Project</a></li>
<li class="active">Update Data Project</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-info">
<div class="box-body">
<!-- form start -->
<form role="form" method = "POST" action="">
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">No. Project</label>
<input type="text" class="form-control" id="txtId" name="txtId" placeholder="Nama Project"
required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $d['id_mon'];?>" disabled>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Project</label>
<input type="text" class="form-control" id="name_mon" name="name_mon" placeholder="Nama Project"
required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $d['name_mon'];?>">
<input type="hidden" class="form-control" id="id" name="id" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $d['id_mon'];?>">
</div>
<div class="form-group">
<label for="exampleInputEmail1">PIC</label>
<input type="text" class="form-control" id="pic" name="pic" placeholder="Nama Project"
required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $d['pic'];?>">
</div>
<div class="form-group">
<table>
<tr>
<td>No.</td>
<td>Task Name</td>
<td>Date Start</td>
<td>Date Finish</td>
<td>Description</td>
</tr>
<?php
$nomor =1;
while ($c = mysql_fetch_object ($result))
{
$id_mon = $c->id_mon;
$id = $c->id_test;
$desc = $c->desc;
$proj_start = $c->proj_start;
$proj_finish = $c->proj_finish;
$desc_user = $c->desc_user;
$problem = $c->problem;
$solution = $c->solution;
?>
<tr>
<td>
<div class="form-group">
<input type="text" class="form-control" id="txtNo.$nomor." name="txtNo.$nomor." placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $nomor ?>">
<? echo"<input type=hidden name=idtest".$nomor." value=$id>"; ?>
</div>
</td>
<td>
<div class="form-group">
<input type="hidden" class="form-control" id="txtIdMon<?php echo $nomor ?>" name="txtIdMon<?php echo $nomor ?>" placeholder="Description" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $id_mon ?>">
<input type="text" class="form-control" id="txtDescUser<?php echo $nomor ?>" name="txtDescUser<?php echo $nomor ?>" placeholder="Description" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $desc ?>">
</div>
</td>
<td>
<div class="form-group">
<input type="text" class="form-control" id="txtStart" name="txtStart" placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $proj_start ?>">
</div>
</td>
<td>
<div class="form-group">
<input type="text" class="form-control" id="txtFinish" name="txtFinish" placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $proj_finish ?>">
</div>
</td>
<td>
<div class="form-group">
<input type="text" class="form-control" id="txtDesc<?php echo $nomor ?>" name="txtDesc<?php echo $nomor ?>" placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $desc_user ?>">
</div>
</td>
</tr>
<?php
$nomor++;
}
$query = "select COUNT(*) as jumData FROM trn_monitor d1, detail_monitor d2
WHERE d1.id_mon = d2.id_mon and d1.id_mon ='$_GET[id]'";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);
// mysql_close($link);
$jumData = $data['jumData'];
echo"<input type=text name=jumData value=$jumData />";
?>
</table>
</div>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div> <!-- /.col -->
</div> <!-- /.row -->
<!-- Tombol Bagian Bawah -->
<div class="row">
<!-- left column -->
<div class="col-md-4 col-md-offset-5">
<button type="submit" name = 'update' class="btn btn-info">Update</button>
<button type="reset" class="btn btn-success">Reset</button>
</form>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div> <!-- /.col -->
</div>
<!-- /.row (main row) -->
</section> <!-- /.content -->
</div><!-- /.container -->
</div><!-- /.content-wrapper -->
6 Jawaban:
ada dua cara
<pre> //cara 1 if (isset($_POST['update'])) { $jumData=$_POST['jumData'];
for ($i =0; $i&lt;$jumData ; $i++) {
$idmon=$_POST['txtId'];
//$descus[] = $_POST['txtDesc'.$i];
mysql_query("UPDATE detail_monitor SET desc_user='".$_POST['txtDesc'.$i]."'
WHERE id_mon='$idmon'");
}
echo "&lt;script&gt;window.location='?pg=kategori&act=view'&lt;/script&gt;";
}
//cara 2 if (isset($_POST['update'])) { $jumData=$_POST['jumData'];
for ($i =0; $i&lt;$jumData ; $i++) {
$idmon=$_POST['txtId'];
$descus[] = $_POST['txtDesc'.$i];
}
foreach ($descus as $key =&gt; $value) {
mysql_query("UPDATE detail_monitor SET desc_user='$value'
WHERE id_mon='$idmon'");
}
echo "&lt;script&gt;window.location='?pg=kategori&act=view'&lt;/script&gt;";
} </pre>
Dear @dianarifr,
Terima kasih atas jawaban nya. Tetapi saya update kenapa belum bisa ya? Mohon pencerahan nya.
Terima kasih
coba <pre> $nomor =1; </pre> diganti <pre> $nomor =0; </pre> saat while $result
untuk name="txtId" diganti readonly, jangan disabled
Dear @dianarifr,
Terima kasih sudah menjawab. Saya sudah mengikut langkah nya agan ni. Tetapi masih belum bisa. Mohon pencerahan nya. Terima kasih
<pre> <?php break; // PROSES EDIT DATA PRODUK // case 'edit': $d = mysql_fetch_array(mysql_query("SELECT * FROM trn_monitor WHERE id_mon='$_GET[id]'"));
if (isset($_POST['update'])) {
$jumData=$_POST['jumData'];
for ($i =0; $i&lt;$jumData ; $i++) {
$idmon=$_POST['txtId'];
$descus[] = $_POST['txtDesc'.$i];
$descuser[] = $_POST['txtDescUser'.$i];
$testuser[] = $_POST['txtText'.$i];
}
foreach ($descus as $key =&gt; $value) {
foreach ($testuser as $key =&gt; $value2) {
mysql_query("UPDATE detail_monitor SET desc_user='$value'
WHERE id_mon='$idmon' and id_test='$value2'");
}
}
echo "&lt;script&gt;window.location='?pg=kategori&act=view'&lt;/script&gt;";
}
$sqlstr="select * FROM trn_monitor d1, detail_monitor d2
WHERE d1.id_mon = d2.id_mon and d1.id_mon ='$_GET[id]'";
$result = mysql_query ($sqlstr) or die ("Kesalahan pada perintah SQL!");
?&gt;
<div class="content-wrapper"> <!-- Content Header (Page header) --> <section class="content-header"> <h1> Data Project </h1> <ol class="breadcrumb"> <li><a href="?pg=dashboard"><i class="fa fa-dashboard"></i> Beranda</a></li> <li class="active"><a href="?pg=kategori&act=view">Data Project</a></li> <li class="active">Update Data Project</li> </ol> </section>
<!-- Main content --> <section class="content"> <div class="row"> <div class="col-md-12"> <!-- general form elements --> <div class="box box-info"> <div class="box-body"> <!-- form start --> <form role="form" method = "POST" action=""> <div class="box-body"> <div class="form-group"> <label for="exampleInputEmail1">No. Project</label> <input type="text" class="form-control" id="txtId" name="txtId" placeholder="Nama Project" required data-fv-notempty-message="Tidak boleh kosong" value= "<?php echo $d['id_mon'];?>" readonly> </div>
&lt;div class="form-group"&gt;
&lt;label for="exampleInputEmail1"&gt;Nama Project&lt;/label&gt;
&lt;input type="text" class="form-control" id="name_mon" name="name_mon" placeholder="Nama Project"
required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $d['name_mon'];?&gt;"&gt;
&lt;input type="hidden" class="form-control" id="id" name="id" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $d['id_mon'];?&gt;"&gt;
&lt;/div&gt;
&lt;div class="form-group"&gt;
&lt;label for="exampleInputEmail1"&gt;PIC&lt;/label&gt;
&lt;input type="text" class="form-control" id="pic" name="pic" placeholder="Nama Project"
required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $d['pic'];?&gt;"&gt;
&lt;/div&gt;
&lt;div class="form-group"&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;No.&lt;/td&gt;
&lt;td&gt;Task Name&lt;/td&gt;
&lt;td&gt;Date Start&lt;/td&gt;
&lt;td&gt;Date Finish&lt;/td&gt;
&lt;td&gt;Description&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
$nomor =0;
while ($c = mysql_fetch_object ($result))
{
$id_mon = $c-&gt;id_mon;
$idtest = $c-&gt;id_test;
$desc = $c-&gt;desc;
$proj_start = $c-&gt;proj_start;
$proj_finish = $c-&gt;proj_finish;
$desc_user = $c-&gt;desc_user;
$problem = $c-&gt;problem;
$solution = $c-&gt;solution;
?&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div class="form-group"&gt;
&lt;input type="text" class="form-control" id="txtNo.$nomor." name="txtNo.$nomor." placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $nomor ?&gt;"&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;div class="form-group"&gt;
&lt;input type="hidden" class="form-control" id="txtIdMon&lt;?php echo $nomor ?&gt;" name="txtIdMon&lt;?php echo $nomor ?&gt;" placeholder="Description" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $id_mon ?&gt;"&gt;
&lt;input type="text" class="form-control" id="txtDescUser&lt;?php echo $nomor ?&gt;" name="txtDescUser&lt;?php echo $nomor ?&gt;" placeholder="Description" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $desc ?&gt;"&gt;
&lt;input type="text" class="form-control" id="txtText&lt;?php echo $nomor ?&gt;" name="txtText&lt;?php echo $nomor ?&gt;" placeholder="Description" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $idtest ?&gt;"&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;div class="form-group"&gt;
&lt;input type="text" class="form-control" id="txtStart" name="txtStart" placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $proj_start ?&gt;"&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;div class="form-group"&gt;
&lt;input type="text" class="form-control" id="txtFinish" name="txtFinish" placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $proj_finish ?&gt;"&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;div class="form-group"&gt;
&lt;input type="text" class="form-control" id="txtDesc&lt;?php echo $nomor ?&gt;" name="txtDesc&lt;?php echo $nomor ?&gt;" placeholder="Nama PIC" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $desc_user ?&gt;"&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
$nomor++;
}
$query = "select COUNT(*) as jumData FROM trn_monitor d1, detail_monitor d2
WHERE d1.id_mon = d2.id_mon and d1.id_mon ='$_GET[id]'";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);
// mysql_close($link);
$jumData = $data['jumData'];
echo"&lt;input type=text name=jumData value=$jumData /&gt;";
?&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;!-- /.box-body --&gt;
&lt;/div&gt;&lt;!-- /.box --&gt;
&lt;/div&gt; &lt;!-- /.col --&gt;
&lt;/div&gt; &lt;!-- /.row --&gt;
&lt;!-- Tombol Bagian Bawah --&gt;
&lt;div class="row"&gt;
&lt;!-- left column --&gt;
&lt;div class="col-md-4 col-md-offset-5"&gt;
&lt;button type="submit" name = 'update' class="btn btn-info"&gt;Update&lt;/button&gt;
&nbsp;
&lt;button type="reset" class="btn btn-success"&gt;Reset&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;&lt;!-- /.box-body --&gt;
&lt;/div&gt;&lt;!-- /.box --&gt;
&lt;/div&gt; &lt;!-- /.col --&gt;
</div> <!-- /.row (main row) --> </section> <!-- /.content --> </div><!-- /.container --> </div><!-- /.content-wrapper --> </pre>
Jawaban Terpilih
dibuat gini aja gan lebih simple <pre> <?php if (isset($_POST['update'])) { $jumData=$_POST['jumData'];
for ($i =0; $i&lt;$jumData ; $i++) {
$idmon=$_POST['txtId'];
// $descus[] = $_POST['txtDesc'.$i];
// $descuser[] = $_POST['txtDescUser'.$i];
// $testuser[] = $_POST['txtText'.$i];
mysql_query("UPDATE detail_monitor SET desc_user='".$_POST['txtDesc'.$i]."' WHERE id_mon='$idmon' and id_test='".$_POST['txtText'.$i]."'");
}
echo "&lt;script&gt;window.location='?pg=kategori&act=view'&lt;/script&gt;";
}
?> </pre> ini diganti <pre> echo"<input type=text name=jumData value=$jumData />"; </pre> jadi <pre> echo"<input type='text' name='jumData' value='".$jumData.'" />"; </pre>
Dear @dianarifr,
Terima kasih Gan atas masukkan nya dan sudah saya coba berhasil.
Terima kasih