i wish to know how to create a dinamically edit table entries .can u help me ?thanks.
how to create dynamically edit table entries
Collapse
X
-
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(S QL_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($_REQUES T['PHASE'])) {
if ($_REQUEST['PHASE'] == 2 && !isset($_REQUES T['i'])) {
echo 'Error phase 2 input parm'; exit;
}
if ($_REQUEST['PHASE'] == 3 && !isset($_REQUES T['i']) && !isset($_REQUES T['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($sq lst) or die(mysql_error ());
echo 'Entry updated<br />';
echo '<a href="change.ph p">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_que ry($sqlst) or die(mysql_error ());
echo '<table border="1"><tr> <th>Action</th><th>Email adress</th></tr>';
while ($row = mysql_fetch_ass oc($rows)) {
echo '<tr><td><a href="change.ph p?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.Originally posted by ronverdonkAs 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(S QL_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($_REQUES T['PHASE'])) {
if ($_REQUEST['PHASE'] == 2 && !isset($_REQUES T['i'])) {
echo 'Error phase 2 input parm'; exit;
}
if ($_REQUEST['PHASE'] == 3 && !isset($_REQUES T['i']) && !isset($_REQUES T['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($sq lst) or die(mysql_error ());
echo 'Entry updated<br />';
echo '<a href="change.ph p">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_que ry($sqlst) or die(mysql_error ());
echo '<table border="1"><tr> <th>Action</th><th>Email adress</th></tr>';
while ($row = mysql_fetch_ass oc($rows)) {
echo '<tr><td><a href="change.ph p?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 didn't inderstant about
if (isset($_REQUES T['PHASE'])) {
Is it means i have to put the ID that i want to edit into the PHASE make it become if (isset($_REQUES T['$ID'])) ?Comment
-
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.
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.Code:CREATE TABLE emailads (id INT(2) PRIMARY KEY AUTO_INCREMENT, email VARCHAR(30) DEFAULT ' ');
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:Comment
-
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 itOriginally posted by ronverdonkNono 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.
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.Code:CREATE TABLE emailads (id INT(2) PRIMARY KEY AUTO_INCREMENT, email VARCHAR(30) DEFAULT ' ');
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:
<?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($_GE T['id']);
//required file for database connection
require("config .php");
echo "<table border='1'>";
// construct SQL statement
$sql = mysql_query("SE LECT *FROM iteminfomation WHERE ID='$id'");
$sql1=mysql_que ry("SELECT NameOfTools,Bra nd FROM listofitem WHERE ID='$id'");
$information1=m ysql_fetch_arra y($sql1);
$information=my sql_fetch_array ($sql);
echo "<table border='1'>";
echo "<tr><td>NamaOf Tools</th><td>Brand</th><td>Date</th>Quantity</th><td Price</th><td>Price</th></tr>";
echo"<tr><td>{$ information1['NameOfTools']}</td>
<td>{$informati on1['Brand']}</td>
<td>{$informati on['Date']}</td>
<td>{$informati on['Quantity']}</td>
<td>{$informati on['Price']}</td>
</table>";
}Comment
-
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
what column of what table would you like to change?Code:echo"<tr><td>{$information1['NameOfTools']}</td> <td>{$information1['Brand']}</td> <td>{$information['Date']}</td> <td>{$information['Quantity']}</td> <td>{$information['Price']}</td> </table>"; }
Ronald :cool:Comment
-
ok. let say i want to edit the table of iteminformation column of Quantity.Is it imposiblle to change data in two table?Originally posted by ronverdonkWith 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
what column of what table would you like to change?Code:echo"<tr><td>{$information1['NameOfTools']}</td> <td>{$information1['Brand']}</td> <td>{$information['Date']}</td> <td>{$information['Quantity']}</td> <td>{$information['Price']}</td> </table>"; }
Ronald :cool:Comment
-
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:Comment
-
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 thatOriginally posted by ronverdonkYou 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:
Notice: Undefined index: PHASE in C:\server\Apach e2\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($_REQUES T['PHASE'])) {
if ($_REQUEST['PHASE'] == 2 && !isset($_REQUES T['i'])) {
echo 'Error phase 2 input parm'; exit;
}
if ($_REQUEST['PHASE'] == 3 && !isset($_REQUES T['i']) && !isset($_REQUES T['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='$N ameOfTools' WHERE id=" . $_REQUEST['i'];
mysql_query($sq lst) or die(mysql_error ());
echo 'Entry updated<br />';
echo '<a href="change.ph p">Click here to go back</a>';
exit;
} // End PHASE 3
else {Comment
-
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
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.Code:$sqlst = "UPDATE listofitem SET NameOfTools='$NameOfTools' WHERE id='" . $_REQUEST['i']."'";
Ronald :cool:Comment
-
So sorry to make u diffucult.Originally posted by ronverdonkIs 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
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.Code:$sqlst = "UPDATE listofitem SET NameOfTools='$NameOfTools' WHERE id='" . $_REQUEST['i']."'";
Ronald :cool:
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($_GE T['id']);
//required file for database connection
require("config .php");
$information=my sql_fetch_array ($sql);
echo "<table border='1'>";
echo "<tr><td Name</th><td >Brand</th></tr>";
echo"<tr><td>{$ information1['Name']}</td>
<td>{$informati on1['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_profi le.php?ret=gene ral>General Information</a><br>";
if ($_GET['ret'] == '' && $_GET['edit'] == '' ){
echo "<font color=ultra>*Se lect 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("SE LECT * FROM system_user WHERE userId='{$_SESS ION['userid']}'")
or die(mysql_error ());
$general_row = mysql_fetch_arr ay( $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_profi le.php?edit=gen eral><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("SE LECT * FROM system_user WHERE userId='{$_SESS ION['userid']}'")
or die(mysql_error ());
$general_row = mysql_fetch_arr ay( $general_result ); //set $row to result
//table to display general user inforamtion
echo "<font size=4 color=blue>Gene ral 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_userNa me 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_profi le.php?edit=gen eral_ok> OK <a/>";
//echo $_GET['txt_userName'];
echo "Hello...";
}
if ($_GET['edit'] == 'general_ok' ){
$general_update = "UPDATE system_user SET WHERE userId='{$_SESS ION['userid']}'";
mysql_query($ge neral_update) or die(mysql_error ());
//$general_row = mysql_fetch_arr ay( $general_update );
echo "Are you sure to save the changes? <a href=user_profi le.php?edit=gen eral_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".Comment
-
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:
andyes.i oly want to edit one row of a table. How to make it?
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.i wish to know how to create a dinamically edit table entries .can u help me ?thanks
Now let's get back to that requirement or stop this discussion.
Ronald :cool:Comment
Comment