SQL Query seems to be executing twice [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Insert query on page load, inserts twice?
I'm hoping someone can help with a problem that's driving me up the wall. I've searched but found nothing directly answering this.
I have a problem on a website where an SQL INSERT query seems to run twice but only on some occasions.
It's a simple shopping site. On my product page there is an 'Add To Cart' button. On pressing this a user is taken to the Cart page, and various variable are passed across. An INSERT query is run when the page loads and the results are displayed in the shopping cart.
This all works fine most of the time, but on some occasions, the product is added to the database twice.
The problem is not related to any specific product. Since it works OK the majority of the time, I'm pretty confident the code is OK. The only thing I can say is that I have not as yet seen the problem outside of Firefox. Some people have suggested turning of the 'Disable Cache' via Toolbar, which I have, but still getting problems.
Very grateful for any suggestions.

What happens when you reload the cart page? Does the POST operation happen again - it could be that if there's a hang while the page is loading (which can happen with Firefox - particularly on Macs - something to do with its caching system) the user might be hitting reload and therefore resending the page data and running in INSERT query again.
IF this is the case...
... the best idea is probably to add in a step that deals with the database that outputs nothing (no output means no caching). So your "add to basket" form posts the data to a database handling script which performs the operations and then does a simple header("location:...") to pass the user off to the "display cart" page.
That way the script that performs the database operations displays nothing and is never cached - and the user can hit reload on the "display cart" page as often as they like and it'll just reload their existing cart.

Firstly, though it's not going to sound very helpful, the problem will be with your code, not with Firefox, PHP, or MySQL. I've been through exactly what you're going through, and been sure it must be a bug, but something this obvious would have been fixed by now.
To help you narrow down where the bug is occurring, it would be worth logging various points in your code, so you can see how it flows through. It may help to log the SQL queries as well, so you can see if they are being called in quick succession, or whether it is a loop of some sort where something else occurs before the second call.
As the insert is done in PHP, this is all carried out server-side, so the chances of it being browser-specific are pretty slim.
There's a lot of help on this site, but to give you any more concrete advice you'll need to post code, and a bit more detail about the flow of the application.
Good luck!

Related

Refreshing an entire html/php page smoothly

Case: A php page with SQL needs to be refreshed every 30 seconds, but due to hardware limitations, there is a 0.5-2 second blank screen. Values do need to be retrieved from the database on every refresh, but the idea is to use a kind of seamless transitional effect, but where the transition is from one page to itself. 15 years ago or so, I experimented with what is described here: https://www.hongkiat.com/blog/meta-tag-hidden-features/ and I need something similar, though I am not even entirely sure that it would work. The page is very light and does not contain any images, so the load time is not the issue.
So, in short: How do I make it "impossible" to see that the page is refreshed, though values might have changed (or at least without the page going momentarily blank)?
I have tried numerous approaches with js and ajax functions to take care of the reloading instead of <meta http-equiv="refresh">, but none of them seem to be able to do what I want.
As suggested by #ivar and $usman-afzal, the issue was resolved using Ajax. Having no experience with Ajax/jQuery whatsoever, it took a while to get all the different values right and put them in their correct positions, but I succeeded, so thanks for the tip.

Caching and session variables (PHP, MySQL...)

Apologies in advance for the lengthy post, just trying to explain the situation clearly.
I've created a PHP-driven website for searching a big (millions of records) MySQL database of people. On the search page you have your usual form for search criteria. Due to the way the people often use the site, the search criteria are saved into session variables so that if the search page is reloaded the previous criteria remain in the form fields (there's a button to manually reset the criteria, of course). This in itself works fine.
I also have two language selection links that store the language selection in a session variable (making the page header load an appropriate localization file), and as above, this in itself also works fine.
What's problematic is that when a user gets the search result, a list of people, and wants to open up detailed info on a person (thus going from search.php to info.php) and then wants to go back to the people listing via the back button, it takes too long to reload the previous page as the page re-sends the MySQL query etc, instead of going back to a cached page. It can take even 5 seconds or more sometimes as the queries produce up to 5000 results - but even say, 200-500 results takes long to reload because the database itself is big and not the fastest in the world. And limiting the number of results isn't really practical.
The obvious solution at first glance would SEEM to be enabling the browser cache. Which is exactly what I did via PHP header and pragma controls. And all seemed well, as going back to the list was basically instantaneous. However, I realized that enabling the cache means the updated session variables don't work. New search criteria doesn't properly replace the old ones when reloading the search page after having been to a different page, and even though you select another language, pages open up in the language you previously were using, because that's the way they were cached! You can force the language to update via F5, but that doesn't seem to help the search criteria much. But even if it did, F5-spam isn't really an answer, it needs to work automatically.
So long story short, how do I make the search result list open quickly without making session variables useless? Or will I simply have to make do with sluggish page loads when using back button, thus annoying users? I really don't want to open the info.php in a new page, either.
Have you considered caching the database results on the file system? I have found the Zend Framework caching class to be very easy to use. You can use any information you want, to differentiate cached results from other cached results. So the caching can be as fine grained as required.
http://framework.zend.com/manual/1.12/en/zend.cache.introduction.html
You don't need to use the whole of Zend Framework to use the class. You can use it on its own.

