php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11427 Duplicate record insertion using SQL
Submitted: 2001-06-12 06:27 UTC Modified: 2001-06-12 12:27 UTC
From: john at neutralize dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 4.0.5 OS: Win NT4 PWS
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: john at neutralize dot com
New email:
PHP Version: OS:

 

 [2001-06-12 06:27 UTC] john at neutralize dot com
The script was fine for an Access database but when I changed it over for a MySQL database the insertion queries sometimes (most of the time but not all the time) adds two records to the database.

Heres my connection:

$db_hostname   = "jd";
$db_username   = "john";
$db_password   = "nirvana";
$db_database   = "neuquote";

$conn = mysql_connect($db_hostname, $db_username, $db_password)
  or die ('Unable to connect to database');
		
$db = mysql_select_db($db_database,$conn)
  or die ('Unable to connect to database');

Heres my queries:

$today = getdate();
$today = $today['mday']."/".$today['mon']."/".$today['year']." ".$today['hours'].":".$today['minutes'].":".$today['seconds'];
						
// Insert new company
$sql="INSERT INTO Company (CompanyName,CompanyAddress1,CompanyAddress2,CompanyAddress3,CompanyAddress4,CompanyArea,CompanyCountryId,CompanyPostalCode,CompanyTel,CompanyFax,CompanyEmail,CompanyURL,CompanyDesc,CompanyStatus,TimeCompanyCreated) ". 
				 "VALUES ('$company_name','$company_address1','$company_address2','$company_address3','$company_address4','$company_area',$company_country,'$company_postal_code','$company_tel','$company_fax','$company_email','$company_url','$company_desc',0,'$today')";

$res_insert_company = mysql_query($sql,$conn);

//create a new hash to insert into the db and the confirmation email
$hash=md5($email.$hidden_hash_var);
$sql="INSERT INTO CompanyContact (ContactTitle,ContactFirstName,ContactSurname,ContactMiddleInitial,ContactAddress1,ContactAddress2,ContactAddress3,ContactAddress4,ContactArea,ContactCountryId,ContactPostalCode,ContactTel,ContactExt,ContactFax,ContactEmail,ContactPositionId,ContactUsername,ContactPassword,ContactRemoteAddress,ContactConfirmHash,ContactStatus,TimeContactCreated,CompanyId) ". 
					"VALUES ($title,'$first_name','$surname','$middle_initial','$address1','$address2','$address3','$address4','$area',$country_code,'$postal_code','$tel','$ext','$fax','$email',$pos,'$user_name','". md5($password1) ."','$GLOBALS[REMOTE_ADDR]','$hash',0,'$today',$company_id)";

$res_insert_contact=mysql_query($sql,$conn);

Most of the time two companies are added to the db and sometimes two companycontacts, this is not consistent though as sometimes it works fine.

I have checked the submit button is not being clicked twice and the code is running one insert for company and one for companycontact.

Is this a common problem with MySQL and PHP?

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-12 12:27 UTC] sniper@php.net
submitted twice

 [2003-07-20 14:29 UTC] helmutkemper at yahoo dot com
I have a same problem using windows me, php 4.0.1 and mysql 
4.23.23

Kemper
 [2004-05-13 22:21 UTC] certaplus at hotmail dot com
I have the same problem. On two different servers, I run Apache on one and IIS on the other. Both are ran on Windows XP with MySQL 4.0.1 and PHP 4.3.4. I also use IE 6.0 as well as Netscape 7.1 to display the pages.

form code :

...</tr>
          </table>
        <form name="form1" method="POST" action="process.php">
         
            <p>Brand 
              <input type="text" name="brand">
            </p>
            <p> Model 
              <input type="text" name="model">
            </p>
            <p> 
              <input type="submit" name="Submit" value="submit">
            </p>
          </form>
        </div>...


PHP code :
<?php


define ('db_user', 'hendrix');
define ('db_password', 'rememberme');
define ('db_host', 'localhost');
define ('db_name', 'products');

$link = mysql_connect (db_host, db_user, db_password)
	or die ("Couldn't connect");


mysql_select_db (db_name)
	or die ("Database couldn't be accessed");


$brand= $_POST['brand'];
$model= $_POST['model'];

$query="INSERT INTO product (id,brand,model) VALUES ('','$brand','$model')";
$result= mysql_query($query,$link);


if(!mysql_query($query,$link)) die(mysql_error());
echo "Your information as been entered";


mysql_close();


?>

Jack
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Sep 08 23:01:28 2024 UTC