-1

I have a webpage (submit_score.php) where you can submit your test score and it draws a chart of the point distribution. Once you have inputted the score you press the button that then takes you to the page that shows the point distribution (point_chart.php).

In the 'point_chart.php' page I have the SQL-queries that actually add the scores to the database table that the ChartJS then shows on the page.

The problem is, once you are on the 'point_chart.php' and refresh the page, it also runs the SQL-queries again and adds the points to the database table again.

I am aware of PRG-pattern, but I haven't found anything useful online.

Any ideas?

webdev18
  • 9
  • 2
  • just add a condition to check button value is set $_POST['submit'] . only then execute sql . so it wont run on normal refresh – zod Jun 24 '19 at 14:27
  • 1
    @zod — Rubbish. A normal refresh will resubmit the POST data. – Quentin Jun 24 '19 at 14:32
  • @Quentin What rocket science is in this question - submit_score.php have the sql to add and the point_chart should have a fetch sql . How point_chart will add on refresh if you check the submit if it is a redirect? – zod Jun 24 '19 at 14:47
  • @zod — The OP has explicitly said "The problem is, once you are on the 'point_chart.php' and refresh the page, it also runs the SQL-queries again and adds the points to the database table again." – Quentin Jun 24 '19 at 14:49
  • https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr – zod Jun 24 '19 at 15:05

2 Answers2

1

I am aware of PRG-pattern

That's the solution.

  1. The browser makes a POST request to the PHP program
  2. The program runs the query which adds the points to the database and responds to the browser with a redirect response
  3. The browser makes a GET request to a different PHP program
  4. The second program reads data from the database and generates the page with ChartJS embedded in it
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I think the Post, Redirect, Get pattern is a good solution.

  • submit_score.php will keep your form as is, you'll have it post to a new page you'll have to create
  • record_score.php will be the new page, and you'll put your insert queries in there, after the sql insertion, it will redirect to point_chart.php
  • point_chart.php you'll remove your insertion queries from here, (the post variables won't be there anymore to insert anyway) and you'll just show the chart.

once they are on point_chart.php they can refresh to their heart's content

RightClick
  • 1,090
  • 7
  • 12
  • How can I get the right chart to show? Currently, I'm using if/else statements like so: if ($subject == "Chemistry) { echo 'ChartJS_Chemistry.js; } – webdev18 Jun 24 '19 at 20:18
  • Ok, let's say that one of your fields is named subject, and it is sent as a post variable when the form is submitted. In record_score.php, you will be receiving a POST variable named subject, and using it in your insert query. After the insert queries are done, redirect to `"point_chart.php?subject=" . $_POST['subject']` to pass that variable along to the 3rd page as a GET variable. You'll have to adapt your point_chart.php page to look at the $_GET['subject'] variable and echo out that js file. BTW, it can be risky to use user input without validation/sanitizing, that's a whole other topic. – RightClick Jun 25 '19 at 12:36