Obtención de duplicados cuando inserto en la base de datos

Tengo un formulario que funciona bien, pero cuando inserto, duplica los valores que envié menos el código de trackingcode . Aquí hay un recorte de la base de datos después de la inserción:

Duplicar

Estoy tratando de averiguar de dónde viene, posicionar algunos elementos fuera del bucle, pero parece que no puedo arreglarlo, así que pensé en preguntar.

Aquí está mi código de inserción:

 $connect = mysqli_connect("localhost", "root", "", "pcrequest"); if(isset($_POST["reqname"])) { $length = 3; $req_date = $_POST["req_date"]; $reqname = $_POST["reqname"]; $date = $_POST["date"]; $empname = $_POST["empname"]; $position = $_POST["position"]; $account = $_POST["account"]; $platform = $_POST["platform"]; $processor = $_POST["processor"]; $ram = $_POST["ram"]; $monitor = $_POST["monitor"]; $phone = $_POST["phone"]; $phonetype = $_POST["phonetype"]; $headset = $_POST["headset"]; $req_table = $_POST["req_table"]; $req_chair = $_POST["req_chair"]; $approval = $_POST["approval"]; $status = $_POST["status"]; $query = ''; function generate ($value='',$inp=""){ return $value.str_pad($inp, 4, "0", STR_PAD_LEFT); } for($count = 0; $count<count($reqname); $count++) { $req_date_clean = mysqli_real_escape_string($connect, $req_date[$count]); $reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]); $date_clean = mysqli_real_escape_string($connect, $date[$count]); $empname_clean = mysqli_real_escape_string($connect, $empname[$count]); $position_clean = mysqli_real_escape_string($connect, $position[$count]); $account_clean = mysqli_real_escape_string($connect, $account[$count]); $platform_clean = mysqli_real_escape_string($connect, $platform[$count]); $processor_clean = mysqli_real_escape_string($connect, $processor[$count]); $ram_clean = mysqli_real_escape_string($connect, $ram[$count]); $monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]); $phone_clean = mysqli_real_escape_string($connect, $phone[$count]); $phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]); $headset_clean = mysqli_real_escape_string($connect, $headset[$count]); $req_table_clean = mysqli_real_escape_string($connect, $req_table[$count]); $req_chair_clean = mysqli_real_escape_string($connect, $req_chair[$count]); $approval_clean = mysqli_real_escape_string($connect, $approval[$count]); $status_clean = mysqli_real_escape_string($connect, $status[$count]); if($req_date_clean != '' && $reqname_clean != '' && $date_clean != '' && $empname_clean != '' && $position_clean != '' && $account_clean != '' && $platform_clean != '' && $processor_clean != '' && $ram_clean != '' && $monitor_clean != '' && $phone_clean != '' && $phonetype_clean != '' && $headset_clean != '' && $req_table_clean != '' && $req_chair_clean != '' && $approval_clean != '' && $status_clean != '') { $query = ' INSERT INTO request(trackingcode, req_date, reqname, date, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, req_table, req_chair, approval, status) VALUES("'.$trackingcode.'", "'.$req_date_clean.'", "'.$reqname_clean.'", "'.$date_clean.'", "'.$empname_clean.'", "'.$position_clean.'", "'.$account_clean.'", "'.$platform_clean.'", "'.$processor_clean.'", "'.$ram_clean.'", "'.$monitor_clean.'", "'.$phone_clean.'", "'.$phonetype_clean.'", "'.$headset_clean.'", "'.$req_table_clean.'", "'.$req_chair_clean.'", "'.$approval_clean.'", "'.$status_clean.'"); '; if(mysqli_query($connect,$query)){ $last_id = mysqli_insert_id($connect); $code = generate("REQ",$last_id); mysqli_query($connect,"update request set trackingcode='$code' where reqnumber='$last_id'"); } } 

