Page 1 of 1

duplicate value on INSERT

Posted: Sat Jun 19, 2004 6:51 am
by mm00re
hey to all,

i am getting duplicate values on insert into a mySQL db.

error is
Duplicate entry '' for key 2
cant see y can anyone offer any ideas pls

only happening with two queries:
if(($tabID == 'Add') && ($action == 'Tape')) and if(($tabID == 'Add') && ($action == 'Client'))

url is: /process_form.php?tape_name=Tape+One&date_bought=19-06-2004

code to process this and oother forms is:

mySQl table structure is:

Code: Select all

CREATE TABLE `tape` (
  `tapeID` int(11) NOT NULL auto_increment,
  `tape_name` varchar(100) NOT NULL default '',
  `date_bought` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`tapeID`)
)

Code: Select all

<?php
include '../header.inc.php';

$referrer = $_GET['referrer'];

if(isset($_GET['action'])){
  $action = $_GET['action'];
  $tabID = $_GET['tabID'];
}
else{
  $action = 'Record';
  $tabID = 'Add';  
}


############# start processing of $_GET vars to add to database ####################
### each var matches the name in the database
###
### client database entries
###
// chop the client id from the clients drop box entry - in form of 'id - name'
$clientID       = substr($_GET['cid'], 0, 1);
$company_name   ? substr($_GET['cid'], 4) : $_GET['comp_name'];

// chop the tape id from get vars from drop box
$tapeID       = substr($_GET['tid'], 0, 1);
$tape_name = $_GET['tape_name'];

// chop the product id from get vars from drop box
$productID    = substr($_GET['pid'], 0, 1);

$address      = $_GET['add'];
$bus_phone  = $_GET['bphone'];
$bus_fax      = $_GET['bfax'];
$email          = $_GET['email'];
$website       = $_GET['web'];
$contact_name   = $_GET['contact'];
$contact_mobile  = $_GET['mobile'];
$key_number     = $_GET['key'];
$product_name   = $_GET['prod_name'];
$title = $_GET['title'];

// change date format from dd-mm-yyyy from get vars
// to yyyy-mm-dd for sql date type
$date = $_GET['date'];
$date = split('-', $date);
$date = $date[2].'-'.$date[1].'-'.$date[0];

$date_bought = $_GET['date_bought'];
$date_bought = split('-', $date_bought);
$date_bought = $date_bought[2].'-'.$date_bought[1].'-'.$date_bought[0];

$time_code = $_GET['time'];
$comments = $_GET['comments'];

###
### Determine which form we are dealing with and create query based on that form
###
$link = mysql_connect('localhost', 'user', '********');
$db = mysql_select_db('tape_lib');
##
##
if(($tabID == 'Add') && ($action == 'Record')){
    
    $query = "INSERT INTO product (key_number, clientID, tapeID, product_name, title, date, time_code, comments)
		          VALUES('".$key_number."', '".$clientID."', '".$tapeID."', '".$product_name."', 
                     '".$title."', '".$date."', '".$time_code."', '".$comments."')";
                     
}
else if(($tabID == 'Add') && ($action == 'Client')){
   
    $query = "INSERT INTO client (company_name, address, bus_phone, bus_fax, email, website, contact_name, contact_mobile)
		          VALUES('".$company_name."', '".$address."', '".$bus_phone."', '".$bus_fax."', 
                     '".$email."', '".$website."', '".$contact_name."', '".$contact_mobile."')";
                  
}

else if(($tabID == 'Add') && ($action == 'Tape')){
  
  $query = "INSERT INTO tape (tape_name, date_bought)
		        VALUES('".$tape_name."', '".$date_bought."')";

}
    // perform the query

    $error = mysql_query($query, $link);
    $err_str = mysql_error();


############### check for errors, if none include the details of entries just added ################
if(mysql_affected_rows($link) < 1){
  echo '<div align="center">Error in processing details: ' . $err_str;
  echo '<br><br>';
  echo '<a href="javascript:history.go(-1);">Click here to return.</a></div>';
}
else{
  include 'process_finished.tpl';
}

mysql_close($link);
?>

kind regards,

g00fy

Posted: Mon Jun 21, 2004 11:41 pm
by kettle_drum
My guess would be:

Code: Select all

$tapeID       = substr($_GET['tid'], 0, 1);
The tape idea is one char long and it needs to be unique in the database - so once you get to 10 the id isnt unique - $_GET['tid'] = 10, 11, 12 the id is always '1'.