Why Are Database Entries Being Automatically Created When I Visit the "New" Page

New session entries are being created in database even when codeigniter session library is not loaded. Why?

For the 1st part of the problem:

As pointed out by @JamesLalor in the comments,

  1. You must either be autoloading the session

    OR

  2. You are using some external library which is, in turn, loading the session library.

For the 2nd part of the problem:

The below solution may not be the best, but it worked for me.

Multiple sessions creation problem occurs when:

  1. You have both REST Server and Client within the same CodeIgniter Application directory

    AND

  2. Session library is auto-loaded

To the Client, the user is the consumer. A session is created for the user, having the IP address of that user. A cookie is set on the user’s browser with which the session is validated and updated (or newly created) based on the validation.

To the REST Server, the Client is the consumer. Here also a session is created (if both condition 1 and 2 above is fulfilled), but this time, it is for the Client, and this session has the IP address of the server on which the Client resides (if condition 1 above is fulfilled, then it is the IP address of the same server on which your app resides). But this time a cookie could not be set, as the consumer is not a browser. Hence, the session validation fails and a new session is created each time the page loads.

Solution:

REST is stateless and every request should contain all the information required to fulfil the request. Therefore, using sessions (whose sole job is to maintain user’s state) on REST Server is considered a bad practice. It is the job of the Client to maintain the user’s session and pass the required information to the REST Server on each and every request.

Therefore, assuming that you would not be needing session within REST Server, the solution is to remove session from autoload[‘libraries’] list, within autoload.php file, and load the library within the Client constructor (or when you need it).

Sorry for grammatical errors and/or bad English. It is not my native language.

Table content keeps adding when refreshing PHP page

Why the sample values are added on each page visit

Your PHP code is executed every time you visit the page. It generates the page (writes parts of it dynamically, just copies everything outside <?php … ?>), and the page is the output of your script. Apache, or whatever server software you use, just gets an HTTP request, executes your script, takes its output and sends it to the client in an HTTP response.

If your INSERT query is executed unconditionally (as it is), it is also executed every time you visit the page. That's why the values are added over and over again.

How to stop adding the sample values

You may want to check if the cars table was created and execute the insert query only if it was:

# Creating the table with rows start
# CHANGED: Notice that IF NOT EXISTS is not used anymore!
# Fails if the table already exists, not touching its original contents.
$db_query = 'CREATE TABLE `cars` (' .
'`id` int(11) NOT NULL AUTO_INCREMENT,' .
'`make` varchar(50) NOT NULL,' .
'`model` varchar(50) NOT NULL,' .
'`year` varchar(4) NOT NULL,' .
'`color` varchar(50) NOT NULL,' .
'`engine` decimal(2,1) NOT NULL,' .
'PRIMARY KEY (`id`));';
$result = mysqli_query($conn, $db_query);
# Creating the table with rows end

# Putting information in each column start
$db_query = 'INSERT INTO `cars`' .
' (`id`, `make`, `model`, `year`, `color`, `engine`)' .
' VALUES' .
' (NULL, \'Citroen\', \'Saxo\', \'1997\', \'White\', 1.1),' .
' (NULL, \'Citroen\', \'C3\', \'2012\', \'Blue\', 1.4),' .
' (NULL, \'Volkswagen\', \'Golf\', \'2010\', \'Blue\', 1.8),' .
' (NULL, \'Ford\', \'Mondeo\', \'2009\', \'Black\', 1.6),' .
' (NULL, \'Renault\', \'Clio\', \'2010\', \'Silver\', 1.2);';
# CHANGED: Executing the query only if previous query (`CREATE TABLE …`) succeeded.
if ($result) {
$result = mysqli_query($conn, $db_query);
}
# Putting information at each end

Refresh after a POST problem

Another problem we met during debugging the issue is refresh after POST form submission. Browser automatically submits the original form contents again, which is standard behavior. It results in inserting the submitted values to the database again, now storing them in two or even more copies.

This can be prevented, though. See e.g. Stop browsers asking to resend form data on refresh. The accepted answer recommends the POST-redirect-GET pattern, which is what I would use in your case. You can get much more on that topic when googling post data resent stackoverflow.

stop inserting data in the database when refreshing the page

Header the user to a new page :

if (isset($_POST['submit'])) 
{
$user= $_POST['username'];
$email = $_POST['useremail'];
$pass= $_POST['password'];

mysql_query("INSERT INTO table (username, useremail, email) VALUES(`$username','$useremail','$email')");

}
//best outside the if statement so user isn't stuck on a white blank page.
header("location: landing_page.php");
exit;

By doing this the user who refreshes will be refreshing landing_page.php which means it won't do the insert twice.

best advice: do a check to see if user exists first if so don't insert!

DIV stops refreshing when new database entry is made

This could be caused by the load method throwing an exception. You should add some error handling to assist with debugging the issue.

Try using this to catch for any errors:

$('#incActive').load('active.php', function( response, status, xhr ) {
if ( status == "error" ) {
var msg = "Sorry but there was an error: ";
$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
}
else{
setTimeout(refreshActive, 5000);
}
);


Related Topics



Leave a reply



Submit