Código entero:

 <?php //insert.php $connect = mysqli_connect("localhost", "root", "", "pcrequest"); if(isset($_POST["reqname"])) { $req_date = $_POST["req_date"]; $reqname = $_POST["reqname"]; $date = $_POST["date"]; $empname = $_POST["empname"]; $position = $_POST["position"]; $account = $_POST["account"]; $platform = $_POST["platform"]; $processor = $_POST["processor"]; $ram = $_POST["ram"]; $monitor = $_POST["monitor"]; $phone = $_POST["phone"]; $phonetype = $_POST["phonetype"]; $headset = $_POST["headset"]; $req_table = $_POST["req_table"]; $req_chair = $_POST["req_chair"]; $approval = $_POST["approval"]; $status = $_POST["status"]; $query = ''; function generate ($value='',$inp=""){ return $value.str_pad($inp, 2, "0", STR_PAD_LEFT); } for($count = 0; $count<count($reqname); $count++) { $req_date_clean = mysqli_real_escape_string($connect, $req_date[$count]); $reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]); $date_clean = mysqli_real_escape_string($connect, $date[$count]); $empname_clean = mysqli_real_escape_string($connect, $empname[$count]); $position_clean = mysqli_real_escape_string($connect, $position[$count]); $account_clean = mysqli_real_escape_string($connect, $account[$count]); $platform_clean = mysqli_real_escape_string($connect, $platform[$count]); $processor_clean = mysqli_real_escape_string($connect, $processor[$count]); $ram_clean = mysqli_real_escape_string($connect, $ram[$count]); $monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]); $phone_clean = mysqli_real_escape_string($connect, $phone[$count]); $phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]); $headset_clean = mysqli_real_escape_string($connect, $headset[$count]); $req_table_clean = mysqli_real_escape_string($connect, $req_table[$count]); $req_chair_clean = mysqli_real_escape_string($connect, $req_chair[$count]); $approval_clean = mysqli_real_escape_string($connect, $approval[$count]); $status_clean = mysqli_real_escape_string($connect, $status[$count]); if($req_date_clean != '' && $reqname_clean != '' && $date_clean != '' && $empname_clean != '' && $position_clean != '' && $account_clean != '' && $platform_clean != '' && $processor_clean != '' && $ram_clean != '' && $monitor_clean != '' && $phone_clean != '' && $phonetype_clean != '' && $headset_clean != '' && $req_table_clean != '' && $req_chair_clean != '' && $approval_clean != '' && $status_clean != '') { $query = ' INSERT INTO request(trackingcode, req_date, reqname, date, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, req_table, req_chair, approval, status) VALUES("'.$trackingcode.'", "'.$req_date_clean.'", "'.$reqname_clean.'", "'.$date_clean.'", "'.$empname_clean.'", "'.$position_clean.'", "'.$account_clean.'", "'.$platform_clean.'", "'.$processor_clean.'", "'.$ram_clean.'", "'.$monitor_clean.'", "'.$phone_clean.'", "'.$phonetype_clean.'", "'.$headset_clean.'", "'.$req_table_clean.'", "'.$req_chair_clean.'", "'.$approval_clean.'", "'.$status_clean.'"); '; if(mysqli_query($connect,$query)){ $last_id = mysqli_insert_id($connect); $code = generate("REQ",$last_id); mysqli_query($connect,"update request set trackingcode='$code' where reqnumber='$last_id'"); //exec code : update tablename set trackingcode='$code' where tableprimary_id='$last_id' } } $email_from = "PC Request";//<== update the email address $email_subject = "PC Request for $account_clean"; $message = ''; $message .= '

Hello, World!

