473,414 Members | 1,688 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,414 software developers and data experts.

Selecting Table Rows for update, edit or delete

27
Hi!

Have been hitting my head on the wall for two weeks now. am a php newbie. have a form that on submitting to a MySQL DB instantly generates a HTML table on the same page. what i need now is way to be able to select rows from this table. specific selected row info sent back to text-box in my multi-button form for further processing depending on which button i click. have been trying to study javascript onclcik event handlers to see if this could be the solution. my scripts for multi-button form and generated table below:

1. Form:
Expand|Select|Wrap|Line Numbers
  1. <table id = "MAINFORM">
  2. <form name = "complaint_settings" method = "POST" action = "php_complaint_mysql.php">
  3.  
  4. <td>
  5. <!--<div align = "left"></div> -->
  6. <table border = "3" bordercolor = "gold" bgcolor = "00008B">
  7. <tr><td>
  8.  
  9. <p style = "color:gold">Complaint: <br>
  10. <input type = "text" size = "50" maxlength = "50" name = "complaint"></td></tr><br>
  11. <tr><td>
  12. <input type = "submit" name = "submit_c" value = "submit"> <br>
  13. <input type = "submit" name = "submit_c" value = "update"> <br>
  14. <input type = "submit" name = "submit_c" value = "delete" onClick = "return confirm('Are you sure you want to delete record?')">
  15.  
  16. </td></tr>
  17.  
  18. 2. Generated Table based on submission from above form:
  19.  
  20. //initializing extraction of complaint parameters from mysql
  21. $complaintTable = mysql_query("SELECT * FROM complaint") or die (mysql_error());
  22.  
  23. //store complaint records into array $complaint_row &&
  24. //create table of mysql complaint array
  25.                         echo "<table>";
  26.                         echo "<td><div>";"</div></td>";
  27.                         echo "<table id = \"table_c\" border = \"4\" bordercolor = 00008B>";
  28.                         echo "<tr><th>Complaint</th>";
  29.                         while ($comp_rec= mysql_fetch_array($complaintTable))
  30.                         {echo "<tr><td>"; echo $comp_rec['complaint']; echo "</td>";}
  31.                         echo "</tr></table>";
[Please use CODE tags when posting source code. Thanks! --pbmods]

THANKS IN ADVANCE.
Jul 5 '07 #1
8 31341
epots9
1,351 Expert 1GB
do u know AJAX? Tutorial

that way using your javascript u can call php files. (different php files for delete, update, etc.)
Jul 5 '07 #2
prosad
27
do u know AJAX? Tutorial

that way using your javascript u can call php files. (different php files for delete, update, etc.)
sorry, dont know AJAX yet.
just starting out with PHP, MySQL, Firebird and it seems am now being forced to learn javascritp just for this purpose on recovering text item from my HTML table.
Jul 5 '07 #3
code green
1,726 Expert 1GB
Give these buttons different names then you can test which one was clicked to submit the form
Expand|Select|Wrap|Line Numbers
  1. <input type = "submit" name = "select" value = "select"> <br>
  2. <input type = "submit" name = "update" value = "update"> <br>
  3. <input type = "submit" name = "delete" value = "delete"
AJAX is for wimps. Then in the script called by "action" [PHP]//php_complaint_mysql.php
if(isset($_POST['select']))
//do lots of select stuff
if(isset($_POST['delete']))
//do lots of delete stuff
if(isset($_POST['update']))
//do lots of update stuff [/PHP] Bit more tidying up to do but this is the idea
Jul 5 '07 #4
prosad
27
Give these buttons different names then you can test which one was clicked to submit the form
Expand|Select|Wrap|Line Numbers
  1. <input type = "submit" name = "select" value = "select"> <br>
  2. <input type = "submit" name = "update" value = "update"> <br>
  3. <input type = "submit" name = "delete" value = "delete"
