product_edit.php

<?php


// DATA BARU

//if ($_REQUEST[mode]=='edit')
//{extract ($_REQUEST);
//do something  

//echo $_REQUEST['id'];
//will give us the valid id from the previous page.
//}


$nama = isset($_POST['nama']) ? $_POST['nama'] : '';	  
$kategori = isset($_POST['kategori']) ? $_POST['kategori'] : '';
$harga = isset($_POST['harga']) ? $_POST['harga'] : '';	  
$gambar = isset($_POST['gambar']) ? $_POST['gambar'] : '';	  
$deskripsi = isset($_POST['deskripsi']) ? $_POST['deskripsi'] : '';

echo $nama;
  


// SETELAH TOMBOL SIMPAN DI TEKAN

if (isset($_GET['save'])){
		$nama = $_GET['nama'];
		$kategori = $_GET['kategori'];
		$harga = $_GET['harga'];
		$gambar = $_GET['gambar'];
		
		if (empty($_GET['id'])){
			$result = mysql_query("INSERT INTO produk(nama_produk, harga, deskripsi, gambar) VALUES('".$nama."','".$harga."','".$deskripsi."','".$gambar."')");
			}
		else{
			$result = mysql_query("UPDATE produk SET nama_produk='".$nama_produk."', harga='".$harga."', deskripsi='".$deskripsi."', gambar='".$gambar."' WHERE id=".$_GET['id']);
		$result = mysql_query($sqlstr) or die(mysql_error());
		
		//Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
		//if (empty($_REQUEST['id']))	kirimEmail($idKategori, $judul, $news);
		$confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";	
		}
	}

?>
        
<h2><center>Product Preview</center></h2>
<p></p>

<?php 

$data = array();
if(!empty($_GET['id']))
	{
	$result = mysql_query("SELECT * FROM produk AS p, kategori AS k WHERE id_produk='".mysql_real_escape_string($_GET['id'])."'AND 			    p.id_kategori = k.id_kategori") or die(mysql_error());
    $data = mysql_fetch_array($result);
	echo '<pre>';
	print_r($data);
	echo '</pre>';
    }
else
	{
	$data['nama_produk'] = '';
	$data['nama_kategori'] = '';
	$data['harga'] = '';
	//$data['deskripsi'] = '';
	$data['gambar'] = '';
	//print_r($data);
	}		 

?>

<form method = "POST" not enctype="multipart/form-data" action = "<?php $_SERVER['PHP_SELF']; ?>"> 
<table border="0" cellpadding="2">

        <tr>
        	<td>Nama</td>
        	<td><input name="nama" size="20px" type="text" value ="<?php echo $data['nama_produk']; ?>"/>
        </tr>
        <tr>
        	<td>Kategori</td>
         	<td><select name="kategori"><option value=<?php echo $data['nama_kategori']; ?> selected><?php echo $data['nama_kategori']; ?></option>
             			<?php 		
						
							$resultCOMBO = mysql_query("SELECT nama_kategori FROM kategori") or die(mysql_error());			while($dataCOMBO = mysql_fetch_array($resultCOMBO)){
							         echo "<option value={$dataCOMBO['nama_kategori']}>{$dataCOMBO['nama_kategori']}</option>";	
						}?>	
                </select>              
            </td>

       </tr>
        <tr>
        	<td>Harga</td>
            <td><input name="harga" type="text" value = <?php echo $data['harga']; ?>></td>
        </tr>
        <tr>
        	<td>Deskripsi</td>
            <td>
                    <textarea name="deskripsi" cols="30" rows="15"><?php echo isset($data['deskripsi']) ? $data['deskripsi'] : '';  ?></textarea></td>
                    <script type="text/javascript">
					var editor = CKEDITOR.replace('deskripsi');
			        </script>
        </tr>
        <tr>
        	<td>Gambar</td>
			<td><img src="foto_produk/<?php echo $data['gambar'];?>" width="30%" height="30%" /></td>            
        </tr>	
		<tr>
        	<td>Ganti Gambar</td>
            <td>
            <input name="gambar" type="file" value="Browse" /></td>
        </tr>	
        <tr>
        	<td></td>
            <td><br /><center><input name="save" type="submit" value="Simpan" /></center></td>
        </form>
        </tr>
        
	</tr>
    
 
   <?php // include('includes\footer.php'); ?>

    
<p>&nbsp;</p>
</body>
</html>

This form suppose to save data when I press simpan (translate: save) button. But in actual, it does not. I try to enter sample data and press "simpan" button and it does not save the data as it suppose to.

Why is it ?

Did you connect to a database?

$conn = mysql_connect("localhost", "root", "root"); 
mysql_select_db("example") or die (mysql_error());

If you don't connect to a database, you won't be able to store your information.

The method you form uses to send data to the server is POST.

So on line 28 use POST as opposed to the GET you've used.

Did you connect to a database?

$conn = mysql_connect("localhost", "root", "root"); 
mysql_select_db("example") or die (mysql_error());

If you don't connect to a database, you won't be able to store your information.

Yes, I do connect to the database. I have the code above the script that I posted.

The method you form uses to send data to the server is POST.

So on line 28 use POST as opposed to the GET you've used.

I replace all the $_GET with $_POST. I have this error:

Notice: Undefined index: gambar in C:\xampp\htdocs\Masterlink\cgoods\product_edit.php on line 86

Line 86: $gambar = $_POST;

(equal to line 32 in my post)

Use this