'; $message .= ''; $message = ''; $message .= "Good morning,"; $message .= "
"; $message .= "
"; $message .= "Here are the specifications:"; $message .= "
"; $message .= "
"; $message .= ''; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= "
Tracking Code " . $trackingcode . "
Requested by " . $req_date_clean = mysqli_real_escape_string($connect, $req_date[$count]) . "
Requested by " . $reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]) . "
Start Date " . $date_clean = mysqli_real_escape_string($connect, $date[$count]) . "
Employee Name " . $empname_clean = mysqli_real_escape_string($connect, $empname[$count]) . "
Position " . $position_clean = mysqli_real_escape_string($connect, $position[$count]) . "
Account " . $account_clean = mysqli_real_escape_string($connect, $account[$count]) . "
Platform " . $platform_clean = mysqli_real_escape_string($connect, $platform[$count]) . "
Processor " . $processor_clean = mysqli_real_escape_string($connect, $processor[$count]) . "
RAM " . $ram_clean = mysqli_real_escape_string($connect, $ram[$count]) . "
Monitor " . $monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]) . "
Phone " . $phone_clean = mysqli_real_escape_string($connect, $phone[$count]) . "
Phone Type " . $phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]) . "
Headset " . $headset_clean = mysqli_real_escape_string($connect, $headset[$count]) . "
Table " . $req_table_clean = mysqli_real_escape_string($connect, $req_table[$count]) . "
Chair " . $req_chair_clean = mysqli_real_escape_string($connect, $req_chair[$count]) . "
For Approval Click Here!
"; $message .= ""; $to = "";//<== update the email address $headers = "From: $email_from \r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; //$headers .= "Reply-To: $visitor_email \r\n"; //Send the email! mail($to,$email_subject,$message, $headers); //done. redirect to thank-you page. //header('Location: index.php'); //-----------------------FOR IT TEAM-----------------------------// $email_from = "PC Request";//<== update the email address $email_subject = "PC Request for $account_clean"; $message = ''; $message .= '

Hello, World!

'; $message .= ''; $message = ''; $message .= "Good morning,"; $message .= "
"; $message .= "
"; $message .= "Here are the specifications:"; $message .= "
"; $message .= "
"; $message .= ''; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= "
Tracking Code " . $trackingcode . "
Requested by " . $req_date_clean = mysqli_real_escape_string($connect, $req_date[$count]) . "
Requested by " . $reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]) . "
Start Date " . $date_clean = mysqli_real_escape_string($connect, $date[$count]) . "
Employee Name " . $empname_clean = mysqli_real_escape_string($connect, $empname[$count]) . "
Position " . $position_clean = mysqli_real_escape_string($connect, $position[$count]) . "
Account " . $account_clean = mysqli_real_escape_string($connect, $account[$count]) . "
Platform " . $platform_clean = mysqli_real_escape_string($connect, $platform[$count]) . "
Processor " . $processor_clean = mysqli_real_escape_string($connect, $processor[$count]) . "
RAM " . $ram_clean = mysqli_real_escape_string($connect, $ram[$count]) . "
Monitor " . $monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]) . "
Phone " . $phone_clean = mysqli_real_escape_string($connect, $phone[$count]) . "
Phone Type " . $phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]) . "
Headset " . $headset_clean = mysqli_real_escape_string($connect, $headset[$count]) . "
Table " . $req_table_clean = mysqli_real_escape_string($connect, $req_table[$count]) . "
Chair " . $req_chair_clean = mysqli_real_escape_string($connect, $req_chair[$count]) . "
Update Work Status Click Here!
"; $message .= ""; $to = "";//<== update the email address $headers = "From: $email_from \r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; //$headers .= "Reply-To: $visitor_email \r\n"; //Send the email! mail($to,$email_subject,$message, $headers); //done. redirect to thank-you page. //header('Location: index.php'); //-----------------------Without Admin-----------------------------// $email_from = "PC Request";//<== update the email address $email_subject = "PC Request for $account_clean"; $message = ''; $message .= '

Hello, World!