AJAX is for wimps. Then in the script called by "action" [PHP]//php_complaint_mysql.php
if(isset($_POST['select']))
//do lots of select stuff
if(isset($_POST['delete']))
//do lots of delete stuff
if(isset($_POST['update']))
//do lots of update stuff [/PHP] Bit more tidying up to do but this is the idea
thanks for the tip. my different buttons work fine.
the BIG Question though i have is that how do i retrieve exactly already submitted (posted) data that is being generated and displayed in my html table immediately after posting. the rows on my table are not clickable. i wish to click on any row on my table and data/item on that row will be returned to the form (text field) it was originally submitted from.
html form and php, mysql generated table all shown in my source code. i wish to use form for submit, update, delete functions on my MySQL database through this single form and table. table to be used for just reselcting already posted data. form to be used for processing data (retrieved from my table).
Jul 5 '07 #5
code green
1,726 Expert 1GB
i wish to click on any row on my table and data/item on that row will be returned to the form (text field) it was originally submitted from
PHP can handle this very well. You need to wrap your data display line in a hyperlink, with the link returning the same page and your data being passed as a URL variable.
I assume this is your data line
[PHP]{echo "<tr><td>"; echo $comp_rec['complaint']; echo [/PHP]
Please check my syntax because this is of the top of my head.
Please note that I only use single quotes, because PHP parses them much faster than double.
And I have inserted extra spaces for clarity only. Remove them.
[PHP]{echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';[/PHP]
Jul 6 '07 #6
prosad
27
PHP can handle this very well. You need to wrap your data display line in a hyperlink, with the link returning the same page and your data being passed as a URL variable.
I assume this is your data line
[PHP]{echo "<tr><td>"; echo $comp_rec['complaint']; echo [/PHP]
Please check my syntax because this is of the top of my head.
Please note that I only use single quotes, because PHP parses them much faster than double.
And I have inserted extra spaces for clarity only. Remove them.
[PHP]{echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';[/PHP]
ur script:
[PHP]{echo '<tr><td><a href="'.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' "'.$comp_rec['complaint'].'</a>';}[/PHP]

thanks for the effort. though does not work cos i submit to a script that validates then inserts into a MySQL DB. as soon as database insert is carried out the data row is generated with the new data item just inserted into DB. using your script it does not return any data to my "complaint" table for display on page even though it has succesfully inserted data item in MySQL DB. my Database submit script is given below and as u can see it returns thro header to my submit page so user stays on same page. after submitting thro form - given above (form name: "complaint_settings") - Now the submit page (csu_settings.php) will have a single difference. HTML table displayed below form includes newly submited item fetched from database as new row. THE QUESTION??? HOW DO I SELECT THESE ITEMS ANY OF MY TABLE ROWS SUCH THAT ON CLICKING ANY CELL DATA IT RETURNS INTO MY FORM (INPUT TYPE = TEXT) THAT IS DISPLAYED ABOVE THE TABLE FOR OTHER ACTION SUCH AS UPDATE, DELETE . DIFFERENT BUTTONS ON FORM DETERMINES APPROPRIATE SCRIPT TO SEND DATA ITEM FOR PROCESSING UPDATE, DELETE ETC.

[PHP]//inserting record in complaint table
mysql_select_db("CSU", $con);
$sql="INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
if (!mysql_query($sql,$con)) {die('Error: ' . mysql_error()); }

mysql_close($con)

;};}

header('location:http://localhost/CSU_Project/csu_settings.php'); [/PHP]

[PHP]//initializing extraction of complaint parameters from mysql
$complaintTable = mysql_query("SELECT * FROM complaint") or die (mysql_error());

//store complaint records into array $complaint_row &&
//create table of mysql complaint array that is displayed under submit form on csu_settings.php page.

echo "<table>";
echo "<td><div>";"</div></td>";
echo "<table id = \"table_c\" border = \"4\" bordercolor = 00008B>";
echo "<tr><th>Complaint</th>";
while ($comp_rec= mysql_fetch_array($complaintTable))[/PHP]
Jul 6 '07 #7
code green
1,726 Expert 1GB
I suggesting passing the variables as URL variables
Expand|Select|Wrap|Line Numbers
  1. echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';
The following does not pass anything anywhere
Expand|Select|Wrap|Line Numbers
  1. (header 'location:http://localhost/CSU_Project/csu_settings.php');
using your script it does not return any data to my "complaint" table
You must read the URL variables using GET and not POST
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
Jul 11 '07 #8
prosad
27
I suggesting passing the variables as URL variables
Expand|Select|Wrap|Line Numbers
  1. echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';
The following does not pass anything anywhere
Expand|Select|Wrap|Line Numbers
  1. (header 'location:http://localhost/CSU_Project/csu_settings.php');
You must read the URL variables using GET and not POST
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
thanks code green,
solved my problem using Javascript onclick, can click on any cell in a dynamic table and it will pass the innerText object value to my form text field. parts of code given below:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function select_cell(obj)
  3. {
  4.     document.getElementById('trial').value = obj.innerText; 
  5.     document.getElementById('complaint').value = obj.innerText;
  6.     document.getElementById('selected_form');
  7. }
[PHP]echo "<tr><th>Complaint</th>";
while ($comp_rec= mysql_fetch_array($complaintTable))
{echo "<tr><td id = 'complaint' onclick='select_cell(this);'>"; echo $comp_rec['complaint'];
echo "</td>";[/PHP]

[HTML]<table id = "MAINFORM">
<form id = "selected_form" name = "complaint_settings" method = "POST" action = "php_complaint_mysql.php">
<td>
<table border = "3" bordercolor = "gold" bgcolor = "00008B">
<tr><td>
<p style = "color:gold">Complaint: <br>
<input type = "text" size = "50" maxlength = "50" name = "complaint">
</td></tr><br>
<tr><td>
<input type = "submit" name = "submit_c" value = "submit"> <br>
<input type = "submit" name = "submit_c" value = "update"> <br>
<input type = "submit" name = "submit_c" value = "delete" onClick = "return confirm('Are you sure you want to delete record?')">[/HTML]

my problem now is how to pass along my primary key which is the unique id of each $comp_rec to my form as a hidden variable so i can pass $_POST['complaint'] to php_complaint_mysql.php for update or delete. As it is now the table i use onclick only shows a single column(complaint) and even if i add another cell (id) the two cells are not carried and placed in separate fields on the same form. Any idea on populating form fields by clicking a row record with each attribute being passed to appropriate field? Attribute ID not presently on table. wish to keep hidden. But need to pass ID so i can use to update mysql. let me go wander around javascript forum.
thanks.
Jul 12 '07 #9

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

Similar topics

4
by: webhigh | last post by:
I¹m not sure if this a PHP question or an MySQL question but here it goes. I have a repeat region of a table called userid What I¹m trying to accomplish is being able to edit the record and...
4
by: Stephen | last post by:
Hello People, Using MS Access 2003 VBA I get the error 3020 Update or CancelUpdate without AddNew or Edit when I run through the following code. Can anyone help suggest anything to try? Thanks....
1
by: Qnavry | last post by:
I am creating one page to edit, delete, and add data to a database. The Add code works but the Edit and Delete do not. What am I doing wrong? If you need more code let me know. Thanks Code:...
5
by: tony010409020622 | last post by:
I just spent 4 months taking a dotnet class where i learned very little. One of the things I did not learn is this: What are the dotnet equivilents of commands such as: Adodc1.Recordset.AddNew...
9
by: Dejan | last post by:
Hy, Sorry for my terreble english I have this simple code for deleting rows in mysql table... Everything works fine with it. So, what do i wanna do...: my sql table looks something like...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
1
by: micross | last post by:
hi how to add the border and edit ,delete button ,plz helme . <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
0
by: bplthebest | last post by:
Hi I have have functionality like Selecting multiple rows (without using check boxes) in Grid view by Ctrl+click (IE) and shift+click(safari and Firefox). And edit the selected rows in a panel...
0
by: kumardharanik | last post by:
Hi Friends, The below code is a sample code for insert, update and delete using datagrid but i need to convert the entire code for datagridview.. Plsss help me.. using System; using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.