PHP Mystery Error No.2 !!

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

PHP Mystery Error No.2 !!

Post by Sephirangel »

Hey again,
Right, for some reason, my INSERT queries are being weird and are trying to enter the same record twice, even though i only call the function once! It enters the record, then gives me an error message saying that it cant insert the record because it is already there! (Duplicate record). The INSERT function i use is below, which basically returns a list of what has been inserted on successful completion, and returns an error message if failed:

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)){
            $response = "<font size=3>The following item has been saved successfully:</font>";
            $response = $response + "<table border=1>";
            $response = $response + "<tr><td align=right><b>Item Code: </b></td><td align=right>".$stockArray['itemCode']."</tr>";
            $response = $response + "<tr><td align=right><b>Description: </b></td><td align=right>".$stockArray['Description']."</tr>";
            $response = $response + "<tr><td align=right><b>Product: </b></td><td align=right>".$stockArray['Product']."</tr>";
            $response = $response + "<tr><td align=right><b>Company: </b></td><td align=right>".$stockArray['Company']."</tr>";
            $response = $response + "<tr><td align=right><b>Brand: </b></td><td align=right>".$stockArray['Brand']."</tr>";
            $response = $response + "<tr><td align=right><b>Type Of Product: </b></td><td align=right>".$stockArray['TypeOfProduct']."</tr>";
            $response = $response + "<tr><td align=right><b>Colour: </b></td><td align=right>".$stockArray['Colour']."</tr>";
            $response = $response + "<tr><td align=right><b>Size: </b></td><td align=right>".$stockArray['Size']."</tr>";
            $response = $response + "<tr><td align=right><b>Shape: </b></td><td align=right>".$stockArray['Shape']."</tr>";
            $response = $response + "<tr><td align=right><b>Length: </b></td><td align=right>".$stockArray['Length']."</tr>";
            $response = $response + "<tr><td align=right><b>Quantity: </b></td><td align=right>".$stockArray['Quantity']."</tr></table><br>";
            $response = $response + "<a href='addNewStock.php'><button name='anotherStock'>Add Another</button></a>";
            $response = $response + "<a href='mainpage.php'><button name='mainpage'>Mainpage</button></a>";
            return $response;
        }else{
            $response = "<p>Cannot insert new stock due to: ".mysql_error();
            return $response;
        }
        
    }
And here is where i call the function:

Code: Select all

$insert = insertNewStock($_SESSION);
    echo $insert;
Connections to the database have already been made, and I use SESSION data in order to insert the record. I do this because I create a list of what the user has entered on the page before in order for them to check if they have entered everything correctly. I know there is probably a much better way of doing it but right now my concern is the problem at hand.

Thanks in advance, any and all help will be much appreciated!
Sephirangel
Forum Commoner
Posts: 45
Joined: Tue Jul 15, 2008 1:37 pm

Re: PHP Mystery Error No.2 !!

Post by Sephirangel »

Is this anything to do with my Database table? Heres an SQL dump of the table:

Code: Select all

Table structure for table `stock`
--
 
CREATE TABLE `stock` (
  `ItemCode` varchar(15) NOT NULL,
  `Product` varchar(25) NOT NULL,
  `Company` varchar(30) NOT NULL,
  `Brand` varchar(30) NOT NULL,
  `TypeOfProduct` varchar(30) NOT NULL,
  `Colour` varchar(30) NOT NULL,
  `Size` varchar(6) NOT NULL,
  `Shape` varchar(20) NOT NULL,
  `Length` varchar(6) NOT NULL,
  `Description` varchar(60) NOT NULL,
  `Quantity` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`ItemCode`),
  UNIQUE KEY `ItemCode` (`Description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
Hope that helps!
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: PHP Mystery Error No.2 !!

Post by manixrock »

you could simply use the IGNORE statement to avoid duplication errors (if the index is already present it doesn't do anything):

Code: Select all

INSERT IGNORE INTO ...
Post Reply