0

product_edit.php

// SETELAH TOMBOL SIMPAN DI TEKAN

if (isset($_POST['save'])){

			 
	$tgl_masuk = date("y-m-j");
		
	if (empty($_POST['id'])){
$result = mysql_query("INSERT INTO produk(nama_produk, harga, deskripsi, tgl_masuk, gambar) VALUES('".$nama."','".$harga."','".$deskripsi."','".$tgl_masuk."','".$gambar."')");
			
echo "Data has been recorded in the database.";
		}
	else{
	$result = mysql_query("UPDATE produk SET nama_produk='".$nama_produk."', harga='".$harga."', deskripsi='".$deskripsi."', gambar='".$gambar."' WHERE id=".$_POST['id']);
	$result = mysql_query($sqlstr) or die(mysql_error());
		
	$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);
	//print_r($array);
	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>

Hi, I am trying to insert the following data into table product:

nama_produk, harga, deskripsi, tgl_masuk, gambar

There are 2 datas that I failed to insert:

1. id_kategori (I have not include in the insert since in the form only shows nama_kategori which id's is shown in kategori table.). I do not know how to insert id_kategori since the form only shows me nama_kategori (kategori table shows the information regarding the kategori id from nama_kategori)

2. gambar (or picture). I wonder why when I checked the table the file information is not there yet.

Please help. Thanks.

2
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by cereal
0

Each one of these nama_produk, harga, deskripsi, tgl_masuk, gambar should refer to a field name in the form from which you send information to save, in the PHP script you need to use the $_POST array in order to connect them to variables:

$nama = $_POST['nama'];
$harga = $_POST['harga'];
$deskripsi = $_POST['deskripsi'];
// and so on

In your form tag you wrote "not" remove that.
In order to store an image directly into the database you need to:

a) create a blob field in the table database
b) use $_FILES array to collect info about the uploaded file
c) use fread()

Search php insert image to mysql blob you will find a lot of tutorials on this. A suggestion, save only the name of the file in the database. Bye.

Edited by cereal: n/a

0

From line 4 to 20 of your previous code:

if (isset($_POST['save'])){
 
	$tgl_masuk = date("y-m-j");
 
	if (empty($_POST['id'])){
           $result = mysql_query("INSERT INTO produk(nama_produk, harga, deskripsi, tgl_masuk, gambar) VALUES('".$nama."', '".$harga."', '".$deskripsi."', '".$tgl_masuk."', '".$gambar."')");
           echo "Data has been recorded in the database.";
	} else {
	   $result = mysql_query("UPDATE produk SET nama_produk='".$nama_produk."', harga='".$harga."', deskripsi='".$deskripsi."', gambar='".$gambar."' WHERE id=".$_POST['id']);
	   $result = mysql_query($sqlstr) or die(mysql_error());
	   $confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";	
	}
}

Change it with this:

if (isset($_POST['save']) && $_FILES['userfile']['size'] > 0){
 
 	$nama = $_POST['nama'];
	$harga = $_POST['harga'];
	$deskripsi = $_POST['deskripsi'];
	$tgl_masuk = date("y-m-j");

        # get values from uploaded file
	$fileName = $_FILES['userfile']['name'];
	$tmpName  = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];
	
        # read uploaded file, so you can save it to the database
	$fp = fopen($tmpName, 'r');
	$gambar = fread($fp, filesize($tmpName));
	$gambar = addslashes($content); # image to save
	fclose($fp);

        # if you just want to save the name of the image, remove previous four lines,
        # uncomment below and switch gambar field from blob to varchar
        /*
        $uploads_dir = "/images"; # this directory needs write permissions
        $gambar = $fileName;
        move_uploaded_file($tmpName, "$uploads_dir/$fileName");
        */

	if (empty($_POST['id'])){
           $result = mysql_query("INSERT INTO produk(nama_produk, harga, deskripsi, tgl_masuk, gambar) VALUES('".$nama."', '".$harga."', '".$deskripsi."', '".$tgl_masuk."', '".$gambar."')");
           echo "Data has been recorded in the database.";
	}
	else
        {
	   $result = mysql_query("UPDATE produk SET nama_produk='".$nama_produk."', harga='".$harga."', deskripsi='".$deskripsi."', gambar='".$gambar."' WHERE id=".$_POST['id']);
	   $result = mysql_query($sqlstr) or die(mysql_error());
           $confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";	
	}
}

And if you still want to save images to database, add a field for the filename to your table. Bye.

0

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

Line 88 is the first line in your post. What is the filename ?

I attached the file so perhaps you can take a look. I do not know if it works in your machine. hehe.

Attachments
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin</title>
<link href="admin-style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="includes/navstyleadmin.css" />
<script type="text/javascript" src="includes/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="includes/superfish.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js">
<link href="ckeditor/content.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
jQuery(function(){
	jQuery('ul.menu').superfish({
		animation: {opacity:'show',height:'show'},
		speed: 'slow', 
		autoArrows: true
	});
});
</script>

<style type="text/css">
<!--
.style1 {font-size: 12px}
-->
</style>
</head>

<body>
<div id="header">
      <p><a href="#">Home </a>| <a href="index.php">Logout </a>| <a href="admin.php">Admin Panel</a></p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
    </div>
    
    <div id="search">
      
      <form action="search.php" method="post" name="login" target="_self" id="login" style="style.css" title="search">
      	<br>
        <label>search :
        <input type="text" name="search" id="search2" />
</label>
        <br />
                    
      </form> 		
	</div>
    
    <div id="menu">
            
<?php include ("includes/navigation_admin.php"); ?>
<?php include ("includes/koneksi.php"); ?>        
	 </div>     
        
<?php
session_start();

if ( !isset($_SESSION['username']) )
{
header("location:index.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'] : '';


// SETELAH TOMBOL SIMPAN DI TEKAN

if (isset($_POST['save']) && $_FILES['userfile']['size'] > 0){  	
 
 	$nama = $_POST['nama'];
	$harga = $_POST['harga'];
	$deskripsi = $_POST['deskripsi'];
	$tgl_masuk = date("y-m-j");

        # get values from uploaded file
	$fileName = $_FILES['userfile']['name'];
	$tmpName  = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];
	
        # read uploaded file, so you can save it to the database
	$fp = fopen($tmpName, 'r');
	$gambar = fread($fp, filesize($tmpName));
	$gambar = addslashes($content); # image to save
	fclose($fp);

        # if you just want to save the name of the image, remove previous four lines,
        # uncomment below and switch gambar field from blob to varchar
        /*
        $uploads_dir = "/images"; # this directory needs write permissions
        $gambar = $fileName;
        move_uploaded_file($tmpName, "$uploads_dir/$fileName");
        */

	if (empty($_POST['id'])){
           $result = mysql_query("INSERT INTO produk(nama_produk, harga, deskripsi, tgl_masuk, gambar) VALUES('".$nama."', '".$harga."', '".$deskripsi."', '".$tgl_masuk."', '".$gambar."')");
           echo "Data has been recorded in the database.";
	}
	else
        {
	   $result = mysql_query("UPDATE produk SET nama_produk='".$nama_produk."', harga='".$harga."', deskripsi='".$deskripsi."', gambar='".$gambar."' WHERE id=".$_POST['id']);
	   $result = mysql_query($sqlstr) or die(mysql_error());
           $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);
	//print_r($array);
	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>
0

Userfile is the name of the input field from which you upload the file. In your form the input field name (line 195 of your attached file) is gambar, so or you change that name or you change all the $_FILES reference from userfile to gambar. Bye :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.