Save user updated data from table

Okay I have searched for the last 3 days and this seems that is should be very easy.

I have a table that is generated from a mysql database. In the table I have some text boxes that fill in but can be updated. Once the user updates that text box I want them to be able to push a button, or a link that is also in the row that will then update the text boxes to the mysql database.

The table is a schedule/score table as below.

Game Date Game Time AwayTeam AwayScore

There are more columns, but that should give you the idea. Basically the admin person can enter scores or enter the AwayTeam if there is an empty spot in the schedule. The GameDate/Time never get updated on this screen.

Can someone please point me in the right direction. I know programing, but I am new to web programing.

Thanks,

whether your using the form method “GET” or “POST” you’ll need to pull the values from that array on submit and build a query to “UPDATE” the database,

example not tested or exactly following proper syntax:

$name = mysql_real_escape_string($_POST[‘name’]); **
$query = “UPDATE tablename WHERE insert_clause_here name = ‘$name’”;
mysql_query($query) or die(‘Unable to process request’);

** mysql_real_escape_string is a function to help protect the database, but its just a simple method for a better option I would suggest reading up on parameterized queries.

WOW! Deja-Vue !!! LOL! Wow, that took me back a few years!

I have an on-going NFL pool that used to use the web. (I recently made a much better version using VB.NET that uses an online DB and is more secure!) Anyway, I displayed my table of scores in the weekly display that was on a PHP page that pulled from the database and displayed “final” games as READONLY. So, users would see a little difference in “final” games with different colors. (Slightly different.) And, since my ADMIN page was passworded and secure, I would change my version of the table and it would be sent to a PHP update page that would post my updates. Then, everyone sees the change right away! (As soon as I post!) So, in your table, all of the grid is basically rows and cols. These create the cells of the grid. Each that are changeable should be a field. So, inside each of the grid you have a etc… Each has a unique name. When posted to your update page, the page searches thru the values in each field and updates it to the database. Simple enough… LOL…

So, that site of mine took months to debug due to all of the very neat displays I added. Such as weekly scores of players sorted highest picks to lowest, daily winner, weekly winner and overall stats sorted on total points for highest picks overall… Was a fun and interesting site… PM me and I will send you some of the pages…

I have been trying a lot of different things with Post/Get. But my issue is I have a number of rows, and I always get the data from the last row. I want the data from the row the user clicks update on. I have no problem getting the correct row when pulling from the database, the issue is when a user updates I have no idea how to pull the new user entered data for my update query, I keep getting the last row of data in the table or the original data from the database.

ernieAlex - I wish I could start over. I know VB.Net, just not the web side. When I started this site people recommended php over vb.net.

May we see the code you have thus far(that which is applicable)? Will make the communication easier. :slight_smile:

From the MaySchedule Page I have these two Posts.

$id = mysql_real_escape_string($_POST[’$id’]);
$aname = mysql_real_escape_string($_POST[’$aname’]);

This calls the update_ab.php

Then for now on the update_ab.php I am just trying to display what I sent to see if it is the correct data.

Team <?php echo $ateam; ?> ID <?php echo $id; ?>

My Update Query works fine, but I have the data from the last row.

$sql=“UPDATE $tbl_name SET ateam=’$ateam’, ascore=’$ascore’ WHERE id={$id}”;
$result=mysql_query($sql);

I know my issue is I am not telling what row to get the POST variables from, but I cannot figure out for the life of me how to get the correct row. When I do a select query with the hidden ID it gives me the data from the database not, the newly entered data on the screen.

I can get it to work fine if I only have one row of data displayed.

let me see if I understand this right… you have a table on your page that displays something like:

Game Date Game Time AwayTeam AwayScore / edit?

    x                  x                   x               x               Click to edit    /     o
    x                  x                   x               x               Click to edit    /     o
    x                  x                   x               x               Click to edit    /     o
    x                  x                   x               x               Click to edit    /     o
    x                  x                   x               x               Click to edit    /     o

Or possibly instead of a click to edit maybe a checkbox or radio button?

Or do you have just a bunch of text fields with the value filled which are editable?

Depending on how you have the page formatted, the way the form is processed will vary. If you have something like the above example when pulling items from the database to fill the row you would add the individual rows identifier to the link/button.

but either way each text field/form option will need its own unique ‘name’ ie:

<input type='text' name='row-4-awayTeam' size='20' value='<?php echo $row[4][awayTeam]; ?>'

So when processing any edit you would do something like
[php]$query = array(); // if accepting multiple row edits
foreach ($_POST as $k => $v) {
$getRow = explode("-", $k);
$row = $getRow[1];
$element = $getRow[2];
$query[] = upset set item=$element where id=$row
}[/php]
depending on what other elements on the form you will need to verify the POST element is from where you want it to be, then you create your query:

LOL, Sabmin, You read my mind… I was just going to tell him to do it that way!

Ryan, Sabmin explained a VERY useful trick-of-the-trade using row/col encoded with dashes and then
exploded back in the processing for to use in your code. I use this routine very often in passing all kinds
of data to pages. Very very useful… Study Sabmin’s code a bit, and you will get it…

Sorry, had to throw in my 2cents!

Well I now see why my other attempts did not work. I probably did not explain this part well. The table is just as Sabmin posted, but it is generated from a database and can have a different number of rows each month. I just load the first recordset into the table and then do a repeat recordset.

I have a way of doing it where the user clicks update and it goes to a new update page with the data. The user can then update from the screen (which I can name text boxes) and then when they click submit it updates the database and then goes back to the main screen. It works fine, but it is just extra steps when you are entering in 6 games scores at a time.

Thanks for the help so far, I really appreciate it.

you could cut that down a lot by doing everything on the same page… if something is editable just display it in a textbox rather than having to move to another page to edit, as far as the number of boxes differing you can just do something like:

[php]

$x = 6 //or whatever your dynamic number of rows is
for ($i = 1; $i <= $x; $i++) {
if (is admin) {
echo ("<input type=‘text’ name=‘row-" . $i . "-awayTeam’ size=‘20’ value=’" . $row[4][‘awayTeam’] . “’”);
} else {
echo $row[4][‘awayTeam’];
}
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service