Creating a Database that can be updated live

This is very hard to explain but I'm going to try.
We run a motor shop that has a QC program. The program was coded in access97 and it's time for an upgrade, we have elected to try a PHP/MySQL approach to do this.
Right now the access software has several pages to the form and each box sends to the database live so when you type something in you don't have to hit a save button or next or anything and when you come back it's there.
Also the forms are driven by an auto-incremented job number that you can punch into a field at the top of the page and it query's the server and displays all the data in the form boxes so you can edit it.
I don't know how to even start this project. I got a working form and an insert.php page but I don't know how to go about the rest.
If I could get a pointer in the right direction that would be appreciated. Thanks!
You just want it to save automatically? You'll have to look into JavaScript, and more specifically AJAX. I recommend using the jQuery library. Basically, you're going to want to make an AJAX call every time your form field is modified, and that AJAX call will simply update one field in particular.
I understand you are likely very new to website design, so this might be complicated for you.
I would read through this W3Schools tutorial. After reading through that, I'd pay close attention to this tutorial.
Again, this is difficult for beginners. I'd recommend you continue to work at your script, and ask more specific questions here on StackOverflow as time goes on. Good luck!
I have created a simple example here:
HTML/JS:
shaquin.tk/experiments/ajax.html,
PHP: shaquin.tk/experiments/qc.txt.
Have a look at the source to see how it works (I also have some comments in my code), feel free to copy it and modify for your own needs.
To sum up how it works:
When text is typed into a text box, a list of changed elements is updated.
Every updateInterval milliseconds (default 1000), the list is checked. (This helps reduce traffic and lag.) If anything has changed, the PHP file is called to update the database, and the list is cleared.
If an element loses focus and it has changed (e.g. copy/paste), the PHP file is called.
The PHP file sanitizes the query, checks for a valid job number, and updates the database.
References:
AJAX XMLHttpRequest
setInterval
addEventListener
encodeURI
mysqli_connect
mysqli_query
mysqli_real_escape_string
You'll need to submit the data as an ajax request. That way the data can be sent and returned without the page needing to be reloaded to update the information.

Why is my PHP Script not working when JavaScript is switched on? (but only in certain browsers)

