How to edit and delete the selected row

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

will require_once('suppliesEdit.php'); load a new window named suppliesEdit.php? What will require_once do?

well, here are some of my works in flash.
http://www.hydrolight.frih.net
http://www.whatsthelatest.net/sites/lfisher
http://www.whatsthelatest.net/sites/kaisei

Hope you like those
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: How to edit and delete the selected row

Post by Sindarin »

will require_once('suppliesEdit.php'); load a new window named suppliesEdit.php? What will require_once do?
No it will add the suppliesEdit.php code to your current page.

btw your sites are good, but I could barely see the portfolio previews. And in Philippines eh? Book me a room there! :D
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

thanks Sindarin. ahhhh..okay. So the require_once('suppliesEdit.php') must not contain any html codes? am i right?
Hey how do you find out the site?
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: How to edit and delete the selected row

Post by Sindarin »

So the require_once('suppliesEdit.php') must not contain any html codes?
it can include files only containing that code you want.
Hey how do you find out the site?
Check out my previous reply, I had edited it. :D
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

Ohh. Didn't see those. Thanks Sindarin. Really. Thank you very much.

Yep, coz i'm from philippines. where are you from? Do you have any published sites? Can i look at it? By the way the portfolio. It wasn't updated for about 2 years. :)
Sorry. Kinda busy now a days.

Thank you very much Sandarin. Thank you. In Filipino dialect, maraming salamat
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: How to edit and delete the selected row

Post by Sindarin »

You're welcome. :D
I am from Greece and can't say I am really proud of my online works right now as I work in a company which messes up my plans but some nice freelance works will be soon up.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

I have modified some of the codes and here is what i came up with. But still it can't update the selected row. Where have i been wrong?

supplies.php code:

Code: Select all

<?php  
$query = 'SELECT supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost FROM tbl_supplies';
$result = mysql_query($query) or die('Error in Query');
 
echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" >";
 
   while($row = mysql_fetch_assoc($result)){
        echo "<tr onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">";
        echo "<td width = \"60\" >".$row['code']."</td>";
        echo "<td width = \"140\">".$row['name']."</br></td>";
        echo "<td width = \"78\" align = \"right\">".$row['quantity']."&nbsp;</br></td>";
        echo "<td width = \"112\">&nbsp;".$row['measurement']."</br></td>";
        echo "<td width = \"116\" align = \"right\">".$row['perUnitCost']."</br></td>";
        echo "<td width = \"89\" align = \"right\">".$row['totalCost']."</br></td>";
        //edit and delete added here
        echo "<td width = \"90\" align = \"center\"><a href=\"[b]suppliesEdit.php?edit_id=".$row['supplies_PK'].[/b]"\"> Edit</a> | <a href=\"[b]suppliesDelete.php?delete_id=".$row['supplies_PK'][/b]."\"> Delete</a>";
        echo "</tr>";      
    }
    echo "</table>";      
?>
suppliesEdit.php code:

Code: Select all

      // get details of selected record
    if ($_GET && !$_POST) {
        if (isset($_GET['edit_id']) && is_numeric($_GET['edit_id'])) {
            $edit_id = $_GET['edit_id'];
        }
        else {
            $edit_id = NULL;
        }
        if ($edit_id) {
            $sql = "SELECT supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost FROM tbl_supplies WHERE supplies_PK = $edit_id";
            $result = mysql_query($sql) or die (mysql_error());
            $row = mysql_fetch_assoc($result);
                $did = $row['supplies_PK'];
                $dcode = $row['code'];
                $dname = $row['name'];
                $dquantity = $row['quantity'];
                $dmeasurement = $row['measurement'];
                $dperUnitCost = $row['perUnitCost'];
                $dtotalCost = $row['totalCost'];
                
                $ddperUnitCost = number_format($dperUnitCost, 2, '.', '');
                $ddtotalCost = number_format($dtotalCost, 2, '.', '');
        }
    }
and here is the form code for suppliesEdit.php

Code: Select all

<form id="form1" name="form1" method="post" action="suppliesUpdate.php"><input name="code" type="text" class="formFieldsNum" id="code" tabindex="1" value="<?php echo $dcode; ?>" size="18" maxlength="8" /></td>
                  <td><input name="name" type="text" class="formFieldsDescription" id="name" tabindex="2" value="<?php echo $dname; ?>" size="50" maxlength="100"/></td>
                  <td width="66"><input name="quantity" type="text" class="formFieldsUnit" id="quantity" tabindex="3" value="<?php echo $dquantity; ?>" maxlength="4"/></td>
                  <td>
                    <select name="measurement" size="1" class="formFieldsDropDown" id="measurement" tabindex="4" />
                      <option value="<?php echo $dmeasurement;?>"><?php echo $dmeasurement;?></option>
                      <option value="kilogram(s)">kilogram(s)</option>
                      <option value="gram(s)">gram(s)</option>
                      <option value="Liter(s)">Liter(s)</option>
                      <option value="milliLiter(s)">milliLiter(s)</option>
                      <option value="lb(s)">lb(s)</option>
                      <option value="pc(s)">pc(s)</option>
                      <option value="gallon(s)">gallon(s)</option>
                      <option value="sack(s)">sack(s)</option>
                      <option value="ounce(s)">ounce(s)</option>
                    </select>                  </td>
                  <td><input name="perUnitCost" type="text" class="formFieldsCost" id="perUnitCost" maxlength="7" tabindex="5" value="<?php echo $ddperUnitCost; ?>"/></td>
                  <td><input name="totalCost" type="text" class="formFieldsCost" id="totalCost" maxlength="7" tabindex="6" value="<?php echo $ddtotalCost; ?>"/></td>
                </tr>
                
                <tr>
                  <td>&nbsp;</td>
                  <td height="33">&nbsp;</td>
                  <td><input name="edit_id" type="hidden" id="hiddenField" value="<?php echo $did; ?>" /></td>
                  <td colspan="4"><div align="right">
                    <input name="update" type="submit" class="form-button" id="button2" value="Update Entry"/>
here is the code for suppliesUpdate.php

Code: Select all

        $updatecode = $_POST['code'];
        $updatename = $_POST['name'];
        $updatequantity = $_POST['quantity'];
        $updatemeasurement = $_POST['measurement'];
        $updateperUnitCost = $_POST['perUnitCost'];
        $updatetotalCost = $_POST['totalCost'];
        
        $sql = "UPDATE tbl_supplies SET code = '$updatecode', name = '$updatename', quantity = '$updatequantity', measurement = '$updatemeasurement', perUnitCost = '$updateperUnitCost',  totalCost = '$updatetotalCost' WHERE supplies_PK = '$edit_id'";
        
        $result = mysql_query($sql) or die(mysql_error());
        //show message and redirect if successfully update
        if($result){
            $updateSuccess = "<b><font color = \"green\">SUCCESS</font></b>: Entry Updated.";
            header ("Refresh: 3; supplies.php");
        }
        else {
            $updateError = "<b><font color = \"red\">ERROR</font></b>: Unable to update entry.";    
        }
The sequence:
1. i view the supplies.php (displays rows of data from the database, also enables the user to add a new record)
2. i select the row that i want to edit the values and click edit. (it will load suppliesEdit.php?edit_id="")
3. when i'm in the suppliesEdit.php page, i edit some values and clicked update entry button, i received my confirmation message which says SUCCESS: entry updated. and then it will redirect me to supplies.php
4. but when i'm in the supplies.php page, it doens't show up, my edited values aren't showing.

Please help. Anyone, please help.
Thank you in advance. May GOD bless you.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: How to edit and delete the selected row

Post by Sindarin »

Change suppliesUpdate.php to this:

Code: Select all

 
$edit_id=$_GET[id];
$updatecode = $_POST['code'];
        $updatename = $_POST['name'];
        $updatequantity = $_POST['quantity'];
        $updatemeasurement = $_POST['measurement'];
        $updateperUnitCost = $_POST['perUnitCost'];
        $updatetotalCost = $_POST['totalCost'];
       
        $sql = "UPDATE tbl_supplies SET code = '$updatecode', name = '$updatename', quantity = '$updatequantity', measurement = '$updatemeasurement', perUnitCost = '$updateperUnitCost',  totalCost = '$updatetotalCost' WHERE supplies_PK = '$edit_id'";
       
        $result = mysql_query($sql) or die(mysql_error());
        //show message and redirect if successfully update
        if($result){
            $updateSuccess = "<b><font color = \"green\">SUCCESS</font></b>: Entry with ID: $edit_id Updated.";
            header ("Refresh: 3; supplies.php");
        }
        else {
            $updateError = "<b><font color = \"red\">ERROR</font></b>: Unable to update entry.";   
        }
and tell me how it works and what ID it outputs when updated.
I can't see anywhere you GET the id to use for the $edit_id variable, so I added it. Remember when you click a link with a value (e.g. index.php?id=5) remember it still will reload the page meaning you will need to GET the id again (unless you use AJAX I suppose.)
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

Hello Sindarin. Thanks for the response. Here is what i get when i click update entry.
SUCCESS: Entry with ID: Updated.

here is the code:

Code: Select all

        $entry_id = $_GET['entry_id'];
        $updatecode = $_POST['code'];
        $updatename = $_POST['name'];
        $updatequantity = $_POST['quantity'];
        $updatemeasurement = $_POST['measurement'];
        $updateperUnitCost = $_POST['perUnitCost'];
        $updatetotalCost = $_POST['totalCost'];
       
        $sql = "UPDATE tbl_supplies SET code = '$updatecode', name = '$updatename', quantity = '$updatequantity', measurement = '$updatemeasurement', perUnitCost = '$updateperUnitCost',  totalCost = '$updatetotalCost' WHERE supplies_PK = '$entry_id'";
       
        $result = mysql_query($sql) or die(mysql_error());
        //show message and redirect if successfully update
        if($result){
            $updateSuccess = "<b><font color = \"green\">SUCCESS</font></b>: Entry with ID: $entry_id Updated.";
            header ("Refresh: 3; supplies.php");
        }
        else {
            $updateError = "<b><font color = \"red\">ERROR</font></b>: Unable to update entry.";  
        }
the entry_id is from supplies.php code:

Code: Select all

<a href=\"suppliesEdit.php?entry_id=".$row['supplies_PK']."\">
and in the suppliesEdit.php:

Code: Select all

<?php
    //start session
    session_start();
    
    //include mysql_connection.php to connect to the database
    require_once("connectSupplies.php");
    
    if(!isset($_SESSION['user'])){
        header("location:index.php");
    }   
    
    // get details of selected record
    if ($_GET && !$_POST) {
        if (isset($_GET['entry_id']) && is_numeric($_GET['entry_id'])) {
            $entry_id = $_GET['entry_id'];
        }
        else {
            $entry_id = NULL;
        }
        if ($entry_id) {
            $sql = "SELECT supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost FROM tbl_supplies WHERE supplies_PK = $entry_id";
            $result = mysql_query($sql) or die (mysql_error());
            $row = mysql_fetch_assoc($result);
                $did = $row['supplies_PK'];
                $dcode = $row['code'];
                $dname = $row['name'];
                $dquantity = $row['quantity'];
                $dmeasurement = $row['measurement'];
                $dperUnitCost = $row['perUnitCost'];
                $dtotalCost = $row['totalCost'];
                
                //$ddperUnitCost = number_format($dperUnitCost, 2, '.', '');
                //$ddtotalCost = number_format($dtotalCost, 2, '.', '');
        }
    }
        // close connection
        mysql_close();
?>
Thank GOD your here...Thanks a lot
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: How to edit and delete the selected row

Post by Sindarin »

It doesn't get the ID at all so to update the row. It should say e.g. SUCCESS: Entry with ID:2 Updated.

try getting the entry id like this:

Code: Select all

$get_id=$row['supplies_PK'];
and change,
<a href=\'suppliesEdit.php?entry_id="$get_id"\'>
edit: for some reason the php field displayed wrong quotes.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

Where should i apply that code Sindarin? in the supplies.php?
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

Sindarin are you there? I have put your code in the supplies.php page. Still nothing happened...
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

Anyone please help....

in my suppliesEdit.php i tried to echo the $entry_id. It says 3, but when i echoed the $entry_id in suppliesUpdate.php it doesn't display a thing.

Any help will be greatly appreciated. Thank you so much.

here is the code for suppliesEdit.php

Code: Select all

if ($_GET && !$_POST) {
        if (isset($_GET['entry_id']) && is_numeric($_GET['entry_id'])) {
            $entry_id = $_GET['entry_id'];
        }
        else {
            $entry_id = NULL;
        }
        if ($entry_id) {
            $sql = "SELECT supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost FROM tbl_supplies WHERE supplies_PK = $entry_id";
            $result = mysql_query($sql) or die (mysql_error());
            $row = mysql_fetch_assoc($result);
                $did = $row['supplies_PK'];
                $dcode = $row['code'];
                $dname = $row['name'];
                $dquantity = $row['quantity'];
                $dmeasurement = $row['measurement'];
                $dperUnitCost = $row['perUnitCost'];
                $dtotalCost = $row['totalCost'];
                
                echo $entry_id;             
        }
    }
and here is the code for suppliesUpdate.php

Code: Select all

