By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,107 Members | 673 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,107 IT Pros & Developers. It's quick & easy.

how to create dynamically edit table entries

P: 34
i wish to know how to create a dinamically edit table entries .can u help me ?thanks.
Sep 29 '06 #1
Share this Question
Share on Google+
11 Replies

ronverdonk
Expert 2.5K+
P: 4,258
As always the question is far more simple then the answer, but I'll give it a try.
The following sample is a 1-script processing 3 phases, i.e.
PHASE 1 : the db data is displayed in a table (one table row per db row) and prefixed with a 'Change' button;
PHASE 2 : the data of the selected row is shown in a <textarea> field that can be modified, along with a SAVE button;
PHASE 3 : the changed row is updated in the database.

There is no value cleansing in the sample, BUT DON'T forget it!
[PHP]<?php
/**
* Change an existing email adress in in a table row. MySql table setup:
* CREATE TABLE emailads (id INT(2) PRIMARY KEY AUTO_INCREMENT, email VARCHAR(30) DEFAULT ' ');
* Parms are passed in the $_GET or $_POST arrays, so they are extracted via $_REQUEST
*
* no parameters Display table with email adress prefixed by a Change link
* When 'Change' clicked: call this script, pass id of table entry
* $_REQUEST['PHASE'] == 2
* ['i'] : the id of the table row to be changed.
* Display table. On the spot of the to-be-changed row
* show a text input field with value from the 'old' row
* 'Save' clicked: Call this script:
* 'PHASE'(=3), 'i' (id), 'e' (new email adress)
* $_REQUEST['PHASE'] == 3
* ['i'] : the id of the row-to-change
* ['e'] : the changed email adress
* UPDATE row
*/
//--------------------------------------------------------------------------
// Open database connection (for all phases)
//--------------------------------------------------------------------------
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die("No connection to server: ".mysql_error());
mysql_select_db(SQL_DB) or die("No db connection: ".mysql_error());
//--------------------------------------------------------------------------
// Form is submitted, check parms
// PHASE 1 no parms passed
// PHASE 2 '' passed
// PHASE 3 'i' and 'e' passed
//--------------------------------------------------------------------------
if (isset($_REQUEST['PHASE'])) {
if ($_REQUEST['PHASE'] == 2 && !isset($_REQUEST['i'])) {
echo 'Error phase 2 input parm'; exit;
}
if ($_REQUEST['PHASE'] == 3 && !isset($_REQUEST['i']) && !isset($_REQUEST['e']) ) {
echo 'Error phase 3 input parm'; exit;
}
}
//--------------------------------------------------------------------------
// PHASE 3 - update row in database
//--------------------------------------------------------------------------
if ($_REQUEST['PHASE'] == 3) {
$email = trim(strip_tags($_REQUEST['e']));
// check content: error: issue error message and die
$sqlst = "UPDATE emailads SET email='$email' WHERE id=" . $_REQUEST['i'];
mysql_query($sqlst) or die(mysql_error());
echo 'Entry updated<br />';
echo '<a href="change.php">Click here to go back</a>';
exit;
} // End PHASE 3
else {
//------------------------------------------------------------------------
// PHASE 1 and PHASE 2:
// Select data from database and show on screen
// PHASE 2: Pass email adress in HTML textbox and click SAVE
//------------------------------------------------------------------------
$sqlst = "SELECT id, email from emailads ORDER BY email";
$rows=mysql_query($sqlst) or die(mysql_error());
echo '<table border="1"><tr><th>Action</th><th>Email adress</th></tr>';
while ($row = mysql_fetch_assoc($rows)) {
echo '<tr><td><a href="change.php?PHASE=2&i='.$row['id'].'">Change</a></td>';
echo '<td>';
if ($_REQUEST['PHASE'] == 2 && $row['id'] == $_REQUEST['i'] ) {
?>
Change text and click "SAVE":
<form action="change.php" method="POST">
<textarea name="e" cols="30" rows="1"><?php echo $row['email'] ?> </textarea>
<input type="hidden" name="i" value=<?php echo $row['id'] ?> />
<input type="hidden" name="PHASE" value=3 />
<input type="submit" value="SAVE" />
</form>
<?php }
else
echo $row['email'];
echo '</td></tr>';

} // End FOREACH

echo '</table><hr />';
echo '</body></html>';

} // End ELSE ...
?>[/PHP]

Ronald :cool:
Sep 29 '06 #2

P: 34
As always the question is far more simple then the answer, but I'll give it a try.
The following sample is a 1-script processing 3 phases, i.e.
PHASE 1 : the db data is displayed in a table (one table row per db row) and prefixed with a 'Change' button;
PHASE 2 : the data of the selected row is shown in a <textarea> field that can be modified, along with a SAVE button;
PHASE 3 : the changed row is updated in the database.

There is no value cleansing in the sample, BUT DON'T forget it!
[PHP]<?php
/**
* Change an existing email adress in in a table row. MySql table setup:
* CREATE TABLE emailads (id INT(2) PRIMARY KEY AUTO_INCREMENT, email VARCHAR(30) DEFAULT ' ');
* Parms are passed in the $_GET or $_POST arrays, so they are extracted via $_REQUEST
*
* no parameters Display table with email adress prefixed by a Change link
* When 'Change' clicked: call this script, pass id of table entry
* $_REQUEST['PHASE'] == 2
* ['i'] : the id of the table row to be changed.
* Display table. On the spot of the to-be-changed row
* show a text input field with value from the 'old' row
* 'Save' clicked: Call this script:
* 'PHASE'(=3), 'i' (id), 'e' (new email adress)
* $_REQUEST['PHASE'] == 3
* ['i'] : the id of the row-to-change
* ['e'] : the changed email adress
* UPDATE row
*/
//--------------------------------------------------------------------------
// Open database connection (for all phases)
//--------------------------------------------------------------------------
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die("No connection to server: ".mysql_error());
mysql_select_db(SQL_DB) or die("No db connection: ".mysql_error());
//--------------------------------------------------------------------------
// Form is submitted, check parms
// PHASE 1 no parms passed
// PHASE 2 '' passed
// PHASE 3 'i' and 'e' passed
//--------------------------------------------------------------------------
if (isset($_REQUEST['PHASE'])) {
if ($_REQUEST['PHASE'] == 2 && !isset($_REQUEST['i'])) {
echo 'Error phase 2 input parm'; exit;
}
if ($_REQUEST['PHASE'] == 3 && !isset($_REQUEST['i']) && !isset($_REQUEST['e']) ) {
echo 'Error phase 3 input parm'; exit;
}
}
//--------------------------------------------------------------------------
// PHASE 3 - update row in database
//--------------------------------------------------------------------------
if ($_REQUEST['PHASE'] == 3) {
$email = trim(strip_tags($_REQUEST['e']));
// check content: error: issue error message and die
$sqlst = "UPDATE emailads SET email='$email' WHERE id=" . $_REQUEST['i'];
mysql_query($sqlst) or die(mysql_error());
echo 'Entry updated<br />';
echo '<a href="change.php">Click here to go back</a>';
exit;
} // End PHASE 3
else {
//------------------------------------------------------------------------
// PHASE 1 and PHASE 2:
// Select data from database and show on screen
// PHASE 2: Pass email adress in HTML textbox and click SAVE
//------------------------------------------------------------------------
$sqlst = "SELECT id, email from emailads ORDER BY email";
$rows=mysql_query($sqlst) or die(mysql_error());
echo '<table border="1"><tr><th>Action</th><th>Email adress</th></tr>';
while ($row = mysql_fetch_assoc($rows)) {
echo '<tr><td><a href="change.php?PHASE=2&i='.$row['id'].'">Change</a></td>';
echo '<td>';
if ($_REQUEST['PHASE'] == 2 && $row['id'] == $_REQUEST['i'] ) {
?>
Change text and click "SAVE":
<form action="change.php" method="POST">
<textarea name="e" cols="30" rows="1"><?php echo $row['email'] ?> </textarea>
<input type="hidden" name="i" value=<?php echo $row['id'] ?> />
<input type="hidden" name="PHASE" value=3 />
<input type="submit" value="SAVE" />
</form>
<?php }
else
echo $row['email'];
echo '</td></tr>';

} // End FOREACH

echo '</table><hr />';
echo '</body></html>';

} // End ELSE ...
?>[/PHP]

Ronald :cool:
I am very sorry that i still not well understand the code you had submit.
I didn't inderstant about

if (isset($_REQUEST['PHASE'])) {

Is it means i have to put the ID that i want to edit into the PHASE make it become if (isset($_REQUEST['$ID'])) ?
Sep 29 '06 #3

ronverdonk
Expert 2.5K+
P: 4,258
Nono don't do that! Read the explanation at the top of the script carefully before you change the code. As I said, this program contains 3 phases, so it passes the phase number with the GET or POST. DO NOT CHANGE THAT unless you know what you are doing! You can see in the second textline in the source code on what table this code is working and was tested.
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE emailads (id INT(2) PRIMARY KEY AUTO_INCREMENT, email VARCHAR(30) DEFAULT ' ');
  2.  
And the value of column 'id' is passed as i= and the value of column email is passed as e= in the $_REQUEST array in this script.

If you have a different table, as I am sure you do, you must change the column names in the script accordingly. In this sample it changes column 'email' and uses the 'id' column to pass and select the information from the database.

If you still have problems, show me the table columns you use and I will show you how to change it.

Ronald :cool:
Sep 29 '06 #4

P: 34
Nono don't do that! Read the explanation at the top of the script carefully before you change the code. As I said, this program contains 3 phases, so it passes the phase number with the GET or POST. DO NOT CHANGE THAT unless you know what you are doing! You can see in the second textline in the source code on what table this code is working and was tested.
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE emailads (id INT(2) PRIMARY KEY AUTO_INCREMENT, email VARCHAR(30) DEFAULT ' ');
  2.  
And the value of column 'id' is passed as i= and the value of column email is passed as e= in the $_REQUEST array in this script.

If you have a different table, as I am sure you do, you must change the column names in the script accordingly. In this sample it changes column 'email' and uses the 'id' column to pass and select the information from the database.

If you still have problems, show me the table columns you use and I will show you how to change it.

Ronald :cool:
This is the code and table column i use. When use click to EDIT then will edit the one row of information display. Hope that u can teach me how to do it
<?php

// check if id is passed to this routine
if (isset($_GET['id'])) {
// if yes: remove harmful tags and save the passed id in variable $id
$id = strip_tags($_GET['id']);

//required file for database connection
require("config.php");
echo "<table border='1'>";
// construct SQL statement
$sql = mysql_query("SELECT *FROM iteminfomation WHERE ID='$id'");
$sql1=mysql_query("SELECT NameOfTools,Brand FROM listofitem WHERE ID='$id'");
$information1=mysql_fetch_array($sql1);
$information=mysql_fetch_array($sql);

echo "<table border='1'>";
echo "<tr><td>NamaOfTools</th><td>Brand</th><td>Date</th>Quantity</th><td Price</th><td>Price</th></tr>";


echo"<tr><td>{$information1['NameOfTools']}</td>
<td>{$information1['Brand']}</td>
<td>{$information['Date']}</td>
<td>{$information['Quantity']}</td>
<td>{$information['Price']}</td>
</table>";
}
Sep 29 '06 #5

ronverdonk
Expert 2.5K+
P: 4,258
With the sample code I provided you with you can update 1 row in 1 table. It does not allow you to update 2 rows in 2 tables!
Seeing your display
Expand|Select|Wrap|Line Numbers
  1. echo"<tr><td>{$information1['NameOfTools']}</td>
  2. <td>{$information1['Brand']}</td>
  3. <td>{$information['Date']}</td>
  4. <td>{$information['Quantity']}</td>
  5. <td>{$information['Price']}</td>
  6. </table>";
  7. }
  8.  
what column of what table would you like to change?

Ronald :cool:
Sep 29 '06 #6

P: 34
With the sample code I provided you with you can update 1 row in 1 table. It does not allow you to update 2 rows in 2 tables!
Seeing your display
Expand|Select|Wrap|Line Numbers
  1. echo"<tr><td>{$information1['NameOfTools']}</td>
  2. <td>{$information1['Brand']}</td>
  3. <td>{$information['Date']}</td>
  4. <td>{$information['Quantity']}</td>
  5. <td>{$information['Price']}</td>
  6. </table>";
  7. }
  8.  
what column of what table would you like to change?

Ronald :cool:
ok. let say i want to edit the table of iteminformation column of Quantity.Is it imposiblle to change data in two table?
Sep 29 '06 #7

ronverdonk
Expert 2.5K+
P: 4,258
You replace the 'email' column and value in the sample by the name and value of the column you want to change.

What else you request is, according to me, beyond the scope of this forum. This forum is to help people help themselves e.g. 'show the way', provide samples, give guidance, point to documentation, even educate a bit, etc. But it is not intended to write full-fledged applications.

In my opinion, the sample I provided here is tested and works for updating one column value in a selected db row via an html table. If you want to extend that function to more columns or even 2 tables, go ahead. I will help you when you have questions.

Ronald :cool:
Sep 29 '06 #8

P: 34
You replace the 'email' column and value in the sample by the name and value of the column you want to change.

What else you request is, according to me, beyond the scope of this forum. This forum is to help people help themselves e.g. 'show the way', provide samples, give guidance, point to documentation, even educate a bit, etc. But it is not intended to write full-fledged applications.

In my opinion, the sample I provided here is tested and works for updating one column value in a selected db row via an html table. If you want to extend that function to more columns or even 2 tables, go ahead. I will help you when you have questions.

Ronald :cool:
i am really want to help myself.but i really cant understand wat u mean. I had replace the column i want to change but the error occur saying that

Notice: Undefined index: PHASE in C:\server\Apache2\htdocs\New1.php on line 45

i dont understand why .
I replace like below.please forgive me because i cannot get what u had teach me.

if (isset($_REQUEST['PHASE'])) {
if ($_REQUEST['PHASE'] == 2 && !isset($_REQUEST['i'])) {
echo 'Error phase 2 input parm'; exit;
}
if ($_REQUEST['PHASE'] == 3 && !isset($_REQUEST['i']) && !isset($_REQUEST['e']) ) {
echo 'Error phase 3 input parm'; exit;
}
}
//--------------------------------------------------------------------------
// PHASE 3 - update row in database
//--------------------------------------------------------------------------
if ($_REQUEST['PHASE'] == 3) {
$NameOfTools = trim(strip_tags($_REQUEST['e']));
// check content: error: issue error message and die
$sqlst = "UPDATE listofitem SET NameOfTools='$NameOfTools' WHERE id=" . $_REQUEST['i'];
mysql_query($sqlst) or die(mysql_error());
echo 'Entry updated<br />';
echo '<a href="change.php">Click here to go back</a>';
exit;
} // End PHASE 3
else {
Sep 29 '06 #9

ronverdonk
Expert 2.5K+
P: 4,258
Is your id field in your db row numeric or character? rEMEMBER: In the example it is a numeric, so the sql statement defined is something like "... WHERE id=. $_REQUEST['i']". When it is a character field, you must enclose this id in quotes, such as
Expand|Select|Wrap|Line Numbers
  1. $sqlst = "UPDATE listofitem SET NameOfTools='$NameOfTools' WHERE id='" . $_REQUEST['i']."'";
If the id is numeric, you must have changed the code in some other place. In that case, please show it exactly as you have tested it.

Ronald :cool:
Sep 29 '06 #10

P: 34
Is your id field in your db row numeric or character? rEMEMBER: In the example it is a numeric, so the sql statement defined is something like "... WHERE id=. $_REQUEST['i']". When it is a character field, you must enclose this id in quotes, such as
Expand|Select|Wrap|Line Numbers
  1. $sqlst = "UPDATE listofitem SET NameOfTools='$NameOfTools' WHERE id='" . $_REQUEST['i']."'";
If the id is numeric, you must have changed the code in some other place. In that case, please show it exactly as you have tested it.

Ronald :cool:
So sorry to make u diffucult.
Now i already understand what did u mean in the above code. But it seen like not suitable with what i want to create. So now i have another idea is that create a form that can edit by user corresponding to the ID andinformation that display as the code below


if (isset($_GET['id'])) {
$id = strip_tags($_GET['id']);
//required file for database connection
require("config.php");

$information=mysql_fetch_array($sql);
echo "<table border='1'>";
echo "<tr><td Name</th><td >Brand</th></tr>";

echo"<tr><td>{$information1['Name']}</td>
<td>{$information1['Brand']}</td>;
echo "</table>";

And then i had found an example like below


<?php
//required file for database connection
require 'config.php' ;
//user photo display

echo "<a href=user_profile.php?ret=general>General Information</a><br>";

if ($_GET['ret'] == '' && $_GET['edit'] == '' ){
echo "<font color=ultra>*Select the user profile on the User Profile menu above</font>";
}

if ($_GET['ret'] == 'general' ){

//User General Information Retriving
//find the user
$general_result = mysql_query("SELECT * FROM system_user WHERE userId='{$_SESSION['userid']}'")
or die(mysql_error());
$general_row = mysql_fetch_array( $general_result ); //set $row to result

//table to display general user inforamtion
echo "General Information";
echo "Staff ID: " . $general_row['userId'] ."</td></tr>";
echo "<tr><td width=20%>Staff Name</td><td width=65%>: ".$general_row['userName'] ."</td></tr>";
echo "<tr><td width=65%><br>
<a href=user_profile.php?edit=general><img src=images/edit.jpg border=0></td></tr>";
echo "</table>"
}//end selection on general information retriving

//User general edit mode
if ($_GET['edit'] == 'general' ){

//User General Information Edit Mode
//find the user
$general_result = mysql_query("SELECT * FROM system_user WHERE userId='{$_SESSION['userid']}'")
or die(mysql_error());
$general_row = mysql_fetch_array( $general_result ); //set $row to result

//table to display general user inforamtion

echo "<font size=4 color=blue>General Information: </font>";
echo "<font size=4 color=Red>Edit Mode</font><br><br>";
echo "<table width=75% border=0>";

echo "<tr><td width=20%>Staff ID</td><td width=65%>: " . $general_row['userId'] ."</td></tr>";

echo "<tr><td width=20%>Staff Name</td><td width=65%>:
<input name=txt_userName type=text value='" . $general_row['userName'] ."'></td></tr>";
}//end selection on general information edit


if ($_GET['edit'] == 'gen_confirm' ){
echo "Are you sure to save the changes? <a href=user_profile.php?edit=general_ok> OK <a/>";
//echo $_GET['txt_userName'];
echo "Hello...";
}
if ($_GET['edit'] == 'general_ok' ){
$general_update = "UPDATE system_user SET WHERE userId='{$_SESSION['userid']}'";
mysql_query($general_update) or die(mysql_error());
//$general_row = mysql_fetch_array( $general_update );
echo "Are you sure to save the changes? <a href=user_profile.php?edit=general_ok> OK <a/>";
}
?>

Do u think it is suitable for me to follow this format?If yes how do i do. I had try to follo tat code but it seen like didnt work because error : "unfind index edit".
Sep 30 '06 #11

ronverdonk
Expert 2.5K+
P: 4,258
I am sorry, but you have lost me again. Whatever happened to your original request of changing one row in a table? It is like aiming at a moving target. You keep changing your requirements. What you requested was:
yes.i oly want to edit one row of a table. How to make it?
and
i wish to know how to create a dinamically edit table entries .can u help me ?thanks
for which the sample code was presented to you.WIth some php knowledge you can change that sample and I can help you with that but don't bring in a completely different piece of code now.

Now let's get back to that requirement or stop this discussion.

Ronald :cool:
Sep 30 '06 #12

Post your reply

Sign in to post your reply or Sign up for a free account.