Editing my data


#1

So i’m not a very good programmer, but I can get some things to work :slight_smile:
I have a WordPress site with custom PHP. The page for a fishing competition and is used for storing the weights for the fish catched for each team.
But now I ran into a problem. I have a page that reads some data from my database (fish spicies) and presents them with an inputfield for each spicies represented in the database.
Like this:

$arter = $wpdb->get_results( "SELECT id as artid, artnavn, mindstemaal, sats FROM arter order by artnavn ASC");
					if (!empty ($arter)) {
					// work with results.
					?>
<form action="" method="post" id="form" enctype="multipart/form-data">
					<table style="border:none;background-color:#e0e0eb;width:100%;">
					<tr>
					<?php
					foreach ($arter as $art){
					?>
						 <td style="border:none;width:25%">
						 <?php echo $art->artnavn ?>
						 </td>
						 <td style="border:none;">
						 <input type="text" size="10" name="indvej[<?php echo $art->artid ?>]"> gram
						 </td>
						 <td style="border:none;"><font size="2">Mindstemål <?php echo $art->mindstemaal ?> cm</font></td>
						 <td style="border:none;"><font size="2">Sats <?php echo $art->sats ?></font></td>
						 </tr>
						 <tr>

When you input a weight ind the “indvej” field for some of the spicies and submit it is send to a “APPROVE” site.

						 If($_POST['Submit'])
					{
						$_SESSION['indvej'] = $_POST["indvej"];
						$_SESSION['baadnr'] = $baadnr;
						$_SESSION['id'] 	= $id;
						$_SESSION['dag'] 	= $dag;
						?>
						<script type="text/javascript">
						window.location = "http://localhost/WORDPRESS/?page_id=2";
						</script>

On the APPROVE site the spicies with weight submitted is shown

$indvej = $_SESSION['indvej'];
						$baadnr = $_SESSION['baadnr'];
						$id 	= $_SESSION['id'];
						$dag 	= $_SESSION['dag'];
						
foreach($indvej as $x => $x_value)
	{
if ($x_value){
	$artnavn = $wpdb->get_results( "SELECT ArtNavn FROM arter where id=$x");
	?>

 echo $artnavn[0]->ArtNavn ?>
 echo $x_value ?> gram</b>

Now I can APPROVE and the data is stored in the database, BUT my problem is if I made an error in typing in a weight or if I have to add a spicie more. Then I would like to go back to first page, but with my data for the already typed fish. So when I go back the wieght for the spicies follows with me back. I hope you understand.
I know I have my data in the SESSION variable but how do I get the data out and matchs with the building of the inputfields on page one?


#2

Is the problem that the data DOES follow you back or
is the problem that the data does NOT follow you back?


#3

So the problem is that I might have the data with me back in the $SESSION[‘indvej’] in an array. But back on the page it reads the spicies fields from the database again and I have to fill the correct weight from the session array to the correct spicie… Is that clear?
eg.
Reading from db:
Flounder
Cod
Salmon

I put in
Flounder 10
Cod
Salmon 5
I press submit and it shows me:
Flounder 10
Salmon 5
I remembers the Salmon was 7 and not 5 so I du not prees submit, but back/edit button.
Comes back to page 1 and it again reads from database and shows:
Flounder
Cod
Salmon
I have the data from before in SESSION variables and I wan’t it to put in the data in put in before so I can edit it instead of starting all over. Like this
Flounder 10
Cod
Salmon 5


#4

So, I see two options. The latter would be what I would do, but it would require some changes.

  1. On the form, you check to see if values are already populated before you query the database. That prevents an unneccessary query and shows what is in those fields. Then when you edit it, you are just saving what is there anyway.

  2. Don’t use the sessions at all. Add them to the database with a “pending” or “unapproved” flag.

I’ll expand on that a bit. You add another table of status’s. In the table where you store the values, you add a status column with a foreign key constraint to the status table.

The records waiting for approval are based on that status; as well as the ones that show to everyone else.


#5

When you have made a mistake and go back to the first page, the form fields have the old value from the database instead of the data you recently entered which was saved in $_SESSION.

You want to check if a key and value in the $_SESSION exists and show that value in the form field, but if it does not exist, show the database value.

Most frameworks have a feature for this. I don’t remember what it’s called. But for plain php I have a function called ArrayCompare that I use for this.

include 'functions/ArrayCompare.php';
$values = new ArrayCompare($_SESSION['this_form_values'], $database_values);
include 'form.php'; 

#6

I don’t know if “we are on the same page” :smiley:
I have 1 table with fish.
FISH
[Cod]
[Flounder]
[Salmon]

Then I have a table with Teams
[Team1]
[Team2]

Then I have a table with results
RESULT
[Team]
[FISH]
[WEIGHT]

So when team1 comes in we go to page 1.
Page 1 list all FISH from FISH table. There are NO weight data in that database.
Team 1 has caught 1 cod on 5kg and 1 salmon on 10kg. So I write 5 in input field for cod and I write 10 in input for salmon. Go to page 2 and now the data with FISH and WEIGHT is in the SESSION variable. Now I see that the SALMON is not 10kg but 7kg and that they also caught a HERRING on 1 kg, so I want to go back to page 1 and now page1 again reads all the fishs from FISH table again. Now I want the 5 to be in the COD input field and the 10 in the SALMON field so I can edit 10 to 7 for SALMON and I can write 1 in HEERING and go to page 2 again.


#7

Hmm…

  1. Can you make an example? I don’t know how to do that.
  2. Then when I go back to page 1 I should read from that table and present the data from unapproved?

#8

So what you want to do is, populate those fields with what is currently in the $_SESSION values. You then make changes and the values are replaced entirely with what the new values are.

$replace['cod'] = isset($_SESSION['fish']['cod']) ? $_SESSION['fish']['cod'] : '';

And then a string replace on the placeholder.

<input type="text" name="cod" value="{cod_value}">

#9

Yes thats right. But my data is in the $_SESSION[‘indvej’] variable. In an array. Is that the way to get the data out from the array?


#10

I would store the entire thing in an array.

It would breakdown to something like,

$_SESSION['indvej'] = [
    "cod" => $_POST['cod'],
    "bass" => $_POST['bass'],
    "salmon" => $_POST['salmon'],
];

Then you iterate through it.