$entry_id = $_GET['entry_id'];
        $updatecode = $_POST['code'];
        $updatename = $_POST['name'];
        $updatequantity = $_POST['quantity'];
        $updatemeasurement = $_POST['measurement'];
        $updateperUnitCost = $_POST['perUnitCost'];
        $updatetotalCost = $_POST['totalCost'];
        echo $entry_id;
       
        $sql = "UPDATE $tbl_name SET code='$updatecode', name='$updatename', quantity='$updatequantity', measurement='$updatemeasurement', perUnitCost='$updateperUnitCost',  totalCost='$updatetotalCost' WHERE supplies_PK='$entry_id'";
       
        $result = mysql_query($sql) or die(mysql_error());
        //show message and redirect if successfully update
        if($result){
            $updateSuccess = "<b><font color = \"green\">SUCCESS</font></b>: Entry with ID:". $entry_id." Updated.";
            header ("Refresh: 3; supplies.php");
        }
        else {
            $updateError = "<b><font color = \"red\">ERROR</font></b>: Unable to update entry.";  
        }
Please help me.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: How to edit and delete the selected row

Post by Sindarin »

edit:

Play a bit with those scripts,

index.php

Code: Select all

<?php  
 
//connect to database - you should have a script like this with sql host/username/password
require_once('database-connect.php');
 
switch($_GET['action']){
//detect and delete entry goes here
case 'delete':
//get the supply's id
$deleteid=$_GET[id];
require_once('delete.php');
exit;
//end of delete entry
break;
//detect and edit entry goes here
case 'edit':
//get the supply's id
$editid=$_GET[id];
require_once('edit.php');
exit;
//end of edit entry
break;
default:
//nothing to be done here
}
 
$query = 'SELECT supplies_PK, code, name, quantity, measurement, perUnitCost, totalCost FROM tbl_supplies';
$result = mysql_query($query) or die('Error in Query');
 
echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" >";
 
   while($row = mysql_fetch_assoc($result)){
        echo "<tr onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">";
        echo "<td width = \"60\" >".$row['code']."</td>";
        echo "<td width = \"140\">".$row['name']."</br></td>";
        echo "<td width = \"78\" align = \"right\">".$row['quantity']."&nbsp;</br></td>";
        echo "<td width = \"112\">&nbsp;".$row['measurement']."</br></td>";
        echo "<td width = \"116\" align = \"right\">".$row['perUnitCost']."</br></td>";
        echo "<td width = \"89\" align = \"right\">".$row['totalCost']."</br></td>";
        //edit and delete added here
        echo "<td width = \"90\" align = \"center\"><a href=\"index.php?action=edit&id=".$row['supplies_PK']."\"> Edit</a> | <a href=\"index.php?action=delete&id=".$row['supplies_PK']."\"> Delete</a>";
        echo "</tr>";      
    }
    echo "</table>";      
?>
edit.php

Code: Select all

<?
//get the current selected ID use $id to update the current row
$id=$_GET[id];
 
//start edit
echo "Editing entry ID: $id<br/> Add some fields here!";
 
?>
delete.php

Code: Select all

<?
//get the current selected ID
$id=$_GET[id];
 
//start edit
echo "I will delete entry ID: $id here! No..really.. I can do it...<br/>";
 
?>
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: How to edit and delete the selected row

Post by zplits »

thanks Sindarin. I don't have any problem with delete. My prob is with the edit. I can't the $entry_id into the suppliesUpdate.php

here is the code for suppliesUpdate.php

Code: Select all

$updatecode = $_POST['code'];
        $updatename = $_POST['name'];
        $updatequantity = $_POST['quantity'];
        $updatemeasurement = $_POST['measurement'];
        $updateperUnitCost = $_POST['perUnitCost'];
        $updatetotalCost = $_POST['totalCost'];
        echo $entry_id;
     
             $editNow = $_GET['entry_id'];
                   $sql = "UPDATE $tbl_name SET code='$updatecode', name='$updatename', quantity='$updatequantity', measurement='$updatemeasurement', perUnitCost='$updateperUnitCost',  totalCost='$updatetotalCost' WHERE supplies_PK='$editNow'";
       
        $result = mysql_query($sql) or die(mysql_error());
            
        //show message and redirect if successfully update
        if($result){
            $updateSuccess = "<b><font color = \"green\">SUCCESS</font></b>: Entry with ID:". $entry_id." Updated.";
            header ("Refresh: 3; supplies.php");
        }
        else {
            $updateError = "<b><font color = \"red\">ERROR</font></b>: Unable to update entry.";  
        }
Post Reply