www.webdeveloper.com
Results 1 to 8 of 8

Thread: Help for making edit database page through PHP

  1. #1
    Join Date
    Apr 2014
    Posts
    6

    Help for making edit database page through PHP

    Hello all PHP master,

    I am new in PHP code and I need help for making edit page for database entry through PHP. I have 3 php script here :


    data-barang.php

    Code:
    <?php	
    	require 'header.php';
    
    	require 'panel.php';
    ?>
    
    <div id="content" class="right">
    	<div id="title">
        	<div class="contenttitle">
        		<h1>Data Barang</h1>
        	</div>
        	<div class="topmenu">
        		<ul>
            		<li><a href="module/add-data-barang.php"><img class="icontopmenu left" src="image/add.png" title="Data Baru" alt="Data Baru"> Tambah Data</a></li>
            		<li><a href="module/edit-data-barang.php"><img class="icontopmenu left" src="image/edit.png" title="Edit Data" alt="Edit Data"> Edit Data</a></li>
            		<li><a href="module/delete-data-barang.php"><img class="icontopmenu left" src="image/delete.png" title="Hapus Data" alt="Hapus Data"> Hapus Data</a></li>
            	</ul>
        	</div>
        </div>
        <?php
    		require 'connection.php';
    	
    		$per_page = 10;
    	
    		$result = mysql_query("SELECT barang.*, jenis_barang.* FROM barang, jenis_barang WHERE barang.id_jenis_barang = jenis_barang.id_jenis_barang");
    		$total_results = mysql_num_rows($result);
    		$total_pages = ceil($total_results / $per_page);
    
    		if (isset($_GET['page']) && is_numeric($_GET['page'])) {
    			$show_page=$_GET['page'];
    		
    			if ($show_page > 0 && $show_page <= $total_pages) {
    			$start = ($show_page -1) * $per_page;
    			$end = $start + $per_page; 
    			}
    			else {
    				$start = 0;
    				$end = $per_page; 
    			}		
    		}
    		else {
    			$start = 0;
    			$end = $per_page; 
    		}
    	
    		echo "<p><b>View Page:</b> ";
    		for ($i = 1; $i <= $total_pages; $i++) {
    			echo "<a href='view-paginated.php?page=$i'>$i</a> ";
    		}
    		echo "</p>";
    		
    		echo "<table class='tabledata'>";
    		echo "<tr>
    			<th>ID Barang</th>
    			<th>Nama Barang</th>
    			<th>ID Jenis Barang</th>
    			<th>Jenis Barang</th>
    			<th>Stok Barang</th>
    			<th>Harga Beli</th>
    			<th>Harga Jual</th>
    		</tr>";
    
    		for ($i = $start; $i < $end; $i++) {
    			if ($i == $total_results) {break;}
    			echo "<tr>";
    			echo '<td>'. mysql_result($result, $i, 'id_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'nama_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'id_jenis_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'nama_jenis_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'stok_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'harga_beli').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'harga_jual').'</td>';
    			echo '<td><a href="edit-data-barang.php?id_barang='. mysql_result($result, $i, 'id_barang'). '">Edit</a></td>';
    			echo '<td><a href="delete-data-barang.php?id_barang='. mysql_result($result, $i, 'id_barang'). '">Delete</a></td>';
    			
    			echo "</tr>";
    		}
    		echo "</table>"; 
    	?>
    </div>
    <div class="clear"></div>
        
    <?php
    	require 'footer.php';
    ?>

    add-data-barang.php

    Code:
    <div>
    	<h2>Tambah Data Barang</h2>
    	<form class="formdata" action="" method="post">
    		<fieldset>
    			<p><label>ID Barang</label>: <input type="text" name="idbarang" value="" /></p>
                <p><label>Nama Barang</label>: <input type="text" name="namabarang" value="" /></p>
    			<p><label>ID Jenis Barang</label>: <input type="text" name="idjenisbarang" value="" /></p>
    			<p><label>Stok Barang</label>: <input type="text" name="stokbarang" value="" /></p>
                <p><label>Harga Beli</label>: <input type="text" name="hargabeli" value="" /></p>
                <p><label>Harga Jual</label>: <input type="text" name="hargajual" value="" /></p>
    			<p><input type="submit" name="submit" value="Submit"></p>
    		</fieldset>
    	</form> 
    </div>
    <?php 
    	require ('../connection.php');
    	
    	if (isset($_POST['submit'])) {
    		$idbarang = mysql_real_escape_string(htmlspecialchars($_POST['idbarang']));
    		$namabarang = mysql_real_escape_string(htmlspecialchars($_POST['namabarang']));
    		$idjenisbarang = mysql_real_escape_string(htmlspecialchars($_POST['idjenisbarang']));
    		$stokbarang = mysql_real_escape_string(htmlspecialchars($_POST['stokbarang']));
    		$hargabeli = mysql_real_escape_string(htmlspecialchars($_POST['hargabeli']));
    		$hargajual = mysql_real_escape_string(htmlspecialchars($_POST['hargajual']));
    		
    		if ($idbarang==''|| $namabarang==''|| $idjenisbarang==''|| $stokbarang==''|| $hargabeli==''|| $hargajual=='') {
    			$error='ERROR: Please fill in all required fields!';
    		}
    		else {
    			mysql_query("INSERT barang SET id_barang='$idbarang', nama_barang='$namabarang', id_jenis_barang='$idjenisbarang', stok_barang='$stokbarang', harga_beli='$hargabeli', harga_jual='$hargajual'")
    			or die(mysql_error());
    			
    			header("Location: ../data-barang.php"); 
    		}
    	}
    ?>

    edit-data-barang.php

    Code:
    <div>
    	<h2>Edit Data Barang</h2>
    	<form class="formdata" action="" method="post">
    		<fieldset>
    			<p><label>ID Barang</label>: <input type="text" name="idbarang" value="<?php echo $idbarang; ?>" /></p>
                <p><label>Nama Barang</label>: <input type="text" name="namabarang" value="<?php echo $namabarang; ?>" /></p>
    			<p><label>ID Jenis Barang</label>: <input type="text" name="idjenisbarang" value="<?php echo $idjenisbarang; ?>" /></p>
    			<p><label>Stok Barang</label>: <input type="text" name="stokbarang" value="<?php echo $stokbarang; ?>" /></p>
                <p><label>Harga Beli</label>: <input type="text" name="hargabeli" value="<?php echo $hargabeli; ?>" /></p>
                <p><label>Harga Jual</label>: <input type="text" name="hargajual" value="<?php echo $hargajual; ?>" /></p>
    			<p><input type="submit" name="submit" value="Submit"></p>
    		</fieldset>
    	</form> 
    </div>
    <?php
    	require ('../connection.php');
    	
    	if (isset($_POST['submit'])) {
    		if (is_numeric($_POST['id_barang'])) {
    			$idbarang = mysql_real_escape_string(htmlspecialchars($_POST['idbarang']));
    			$namabarang = mysql_real_escape_string(htmlspecialchars($_POST['namabarang']));
    			$idjenisbarang = mysql_real_escape_string(htmlspecialchars($_POST['idjenisbarang']));
    			$stokbarang = mysql_real_escape_string(htmlspecialchars($_POST['stokbarang']));
    			$hargabeli = mysql_real_escape_string(htmlspecialchars($_POST['hargabeli']));
    			$hargajual = mysql_real_escape_string(htmlspecialchars($_POST['hargajual']));
    			
    			if ($idbarang == ''|| $namabarang == ''|| $idjenisbarang == ''|| $stokbarang == ''|| $hargabeli == ''|| $hargajual == '') {
    				$error = 'ERROR: Please fill in all required fields!';
    			}
    			else {
    				mysql_query("UPDATE barang SET id_barang='$idbarang', nama_barang='$namabarang', id_jenis_barang='$idjenisbarang, stok_barang='$stokbarang', harga_beli='$hargabeli, harga_jual='$hargajual WHERE id_barang='$id'")
    				or die(mysql_error()); 
    				header("Location: ../data-barang.php");
    			}
    		}
     		else {
    			echo 'Error!';
    		}
    	}
    	else {
    		if (isset($_GET['id_barang'])) {
    			$id = $_GET['id_barang'];
    			$result = mysql_query("SELECT * FROM barang WHERE id_barang=$id")
    			or die(mysql_error());
    			$row = mysql_fetch_array($result);
    			
    			if($row) {
    				$idbarang = $row['id_barang'];
    				$namabarang = $row['nama_barang'];
    				$idjenisbarang = $row['id_jenis_barang'];
    				$stokbarang = $row['stok_barang'];
    				$hargabeli = $row['harga_beli'];
    				$hargajual = $row['harga_jual'];
    			}
    			else {
    				echo "No results!";
    			}
    		}
    		else {
    			echo 'Error!';
    		}
    	}
    ?>

    The add-data-barang.php has been worked normally. But when I click "edit button" in mysql database list, I got an error message :

    Code:
    Object not found!
    
    The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
    
    If you think this is a server error, please contact the webmaster.
    Error 404
    localhost
    Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
    Also I haven't test my edit-data-barang.php. Any help would be appreciate. Thanks in advance..

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    My best guess without more info would be the header() redirect is failing.
    PHP Code:
    header("Location: ../data-barang.php"); 
    Either the path is wrong, or it's just plain failing due to the HTTP 1.1 specification that a redirect header should use a fully qualified URI, e.g.:
    PHP Code:
    header("Location: http://www.your_site.com/path/to/data-barang.php"); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2014
    Posts
    49
    I agree with NogDog, however I'd like to mention the following should work too.

    PHP Code:
    header("Location: /path/to/data-barang.php"); 
    I almost never add the domain (http://www.your_site.com) and it's never caused any complaints or issues at all.

    I do this as I've been known to have the same site running on a few different domains at the same time. (I have good reasons in this case)

    Just remember to have the "/" at the front and put the whole remainder of the address after the domain.

  4. #4
    Join Date
    Apr 2014
    Posts
    6
    Thank you, it works. Also I need some more help to built my edit page. Here is my new code :


    data-barang.php

    Code:
    <?php	
    	require 'header.php';
    
    	require 'panel.php';
    ?>
    
    <div id="content" class="right">
    	<div id="title">
        	<div class="contenttitle">
        		<h1>Data Barang</h1>
        	</div>
        	<div class="topmenu">
        		<ul>
            		<li><a href="module/add-data-barang.php"><img class="icontopmenu left" src="image/add.png" title="Data Baru" alt="Data Baru"> Tambah Data</a></li>
            		<li><a href="module/edit-data-barang.php"><img class="icontopmenu left" src="image/edit.png" title="Edit Data" alt="Edit Data"> Edit Data</a></li>
            		<li><a href="module/delete-data-barang.php"><img class="icontopmenu left" src="image/delete.png" title="Hapus Data" alt="Hapus Data"> Hapus Data</a></li>
            	</ul>
        	</div>
        </div>
        <?php
    		require 'connection.php';
    	
    		$per_page = 10;
    	
    		$result = mysql_query("SELECT barang.*, jenis_barang.* FROM barang, jenis_barang WHERE barang.id_jenis_barang = jenis_barang.id_jenis_barang");
    		$total_results = mysql_num_rows($result);
    		$total_pages = ceil($total_results / $per_page);
    
    		if (isset($_GET['page']) && is_numeric($_GET['page'])) {
    			$show_page=$_GET['page'];
    		
    			if ($show_page > 0 && $show_page <= $total_pages) {
    			$start = ($show_page -1) * $per_page;
    			$end = $start + $per_page; 
    			}
    			else {
    				$start = 0;
    				$end = $per_page; 
    			}		
    		}
    		else {
    			$start = 0;
    			$end = $per_page; 
    		}
    	
    		echo "<p><b>View Page:</b> ";
    		for ($i = 1; $i <= $total_pages; $i++) {
    			echo "<a href='view-paginated.php?page=$i'>$i</a> ";
    		}
    		echo "</p>";
    		
    		echo "<table class='tabledata'>";
    		echo "<tr>
    			<th>Kode Barang</th>
    			<th>Nama Barang</th>
    			<th>ID Jenis Barang</th>
    			<th>Jenis Barang</th>
    			<th>Stok Barang</th>
    			<th>Harga Beli</th>
    			<th>Harga Jual</th>
    		</tr>";
    
    		for ($i = $start; $i < $end; $i++) {
    			if ($i == $total_results) {break;}
    			echo "<tr>";
    			echo '<td>'. mysql_result($result, $i, 'kode_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'nama_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'kode_jenis_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'nama_jenis_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'stok_barang').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'harga_beli').'</td>';
    			echo '<td>'. mysql_result($result, $i, 'harga_jual').'</td>';
    			echo '<td><a href="module/edit-data-barang.php?id_barang='. mysql_result($result, $i, 'id_barang'). '">Edit</a></td>';
    			echo '<td><a href="module/delete-data-barang.php?id_barang='. mysql_result($result, $i, 'id_barang'). '">Delete</a></td>';
    			
    			echo "</tr>";
    		}
    		echo "</table>"; 
    	?>
    </div>
    <div class="clear"></div>
    
    <?php
    	require 'footer.php';
    ?>

    edit-data-barang.php

    Code:
    <?php
    	require ('../connection.php');
    	
    	if (isset($_POST['submit'])) {
    		if (is_numeric($_POST['id_barang'])) {
    			$kodebarang = mysql_real_escape_string(htmlspecialchars($_POST['kodebarang']));
    			$namabarang = mysql_real_escape_string(htmlspecialchars($_POST['namabarang']));
    			$idjenisbarang = mysql_real_escape_string(htmlspecialchars($_POST['idjenisbarang']));
    			$stokbarang = mysql_real_escape_string(htmlspecialchars($_POST['stokbarang']));
    			$hargabeli = mysql_real_escape_string(htmlspecialchars($_POST['hargabeli']));
    			$hargajual = mysql_real_escape_string(htmlspecialchars($_POST['hargajual']));
    			
    			if ($kodebarang == ''|| $namabarang == ''|| $idjenisbarang == ''|| $stokbarang == ''|| $hargabeli == ''|| $hargajual == '') {
    				$error = 'ERROR: Please fill in all required fields!';
    			}
    			else {
    				mysql_query("UPDATE barang SET kode_barang='$kodebarang', nama_barang='$namabarang', id_jenis_barang='$idjenisbarang, stok_barang='$stokbarang', harga_beli='$hargabeli, harga_jual='$hargajual WHERE id_barang='$id'")
    				or die(mysql_error()); 
    				header("Location: ../data-barang.php");
    			}
    		}
     		else {
    			echo 'Error!';
    		}
    	}
    	else {
    		if (isset($_GET['id_barang']) && is_numeric($_GET['id_barang']) && $_GET['id_barang'] > 0) {
    			$id = $_GET['id_barang'];
    			$result = mysql_query("SELECT * FROM barang WHERE id_barang='$id'")
    			or die(mysql_error());
    			$row = mysql_fetch_array($result);
    			
    			if($row) {
    				$kodebarang = $row['kode_barang'];
    				$namabarang = $row['nama_barang'];
    				$idjenisbarang = $row['id_jenis_barang'];
    				$stokbarang = $row['stok_barang'];
    				$hargabeli = $row['harga_beli'];
    				$hargajual = $row['harga_jual'];
    			}
    			else {
    				echo "No results!";
    			}
    		}
    		else {
    			echo 'Error!';
    		}
    	}
    ?>
    
    <div>
    	<h2>Edit Data Barang</h2>
    	<form class="formdata" action="" method="post">
    		<fieldset>
    			<p><label>Kode Barang</label>: <input type="text" name="kodebarang" value="<?php echo $kodebarang; ?>" /></p>
                <p><label>Nama Barang</label>: <input type="text" name="namabarang" value="<?php echo $namabarang; ?>" /></p>
    			<p><label>ID Jenis Barang</label>: <input type="text" name="idjenisbarang" value="<?php echo $idjenisbarang; ?>" /></p>
    			<p><label>Stok Barang</label>: <input type="text" name="stokbarang" value="<?php echo $stokbarang; ?>" /></p>
                <p><label>Harga Beli</label>: <input type="text" name="hargabeli" value="<?php echo $hargabeli; ?>" /></p>
                <p><label>Harga Jual</label>: <input type="text" name="hargajual" value="<?php echo $hargajual; ?>" /></p>
    			<p><input type="submit" name="submit" value="Submit"></p>
    		</fieldset>
    	</form> 
    </div>
    I got this error message when I submit an edit form :

    Code:
    Notice: Undefined index: id_barang in C:\xampp\htdocs\application\module\edit-data-barang.php on line 5
    Error!

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    539
    So - you have a bad index in an array ref probably. Look at line 5 like it says.

  6. #6
    Join Date
    May 2014
    Posts
    49
    Your form is listing the variable as idbarang

    PHP Code:
    <p><label>ID Barang</label>: <input type="text" name="idbarang" value="" /></p
    however your php script is listing it as id_barang

    PHP Code:
    if (is_numeric($_POST['id_barang'])) { 
    So the script is not find it as they don't match.

  7. #7
    Join Date
    Apr 2014
    Posts
    6
    Quote Originally Posted by Gravy View Post
    Your form is listing the variable as idbarang

    PHP Code:
    <p><label>ID Barang</label>: <input type="text" name="idbarang" value="" /></p
    however your php script is listing it as id_barang

    PHP Code:
    if (is_numeric($_POST['id_barang'])) { 
    So the script is not find it as they don't match.
    Wow, I just realize that. Thanks gravy it works perfectly..

  8. #8
    Join Date
    May 2014
    Posts
    49
    Quote Originally Posted by preloode View Post
    Wow, I just realize that. Thanks gravy it works perfectly..
    You're most welcome, it's often the simplest of things that are the hardest to notice. The better you become, the more you'll find these are still where most of your errors come up.

    I also noticed you use mysql_real_escape_string which is part of the MySQL function set. Unfortunately this is now depreciated (outdated) and should not be used as it will be removed completely from a future version of PHP.

    You should instead use MySQLi or PDO_MySQL.

    http://us3.php.net/manual/en/book.mysqli.php
    http://us3.php.net/manual/en/ref.pdo-mysql.php

    Keep learning and practising and I'm sure you'll do great and come back and help others too.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles