473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to create dynamically edit table entries

i wish to know how to create a dinamically edit table entries .can u help me ?thanks.
Sep 29 '06 #1
11 9261
ronverdonk
4,258 Expert 4TB
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
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
4,258 Expert 4TB
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
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
4,258 Expert 4TB
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
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
4,258 Expert 4TB
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
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
4,258 Expert 4TB
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
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
4,258 Expert 4TB
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

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

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
10
by: nick_faye | last post by:
Hi guys, i'm still a newbie in using MS Access and in VB programming. I am using DAO connection from my VB to access the entries on my MS Access table. I am having trouble in editting and...
0
by: goitz | last post by:
Im looking for a way to dynamically create a table and append a text box to it using a list of information from a database eg> display a results of a select statment then add a text box--> field1...
0
by: neoteny2 | last post by:
I need MS Access to automatically create reports/subreports based on specific criteria. I am building a database in Access 2003 with different locations/sites. I have the "sites" table created...
2
by: news reader | last post by:
Hi, Does anoone of you know if there is already a simple application doing something like this. I would enhance / tune the missing features, but would like to avoid to start from scratch or...
3
by: dauphian | last post by:
Greetings, I have a user control, that I will need to display on a specific page based on the number of records in database: I have to query a site table to get the number of entries as well...
11
by: bharathmngl | last post by:
now iam trying to add rows dynamically into the table when i click "ADD ROW" button. It should also have the option to delete the selected row. So Please help me to find code.... And also please...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.