'; $message .= ''; $message = ''; $message .= "Good morning,"; $message .= "
"; $message .= "
"; $message .= "Here are the specifications:"; $message .= "
"; $message .= "
"; $message .= ''; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= ""; $message .= "
Tracking Code " . $trackingcode . "
Requested by " . $req_date_clean = mysqli_real_escape_string($connect, $req_date[$count]) . "
Requested by " . $reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]) . "
Start Date " . $date_clean = mysqli_real_escape_string($connect, $date[$count]) . "
Employee Name " . $empname_clean = mysqli_real_escape_string($connect, $empname[$count]) . "
Position " . $position_clean = mysqli_real_escape_string($connect, $position[$count]) . "
Account " . $account_clean = mysqli_real_escape_string($connect, $account[$count]) . "
Platform " . $platform_clean = mysqli_real_escape_string($connect, $platform[$count]) . "
Processor " . $processor_clean = mysqli_real_escape_string($connect, $processor[$count]) . "
RAM " . $ram_clean = mysqli_real_escape_string($connect, $ram[$count]) . "
Monitor " . $monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]) . "
Phone " . $phone_clean = mysqli_real_escape_string($connect, $phone[$count]) . "
Phone Type " . $phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]) . "
Headset " . $headset_clean = mysqli_real_escape_string($connect, $headset[$count]) . "
Table " . $req_table_clean = mysqli_real_escape_string($connect, $req_table[$count]) . "
Chair " . $req_chair_clean = mysqli_real_escape_string($connect, $req_chair[$count]) . "
"; $message .= ""; $to = "";//

 $('#save').click(function(){ var trackingcode = []; var req_date = []; var reqname = []; var date = []; var empname = []; var position = []; var account = []; var platform = []; var processor = []; var ram = []; var monitor = []; var phone = []; var phonetype = []; var headset = []; var req_table = []; var req_chair = []; var approval = []; var status = []; $('.trackingcode').each(function(){ trackingcode.push($(this).text()); }); $('.req_date').each(function(){ req_date.push($(this).text()); }); $('.reqname').each(function(){ reqname.push($(this).text()); }); $('.date').each(function(){ date.push($(this).find('input').val()); }); $('.empname').each(function(){ empname.push($(this).text()); }); $('.position').each(function(){ position.push($(this).find('select').val()); }); $('.account').each(function(){ account.push($(this).find('select').val()); }); $('.platform').each(function(){ platform.push($(this).find('select').val()); }); $('.processor').each(function(){ processor.push($(this).find('select').val()); }); $('.ram').each(function(){ ram.push($(this).find('select').val()); }); $('.monitor').each(function(){ monitor.push($(this).find('select').val()); }); $('.phone').each(function(){ phone.push($(this).find('select').val()); }); $('.phonetype').each(function(){ phonetype.push($(this).find('select').val()); }); $('.headset').each(function(){ headset.push($(this).find('select').val()); }); $('.req_table').each(function(){ req_table.push($(this).find('select').val()); }); $('.req_chair').each(function(){ req_chair.push($(this).find('select').val()); }); $('.approval').each(function(){ approval.push($(this).text()); }); $('.status').each(function(){ status.push($(this).text()); }); $.ajax({ url:"insert-message.php", method:"POST", data:{trackingcode:trackingcode, req_date:req_date, reqname:reqname, date:date, empname:empname, position:position, account:account, platform:platform, processor:processor, ram:ram, monitor:monitor, phone:phone, phonetype:phonetype, headset:headset, req_table:req_table, req_chair:req_chair, approval:approval, status:status}, success:function(data){ alert(data); window.location.reload() $("td[contentEditable='true']").text(""); for(var i=2; i<= count; i++) { $('tr#'+i+'').remove(); } fetch_item_data(); } }); 

Note que usted define (en su código actualizado):

 $query .= 'INSERT INTO request(trackingcode, req_... 

Cuando se usa . antes de = concat la cadena a una existente. Así que básicamente agrega más y más INSERTAR a su consulta. Retire el .