zimmo Posted April 9, 2010 Share Posted April 9, 2010 I have my code now working thanks to guys off here. Next step is for me to extend this, and not sure the best way to do this. This is the scenario: User fills in the first page of the form, if they miss any data that is required it asks them for it. Once done, they click the submit button. This then enters the information into the database. As the id field is auto_increment, it creates a new "id" number for the insert. The problem: The user hits the back button on the browser and then submits the data AGAIN This would then create another insert but with the next "id" number So we then have 2 records the same with different id numbers. I need to be able to check that they have entered only once and not allow them again? My problem is that it creates the id on the first entry, how can php check this? Code for the php insert to mysql (taken from file) if (!$error) { # setup SQL statement $SQL = " INSERT INTO dtable "; $SQL = $SQL . " (venue_name, address_1, address_2, address_3, town, county, postcode, email, telephone_1, telephone_2, fax) VALUES "; $SQL = $SQL . " ('$venue_name', '$address_1', '$address_2', '$address_3', '$town', '$county', '$postcode', '$email', '$telephone_1', '$telephone_2', '$fax') "; #execute SQL statement $result = mysql_db_query( dbname,"$SQL",$connection ); # check for error if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } else { $id = mysql_insert_id(); header("Location: http://www.domain.com/admin/next_contact.html?fishery_id=$id"); exit; } } } Link to comment https://forums.phpfreaks.com/topic/198150-checking-id-exists/ Share on other sites More sharing options...
zimmo Posted April 9, 2010 Author Share Posted April 9, 2010 Would I be better of using a php session? and then query the session on each page? what would be the best way to do that? Link to comment https://forums.phpfreaks.com/topic/198150-checking-id-exists/#findComment-1039693 Share on other sites More sharing options...
the182guy Posted April 9, 2010 Share Posted April 9, 2010 Would I be better of using a php session? and then query the session on each page? what would be the best way to do that? Could do, or just do a query to see if a row already exists with the same data. Link to comment https://forums.phpfreaks.com/topic/198150-checking-id-exists/#findComment-1039701 Share on other sites More sharing options...
zimmo Posted April 9, 2010 Author Share Posted April 9, 2010 Thanks. I thought about doing the query and checking the other data as well, but what happens if the user decides to change some of the data in the form when they hit the back button, it wont match so will insert as a new row? Link to comment https://forums.phpfreaks.com/topic/198150-checking-id-exists/#findComment-1039705 Share on other sites More sharing options...
the182guy Posted April 9, 2010 Share Posted April 9, 2010 Use the session then. You could also store the users IP Address with the row, and do a check to see if that IP has already submitted a row before. Be aware though that some ISP's change the users IP often. With the session method there's no guarantee either because it would only take a clear of the cookies to get a new session ID. Link to comment https://forums.phpfreaks.com/topic/198150-checking-id-exists/#findComment-1039710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.