$nama = isset($_POST['nama']) ? $_POST['nama'] : '';	  
$kategori = isset($_POST['kategori']) ? $_POST['kategori'] : '';
$harga = isset($_POST['harga']) ? $_POST['harga'] : '';	  
$gambar = isset($_POST['gambar']) ? $_POST['gambar'] : '';	  
$deskripsi = isset($_POST['deskripsi']) ? $_POST['deskripsi'] : '';

and ignore this lower block as you've already assigned values to your variables.

$nama = $_GET['nama'];
$kategori = $_GET['kategori'];
$harga = $_GET['harga'];
$gambar = $_GET['gambar'];

can you add before insert query the code below and post output?

print_r($_GET);
die();

It does not show me anything. I place it here:

$nama = isset($_POST['nama']) ? $_POST['nama'] : '';	  
$kategori = isset($_POST['kategori']) ? $_POST['kategori'] : '';
$harga = isset($_POST['harga']) ? $_POST['harga'] : '';	  
$gambar = isset($_POST['gambar']) ? $_POST['gambar'] : '';	  
$deskripsi = isset($_POST['deskripsi']) ? $_POST['deskripsi'] : '';


// SETELAH TOMBOL SIMPAN DI TEKAN

if (isset($_GET['save'])){
		//$nama = $_POST['nama'];
		//$kategori = $_POST['kategori'];
		//$harga = $_POST['harga'];
		//$gambar = $_POST['gambar'];
		     
		print_r($_GET);die();
			 
		$tgl_masuk = date(j-m-y);
		
		if (empty($_GET['id'])){
			$result = mysql_query("INSERT INTO produk(nama_produk, harga, deskripsi, tgl_masuk, gambar) VALUES('".$nama."','".$harga."','".$deskripsi."','".$tgl_masuk."','".$gambar."')");
			}

if (isset($_GET)){
<form method = "POST"
Can you smell anything?

ok, I make it uniform to $_POST.

Array ( [nama] => New Product [kategori] => Air [harga] => 50000 [deskripsi] =>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vestibulum dui ut turpis molestie non rutrum dolor faucibus. Duis ullamcorper, augue at ullamcorper gravida, purus diam auctor velit, molestie rhoncus mi ante sed dui. Aliquam at orci nec odio elementum suscipit sit amet sit amet nisi. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nunc ullamcorper nisi non augue vulputate nec placerat quam placerat.

[save] => Simpan )

data looks fine, you can comment them out and see if your code works!

The insert works! just there is a little prob. in the date format.

I use this code to capture todays date:

$tgl_masuk = date("d-m-y");

I use this code to print out date:

<td><?php echo $data['tgl_masuk']."</br>";?></td>

The print out a little bit a mesh:

2018-11-11 ( 18 november 2011)

how to change the print out to 18-11-2011 ?

$theDate = time(); 
print "The date is:";
print date('d-m-Y',$theDate); 
print ("<br>");

This one prints 18-11-2011

If you want to print the current day use this one:

$theDate = time(); 
print "The date is:";
print date('D-M-Y',$theDate); 
print ("<br>");

The insert works! just there is a little prob. in the date format.

I use this code to capture todays date:

$tgl_masuk = date("d-m-y");

I use this code to print out date:

<td><?php echo $data['tgl_masuk']."</br>";?></td>

The print out a little bit a mesh:

2018-11-11 ( 18 november 2011)

how to change the print out to 18-11-2011 ?

PHP Manual is packed with examples on that
http://php.net/manual/en/function.date.php

I am changing the print out to:

<td><?php echo date('D-M-Y',$data['tgl_masuk'])."</br>";?></td>

Notice: A non well formed numeric value encountered in C:\xampp\htdocs\Masterlink\cgoods\product_preview.php on line 99
Thu-Jan-1970

I don't think it works well with array. What should I do ?

I don't think it is going to work well with arrays, since if you are not having options with speciffic dates, there is no point of doing it.

What exactly do you want to do?

I am trying to print out the date when the data product was first entered. The data is stored in the database and I am trying to print it in the correct format. In my database is stored yyyy-mm-dd instead of dd-mm-yyyy.

data looks fine, you can comment them out and see if your code works!

One more thing, the array does not save $gambar or picture. It is left blank my database.

I am trying to print out the date when the data product was first entered. The data is stored in the database and I am trying to print it in the correct format. In my database is stored yyyy-mm-dd instead of dd-mm-yyyy.

if all you do is just store/display the date and nothing else, then store the value of UNIX time i.e time and format it to your need on display

One more thing, the array does not save $gambar or picture. It is left blank my database.

trace it if it is in POST array (hint print_r($array)) and Check SQL if it gets included!

if all you do is just store/display the date and nothing else, then store the value of UNIX time i.e time and format it to your need on display

How to do so ?

One more problem, when I press edit in product_preview.php in product_edit.php suppose to retrieve all the data that I need to edit, but this time, it does not. It shows me a blank form.

I wonder why. Is it because I update all the $_GET to $_POST? I wonder if $_POST can retrieve or capture data.


product_edit.php

<?php 

$data = array();
if(!empty($_POST['id']))
	{
	$result = mysql_query("SELECT * FROM produk AS p, kategori AS k WHERE id_produk='".mysql_real_escape_string($_POST['id'])."'AND 			    p.id_kategori = k.id_kategori") or die(mysql_error());
    $data = mysql_fetch_array($result);
	echo '<pre>';
	print_r($data);
	//print_r($array);
	echo '</pre>';
    }
else
	{
	$data['nama_produk'] = '';
	$data['nama_kategori'] = '';
	$data['harga'] = '';
	//$data['deskripsi'] = '';
	$data['gambar'] = '';
	//print_r($data);
	}		 

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.