Jump to content

Recommended Posts

This problem has had me tearing my hair out for a good few days!!

I have developed a PHP-based stock system.

 

On the page which calls the insertNewStock() method, i get a MySQL Duplicate entry error, even though the record has never been entered before.

 

After looking at the database, the stock record has been entered once so that would mean the code is executing twice. The method insertNewStock() is required_once from the file "stock_functions.php".

 

The code for the page is as follows:

 

   1. <?php
   2.     //start session
   3.     session_start();
   4.     //connect to mysql server and database
   5.     require_once('PHP/stock_functions.php');
   6.     require_once('PHP/database_connect.php');
   7.     $con = connect($host,$mysqlUsername,$mysqlPass, $dbName);
   8. ?>
   9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11.     <head>
  12.         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  13.         <title>Knitterbabes Intranet Stock System: Edit Stock</title>
  14.         <link rel="stylesheet" href="CSS/site.css">
  15.     </head>
  16.    
  17. <body>
  18.     <div id="content">
  19.     <div id="logo"></div>
  20.     <div id="nav">
  21.         <a href='main.php'>Homepage</a> |
  22.         <a href='addNewStock.php'>Add New Stock To Database</a> |
  23.         <a href='editStock.php'>Edit Stock</a> |
  24.         <a href='addNewSale.php'>Add New Sale</a> |
  25.         <a href='search.php'>Advanced Search</a> |
  26.         <a href='index.php'>Log Out</a><br /><br />
  27.        
  28.     </div>
  29.        
  30.         <div id="text">
  31.         <font size=5><ins>New Stock Processed</ins></font><br /><br />
  32.    
  33.     <?php
  34.        
  35.         $insert = insertNewStock($_SESSION);
  36.                
  37.         if($insert){
  38.             echo "<font size=3>The following item has been saved successfully:</font>
  39.                 <div id='checkStock'>
  40.                 <table border=1>
  41.                 <tr><td align=right><b>Item Code: </b></td><td align=right>".$_SESSION['itemCode']."</tr>
  42.                 <tr><td align=right><b>Description: </b></td><td align=right>".$_SESSION['Description']."</tr>
  43.                 <tr><td align=right><b>Product: </b></td><td align=right>".$_SESSION['Product']."</tr>
  44.                 <tr><td align=right><b>Company: </b></td><td align=right>".$_SESSION['Company']."</tr>
  45.                 <tr><td align=right><b>Brand: </b></td><td align=right>".$_SESSION['Brand']."</tr>
  46.                 <tr><td align=right><b>Type Of Product: </b></td><td align=right>".$_SESSION['TypeOfProduct']."</tr>
  47.                 <tr><td align=right><b>Colour: </b></td><td align=right>".$_SESSION['Colour']."</tr>
  48.                 <tr><td align=right><b>Size: </b></td><td align=right>".$_SESSION['Size']."</tr>
  49.                 <tr><td align=right><b>Shape: </b></td><td align=right>".$_SESSION['Shape']."</tr>
  50.                 <tr><td align=right><b>Length: </b></td><td align=right>".$_SESSION['Length']."</tr>
  51.                 <tr><td align=right><b>Quantity: </b></td><td align=right>".$_SESSION['Quantity']."</tr>
  52.                 <tr><td colspan = '2' align = 'center'<a href='addNewStock.php'><button name='anotherStock'>Add Another</button></a>
  53.                 <a href='mainpage.php'><button name='mainpage'>Mainpage</button></a></td></tr></table>
  54.                 </div>";
  55.         }else{
  56.             echo "An error has occured and the record has not been entered!<br />
  57.                 If this happens more than once, please ring Josh!";
  58.         }
  59.        
  60.            
  61.     ?>
  62.    
  63.     <div id="footer" class="sale">
  64.         <font size="-2">
  65.             "Knitterbabes" is a Registered Trademark ® of Knitterbabes Ltd <br />
  66.             "Knitterbabes Intranet Stock System" is Copyright © to Knitterbabes Ltd <br />
  67.             "Knitterbabes Intranet Stock System" built by Joshua Tedd and Designed by James Morgan <br />
  68.             <i>Version: 2.0</i><br />
  69.         </font>
  70.     </div>
  71.     </div>
  72.     </div>
  73.            
  74. </body>
  75.    
  76. </html>
  77.  

 

The code for the insert method is below:

 

   1. function insertNewStock($stockArray){
   2.         $mysqlAddStock = "INSERT INTO stock (ItemCode, Product, Company, Brand, TypeOfProduct, Colour, Size, Shape, Length, Description, Quantity) values ('".$stockArray['itemCode']."','" .$stockArray['Product']."','" .$stockArray['Company']."','" .$stockArray['Brand']."','" .$stockArray['TypeOfProduct']."','" .$stockArray['Colour']."','" .$stockArray['Size']."','" .$stockArray['Shape']."','" .$stockArray['Length']."','" .$stockArray['Description']."','" .$stockArray['Quantity']."')";
   3.        
   4.         if (!mysql_query($mysqlAddStock))
   5.             echo mysql_error();
   6.                
   7.         return mysql_affected_rows() > 0;
   8.    
   9.     }

 

I am hosting it on the latest version of WAMP server along with the latest versions of Apache, PHP and MySQL, and viewed using firefox.

 

Does anyone have any ideas? I really am at my wits end here!

The page is requested from a hyperlink on the previous page (an <a href> tag, ive used $_SERVER to verify its comoing from that page).

 

I did create a session variable (set to 0) on the page beforeand incremented it at the top of the page, as the last line of the first block of PHP code. When echoed, it always displays '2'.

 

This is strange as it should only load once.

Different browsers will request pages twice for a bunch of different reasons (debugging add ons, character encoding, javascript, favicons, url rewriting...) You need to use a session variable to prevent duplicate page requests - http://www.phpfreaks.com/forums/index.php/topic,252692.msg1186827.html#msg1186827

 

Edit: You should also be validating that your $_SESSION variables being put into the query are set before executing the query, because anytime a search engine or any other type of bot script requests your page it WILL execute that INSERT query with empty values.

The stock system is run locally on my clients machine so form validation isnt a problem yet, although I will be writing some validations in the near future.

 

Thanks for that, ive been using a session variable but set to 0 on the previous page, and incremented on the page in question, and wrapping the form execution code in an if statement. Of course though if the page only loads once, this will backfire!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.