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 -->
avatar FitriR
@FitriR

109 Kontribusi 2 Poin

Diperbarui 6 tahun yang lalu

6 Jawaban:

ada dua cara

<pre> //cara 1 if (isset($_POST['update'])) { $jumData=$_POST['jumData'];

	for ($i	=0; $i&amp;lt;$jumData ; $i++) {

	 $idmon=$_POST[&#039;txtId&#039;];

	 //$descus[] = $_POST[&#039;txtDesc&#039;.$i];
	 
mysql_query(&quot;UPDATE detail_monitor SET desc_user=&#039;&quot;.$_POST[&#039;txtDesc&#039;.$i].&quot;&#039;
			 WHERE id_mon=&#039;$idmon&#039;&quot;);

}
		 
echo &quot;&amp;lt;script&amp;gt;window.location=&#039;?pg=kategori&amp;act=view&#039;&amp;lt;/script&amp;gt;&quot;;
 

}

//cara 2 if (isset($_POST['update'])) { $jumData=$_POST['jumData'];

	for ($i	=0; $i&amp;lt;$jumData ; $i++) {

	 $idmon=$_POST[&#039;txtId&#039;];

	 $descus[] = $_POST[&#039;txtDesc&#039;.$i];
	 

}
foreach ($descus as $key =&amp;gt; $value) {
	mysql_query(&quot;UPDATE detail_monitor SET desc_user=&#039;$value&#039;
			 WHERE id_mon=&#039;$idmon&#039;&quot;);
}

		 
echo &quot;&amp;lt;script&amp;gt;window.location=&#039;?pg=kategori&amp;act=view&#039;&amp;lt;/script&amp;gt;&quot;;
 

} </pre>

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 6 tahun yang lalu

Dear @dianarifr,

Terima kasih atas jawaban nya. Tetapi saya update kenapa belum bisa ya? Mohon pencerahan nya.

Terima kasih

avatar FitriR
@FitriR

109 Kontribusi 2 Poin

Dipost 6 tahun yang lalu

coba <pre> $nomor =1; </pre> diganti <pre> $nomor =0; </pre> saat while $result

untuk name="txtId" diganti readonly, jangan disabled

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 6 tahun yang lalu

Dear @dianarifr,

Terima kasih sudah menjawab. Saya sudah mengikut langkah nya agan ni. Tetapi masih belum bisa. Mohon pencerahan nya. Terima kasih

<pre> &lt;?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[&#039;update&#039;])) {
		$jumData=$_POST[&#039;jumData&#039;];

			for ($i	=0; $i&amp;lt;$jumData ; $i++) {

			 $idmon=$_POST[&#039;txtId&#039;];

			 $descus[] = $_POST[&#039;txtDesc&#039;.$i];
			 $descuser[] = $_POST[&#039;txtDescUser&#039;.$i];
			 $testuser[] = $_POST[&#039;txtText&#039;.$i];
			 

		}
		foreach ($descus as $key =&amp;gt; $value) {
			foreach ($testuser as $key =&amp;gt; $value2) {
			mysql_query(&quot;UPDATE detail_monitor SET desc_user=&#039;$value&#039;
					 WHERE id_mon=&#039;$idmon&#039; and id_test=&#039;$value2&#039;&quot;);
			}
		}
		
				 
		echo &quot;&amp;lt;script&amp;gt;window.location=&#039;?pg=kategori&amp;act=view&#039;&amp;lt;/script&amp;gt;&quot;;
		 
	}
  
  $sqlstr=&quot;select *  FROM trn_monitor d1, detail_monitor d2 
		   WHERE d1.id_mon = d2.id_mon and d1.id_mon =&#039;$_GET[id]&#039;&quot;;

  $result = mysql_query ($sqlstr) or die (&quot;Kesalahan pada perintah SQL!&quot;);
  ?&amp;gt;

&lt;div class="content-wrapper"&gt; &lt;!-- Content Header (Page header) --&gt; &lt;section class="content-header"&gt; &lt;h1&gt; Data Project &lt;/h1&gt; &lt;ol class="breadcrumb"&gt; &lt;li&gt;&lt;a href="?pg=dashboard"&gt;&lt;i class="fa fa-dashboard"&gt;&lt;/i&gt; Beranda&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="?pg=kategori&act=view"&gt;Data Project&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;Update Data Project&lt;/li&gt; &lt;/ol&gt; &lt;/section&gt;

&lt;!-- Main content --&gt; &lt;section class="content"&gt; &lt;div class="row"&gt; &lt;div class="col-md-12"&gt; &lt;!-- general form elements --&gt; &lt;div class="box box-info"&gt; &lt;div class="box-body"&gt; &lt;!-- form start --&gt; &lt;form role="form" method = "POST" action=""&gt; &lt;div class="box-body"&gt; &lt;div class="form-group"&gt; &lt;label for="exampleInputEmail1"&gt;No. Project&lt;/label&gt; &lt;input type="text" class="form-control" id="txtId" name="txtId" placeholder="Nama Project" required data-fv-notempty-message="Tidak boleh kosong" value= "&lt;?php echo $d['id_mon'];?&gt;" readonly&gt; &lt;/div&gt;

                &amp;lt;div class=&quot;form-group&quot;&amp;gt;
                  &amp;lt;label for=&quot;exampleInputEmail1&quot;&amp;gt;Nama Project&amp;lt;/label&amp;gt;
                  &amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;name_mon&quot; name=&quot;name_mon&quot; placeholder=&quot;Nama Project&quot; 
				  required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $d[&#039;name_mon&#039;];?&amp;gt;&quot;&amp;gt;
				  &amp;lt;input type=&quot;hidden&quot; class=&quot;form-control&quot; id=&quot;id&quot; name=&quot;id&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $d[&#039;id_mon&#039;];?&amp;gt;&quot;&amp;gt;
                &amp;lt;/div&amp;gt;
				
				&amp;lt;div class=&quot;form-group&quot;&amp;gt;
                  &amp;lt;label for=&quot;exampleInputEmail1&quot;&amp;gt;PIC&amp;lt;/label&amp;gt;
                  &amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;pic&quot; name=&quot;pic&quot; placeholder=&quot;Nama Project&quot; 
				  required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $d[&#039;pic&#039;];?&amp;gt;&quot;&amp;gt;
                &amp;lt;/div&amp;gt;
				
				&amp;lt;div class=&quot;form-group&quot;&amp;gt;
                  &amp;lt;table&amp;gt;
					&amp;lt;tr&amp;gt;
						&amp;lt;td&amp;gt;No.&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;Task Name&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;Date Start&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;Date Finish&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;Description&amp;lt;/td&amp;gt;
					&amp;lt;/tr&amp;gt;
					&amp;lt;?php 
						$nomor =0;
						
						while ($c = mysql_fetch_object ($result))
						{
							
						$id_mon = $c-&amp;gt;id_mon;
						
						$idtest = $c-&amp;gt;id_test;
						
						$desc = $c-&amp;gt;desc;
						$proj_start = $c-&amp;gt;proj_start;
						$proj_finish = $c-&amp;gt;proj_finish;
						$desc_user = $c-&amp;gt;desc_user;
						$problem = $c-&amp;gt;problem;
						$solution = $c-&amp;gt;solution;
						
					?&amp;gt;
					&amp;lt;tr&amp;gt;
						&amp;lt;td&amp;gt;
						&amp;lt;div class=&quot;form-group&quot;&amp;gt;
							&amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtNo.$nomor.&quot; name=&quot;txtNo.$nomor.&quot; placeholder=&quot;Nama PIC&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $nomor ?&amp;gt;&quot;&amp;gt;
							
						&amp;lt;/div&amp;gt;
						&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;
						&amp;lt;div class=&quot;form-group&quot;&amp;gt;
							&amp;lt;input type=&quot;hidden&quot; class=&quot;form-control&quot; id=&quot;txtIdMon&amp;lt;?php echo $nomor ?&amp;gt;&quot; name=&quot;txtIdMon&amp;lt;?php echo $nomor ?&amp;gt;&quot; placeholder=&quot;Description&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $id_mon ?&amp;gt;&quot;&amp;gt;
							
							&amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtDescUser&amp;lt;?php echo $nomor ?&amp;gt;&quot; name=&quot;txtDescUser&amp;lt;?php echo $nomor ?&amp;gt;&quot; placeholder=&quot;Description&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $desc ?&amp;gt;&quot;&amp;gt;
							
							&amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtText&amp;lt;?php echo $nomor ?&amp;gt;&quot; name=&quot;txtText&amp;lt;?php echo $nomor ?&amp;gt;&quot; placeholder=&quot;Description&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $idtest ?&amp;gt;&quot;&amp;gt;
						&amp;lt;/div&amp;gt;
						&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;
						&amp;lt;div class=&quot;form-group&quot;&amp;gt;
							&amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtStart&quot; name=&quot;txtStart&quot; placeholder=&quot;Nama PIC&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $proj_start ?&amp;gt;&quot;&amp;gt;
						&amp;lt;/div&amp;gt;
						&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;
						&amp;lt;div class=&quot;form-group&quot;&amp;gt;
							&amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtFinish&quot; name=&quot;txtFinish&quot; placeholder=&quot;Nama PIC&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $proj_finish ?&amp;gt;&quot;&amp;gt;
						&amp;lt;/div&amp;gt;
						&amp;lt;/td&amp;gt;
						&amp;lt;td&amp;gt;
						&amp;lt;div class=&quot;form-group&quot;&amp;gt;
						
						&amp;lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;txtDesc&amp;lt;?php echo $nomor ?&amp;gt;&quot; name=&quot;txtDesc&amp;lt;?php echo $nomor ?&amp;gt;&quot; placeholder=&quot;Nama PIC&quot; required data-fv-notempty-message=&quot;Tidak boleh kosong&quot; value= &quot;&amp;lt;?php echo $desc_user ?&amp;gt;&quot;&amp;gt;
							
						&amp;lt;/div&amp;gt;
						&amp;lt;/td&amp;gt;
						
					&amp;lt;/tr&amp;gt;
					&amp;lt;?php 
						$nomor++;
						}
						
						$query   = &quot;select COUNT(*) as jumData FROM trn_monitor d1, detail_monitor d2
						WHERE d1.id_mon = d2.id_mon and d1.id_mon =&#039;$_GET[id]&#039;&quot;;
					
						$hasil  = mysql_query($query);
						$data     = mysql_fetch_array($hasil);
						// mysql_close($link);
						$jumData = $data[&#039;jumData&#039;];

						echo&quot;&amp;lt;input type=text name=jumData value=$jumData /&amp;gt;&quot;;
					?&amp;gt;
				  &amp;lt;/table&amp;gt;
                &amp;lt;/div&amp;gt;
                
              &amp;lt;/div&amp;gt;&amp;lt;!-- /.box-body --&amp;gt;

          &amp;lt;/div&amp;gt;&amp;lt;!-- /.box --&amp;gt;
          &amp;lt;/div&amp;gt; &amp;lt;!-- /.col --&amp;gt;

          &amp;lt;/div&amp;gt; &amp;lt;!-- /.row --&amp;gt;

      
        &amp;lt;!-- Tombol Bagian Bawah --&amp;gt;

        &amp;lt;div class=&quot;row&quot;&amp;gt;
        &amp;lt;!-- left column --&amp;gt;
          &amp;lt;div class=&quot;col-md-4 col-md-offset-5&quot;&amp;gt;

          &amp;lt;button type=&quot;submit&quot; name = &#039;update&#039; class=&quot;btn btn-info&quot;&amp;gt;Update&amp;lt;/button&amp;gt;
          &amp;nbsp;
          &amp;lt;button type=&quot;reset&quot; class=&quot;btn btn-success&quot;&amp;gt;Reset&amp;lt;/button&amp;gt;
              
        &amp;lt;/form&amp;gt;
          &amp;lt;/div&amp;gt;&amp;lt;!-- /.box-body --&amp;gt;
          &amp;lt;/div&amp;gt;&amp;lt;!-- /.box --&amp;gt;
          &amp;lt;/div&amp;gt; &amp;lt;!-- /.col --&amp;gt;

&lt;/div&gt; &lt;!-- /.row (main row) --&gt; &lt;/section&gt; &lt;!-- /.content --&gt; &lt;/div&gt;&lt;!-- /.container --&gt; &lt;/div&gt;&lt;!-- /.content-wrapper --&gt; </pre>

avatar FitriR
@FitriR

109 Kontribusi 2 Poin

Dipost 6 tahun yang lalu

Jawaban Terpilih

dibuat gini aja gan lebih simple <pre> &lt;?php if (isset($_POST['update'])) { $jumData=$_POST['jumData'];

			for ($i	=0; $i&amp;lt;$jumData ; $i++) {

			 $idmon=$_POST[&#039;txtId&#039;];

			 // $descus[] = $_POST[&#039;txtDesc&#039;.$i];
			 // $descuser[] = $_POST[&#039;txtDescUser&#039;.$i];
			 // $testuser[] = $_POST[&#039;txtText&#039;.$i];
			mysql_query(&quot;UPDATE detail_monitor SET desc_user=&#039;&quot;.$_POST[&#039;txtDesc&#039;.$i].&quot;&#039; WHERE id_mon=&#039;$idmon&#039; and id_test=&#039;&quot;.$_POST[&#039;txtText&#039;.$i].&quot;&#039;&quot;);
			 

		}
							 
		echo &quot;&amp;lt;script&amp;gt;window.location=&#039;?pg=kategori&amp;act=view&#039;&amp;lt;/script&amp;gt;&quot;;
		 
	}

?&gt; </pre> ini diganti <pre> echo"&lt;input type=text name=jumData value=$jumData /&gt;"; </pre> jadi <pre> echo"&lt;input type='text' name='jumData' value='".$jumData.'" /&gt;"; </pre>

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 6 tahun yang lalu

Dear @dianarifr,

Terima kasih Gan atas masukkan nya dan sudah saya coba berhasil.

Terima kasih

avatar FitriR
@FitriR

109 Kontribusi 2 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban