Duplicate MySQL Error - Code may be executing twice!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Duplicate MySQL Error - Code may be executing twice!

Post by Sephirangel »

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:

Code: Select all

<?php
    //start session
    session_start();
    //connect to mysql server and database
    require_once('PHP/stock_functions.php');
    require_once('PHP/database_connect.php');
    $con = connect($host,$mysqlUsername,$mysqlPass, $dbName);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Knitterbabes Intranet Stock System: Edit Stock</title>
        <link rel="stylesheet" href="CSS/site.css">
    </head>
    
<body>
    <div id="content">
    <div id="logo"></div>
    <div id="nav">
        <a href='main.php'>Homepage</a> |
        <a href='addNewStock.php'>Add New Stock To Database</a> | 
        <a href='editStock.php'>Edit Stock</a> |
        <a href='addNewSale.php'>Add New Sale</a> |
        <a href='search.php'>Advanced Search</a> |
        <a href='index.php'>Log Out</a><br /><br />
        
    </div>
        
        <div id="text">
        <font size=5><ins>New Stock Processed</ins></font><br /><br />
    
    <?php
        
        $insert = insertNewStock($_SESSION);
                
        if($insert){
            echo "<font size=3>The following item has been saved successfully:</font>
                <div id='checkStock'>
                <table border=1>
                <tr><td align=right><b>Item Code: </b></td><td align=right>".$_SESSION['itemCode']."</tr>
                <tr><td align=right><b>Description: </b></td><td align=right>".$_SESSION['Description']."</tr>
                <tr><td align=right><b>Product: </b></td><td align=right>".$_SESSION['Product']."</tr>
                <tr><td align=right><b>Company: </b></td><td align=right>".$_SESSION['Company']."</tr>
                <tr><td align=right><b>Brand: </b></td><td align=right>".$_SESSION['Brand']."</tr>
                <tr><td align=right><b>Type Of Product: </b></td><td align=right>".$_SESSION['TypeOfProduct']."</tr>
                <tr><td align=right><b>Colour: </b></td><td align=right>".$_SESSION['Colour']."</tr>
                <tr><td align=right><b>Size: </b></td><td align=right>".$_SESSION['Size']."</tr>
                <tr><td align=right><b>Shape: </b></td><td align=right>".$_SESSION['Shape']."</tr>
                <tr><td align=right><b>Length: </b></td><td align=right>".$_SESSION['Length']."</tr>
                <tr><td align=right><b>Quantity: </b></td><td align=right>".$_SESSION['Quantity']."</tr>
                <tr><td colspan = '2' align = 'center'<a href='addNewStock.php'><button name='anotherStock'>Add Another</button></a>
                <a href='mainpage.php'><button name='mainpage'>Mainpage</button></a></td></tr></table>
                </div>";
        }else{
            echo "An error has occured and the record has not been entered!<br />
                If this happens more than once, please ring Josh!";
        }
        
            
    ?>
    
    <div id="footer" class="sale">
        <font size="-2">
            "Knitterbabes" is a Registered Trademark &reg; of Knitterbabes Ltd <br />
            "Knitterbabes Intranet Stock System" is Copyright &copy; to Knitterbabes Ltd <br />
            "Knitterbabes Intranet Stock System" built by Joshua Tedd and Designed by James Morgan <br />
            <i>Version: 2.0</i><br />
        </font>
    </div>
    </div>
    </div>
            
</body>
    
</html>
 
The code for the insert method is below:

Code: Select all

function insertNewStock($stockArray){
        $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']."')";
        
        if (!mysql_query($mysqlAddStock))
            echo mysql_error();
               
        return mysql_affected_rows() > 0;
    
    }
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!
Last edited by Benjamin on Thu May 21, 2009 8:43 am, edited 1 time in total.
Reason: Changed code type from text to php.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Duplicate MySQL Error - Code may be executing twice!

Post by Yossarian »

How does the stock get into the session in the first place?

If when its added to the session, it is also added to the database, why is this page doing another 'insert'?
Post Reply