/    Sign up×
Community /Pin to ProfileBookmark

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 :

[B]data-barang.php[/B]

[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’;
?>
[/CODE]

[B]add-data-barang.php[/B]

[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”);
}
}
?>
[/CODE]

[B]edit-data-barang.php[/B]

[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!’;
}
}
?>
[/CODE]

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
[/CODE]

Also I haven’t test my edit-data-barang.php. Any help would be appreciate. Thanks in advance..

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 07.2014 — My best guess without more info would be the header() redirect is failing.
[code=php]
header("Location: ../data-barang.php");
[/code]

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.:
[code=php]
header("Location: http://www.your_site.com/path/to/data-barang.php");
[/code]
Copy linkTweet thisAlerts:
@GravyJun 07.2014 — I agree with NogDog, however I'd like to mention the following should work too.

[code=php]
header("Location: /path/to/data-barang.php");
[/code]


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.
Copy linkTweet thisAlerts:
@preloodeauthorJun 08.2014 — Thank you, it works. Also I need some more help to built my edit page. Here is my new code :


[B]data-barang.php[/B]

[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';
?>
[/CODE]



[B]edit-data-barang.php[/B]

[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>
[/CODE]


I got this error message when I submit an edit form :

[CODE]
Notice: Undefined index: id_barang in C:xampphtdocsapplicationmoduleedit-data-barang.php on line 5
Error!
[/CODE]
Copy linkTweet thisAlerts:
@ginerjmJun 08.2014 — So - you have a bad index in an array ref probably. Look at line 5 like it says.
Copy linkTweet thisAlerts:
@GravyJun 08.2014 — Your form is listing the variable as [B]idbarang[/B]

[code=php]
<p><label>ID Barang</label>: <input type="text" name="idbarang" value="" /></p>
[/code]


however your php script is listing it as [B]id_barang[/B]

[code=php]
if (is_numeric($_POST['id_barang'])) {
[/code]


So the script is not find it as they don't match.
Copy linkTweet thisAlerts:
@preloodeauthorJun 09.2014 — Your form is listing the variable as [B]idbarang[/B]

[code=php]
<p><label>ID Barang</label>: <input type="text" name="idbarang" value="" /></p>
[/code]


however your php script is listing it as [B]id_barang[/B]

[code=php]
if (is_numeric($_POST['id_barang'])) {
[/code]


So the script is not find it as they don't match.[/QUOTE]


Wow, I just realize that. Thanks gravy it works perfectly..
Copy linkTweet thisAlerts:
@GravyJun 09.2014 — Wow, I just realize that. Thanks gravy it works perfectly..[/QUOTE]

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 [B]mysql_real_escape_string[/B] 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.
×

Success!

Help @preloode spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...