I'm new to PHP (and web development in general) and have come across something that to me seems really, really bizarre!
Background
I am currently designing and developing an online enrolment form for my employer, a training company. The form consists of 3 pages - pages 1 and 2 are for data input whilst page 3 is a summary page with only one input, a box to check that the customer agrees to our T&Cs. Page 3 also includes a box where calculations are made as to the price of the selected training course based on choices made earlier in the form.
The form displayed on page 2, and some of the text on page 3, changes depending on a choice made on page one - that is, whether they are paying for the course themselves or their employer is funding it. If they choose employer, they get one form, if they choose self funding they get a different one. However, both of these forms are contained within a single .php file, using session variables to decide which one should be displayed.
I am using sessions to transfer the data between the pages. Each page has been made sticky using session variables. Real-time validation is carried out using javascript on each individual page, then a final PHP validation check is run on the whole thing when the customer tries to submit the final page. Javascript is also used on page one to calculate the price of the course in real time so the customer can see how much he/she will be paying before proceeding further.
The Problem
I find this really strange:
In Firefox, everything works perfectly
In Internet Explorer, when JavaScript is turned off, everything is fine. But when JS is switched on, the PHP validation on the final page seems to go haywire, thus making it impossible to submit the form.
In Chrome, again when JS is switched off, everything works, but when switched on, I can't even get past the first page. Instead of loading the correct form for page 2, I just get a blank screen.
Can anyone help? This is my first major project and I've been working on it for weeks, coming up against all sorts of problems and tearing my hair out but managing to solve them. Now I'm completely stumped, when I'm almost within touching distance of completion!!
I can't post the code here as it's 3 separate documents and very complicated, but if you want to have a look at the form itself go to:
http://testing.xenongroupadmin.com/testing/enrolment.php
I hope someone can help! Thanks very much!
You are most likely getting errors on the page2.php,
See error reporting to display and debug those:
http://ca.php.net/manual/en/function.error-reporting.php
When I have validation errors in non-Firefox browsers, it's usually because I have some error-reporting code in my JavaScript. Just something like
console.log(testValue);
can bring IE to its knees. If you have any code like that, remove it before testing in other browsers.
Also make sure that all variable declarations are done with "var", and that you're using semicolons at the end of each line (the spec should really enforce this, but I guess it doesn't, and browsers handle it differently).
I've only just seen all these answers, so apologies for not responding sooner.
I managed to fix the problem - turns out it was all to do with JS, no problems with my PHP at all. There were two issues. Firstly, a few syntax errors (which it took forever to find). Secondly, a lot of my HTML elements had id's and classes with identical names (e.g. id = "example" class = "example") which seemed to be throwing the javascript a bit.
Anyway, thanks for all the responses, and also apologies for not being very clear on some aspects - 'going haywire' was a pretty poor way of explaining the problem!!
Cheers

Is there a common name for this "session locking" method?

On one of my pages I have users queue up search terms to be to queried from a 3rd party API. As they're building the queue, my site is making the queries in the background (through ajax) so I can cache the responses, saving them time when they submit. I store a session variable $_SESSION['isloading'] as true during the time that the background queries are running, and its false when they're done.
When they submit, the results page waits for $_SESSION['isloading'] to be false before querying the cache for result. Meanwhile they're shown a progress wheel.
Is there a name for this technique of using a session to locally "lock" a user before proceeding to the next step in the code? I came up with this approach on my own and was wondering if it is a common (or good) solution to this problem, and is used elsewhere.
Putting it in $_SESSION will be a wasted effort. Been there, done that and it didn't work out.
You will be much better off if you provide your "search query string" in as a $_GET variable for your XHR ( marketing people call it - Ajax ).
Off the top of my head, this sounds a little similar to the way some older forum software performs forum searches in the background, and the visible page does a repeated refresh until the background search is complete.
I don't think there's a name for it; I'm also note entirely convinced that it's a great solution. As stevecomrie pointed out, you're going to run into difficulties with concurrency (unless the session variable's name is unique per search query).
I'd instead recommend an XmlHttpRequest (as teresko points out, it's not really called "AJAX", ugh!) and you can handle the "waiting" quite simply with Javascript.
I asked about this on IRC (Hat-Tip to ##php on freenode), and they suggested I just make the search form and search results one page. Then, when they're done entering their searches the view would change rather than submitting to the next page. This would remove the necessity of keeping track of an 'isloading' state. This seems like a better approach to me, are there any problems with it?

Categories