Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert query problem

Status
Not open for further replies.

dalin

Programmer
Joined
Feb 19, 2004
Messages
5
Location
CA
I'm having problems with the following function:
Code:
Function  Commit_Company ($uid, $company, $description, $address, $city, 
		$province, $postalcode, $country, $web, $print, $online, $radio, $venue) 
{
	global $hostname, $password, $user;
	mysql_connect($hostname,$user,$password);
	mysql_select_db("HIV");
	$result = mysql_query("INSERT INTO Media (Company, Description, Address, 
		City, State, PostalCode, Country, Web, Print, Online, Radio, Venue) 
		VALUES ('$company', '$description', '$address', '$city', 
		'$province', '$postalcode', '$country', '$web', 
		'". ($print=="on" ? "-1" : "0"). "', 
		'". ($online=="on" ? "-1" : "0"). "', 
		'". ($radio=="on" ? "-1" : "0"). "', 
		'". ($venue=="on" ? "-1" : "0"). "');");
	echo&quot;<BR>error:&quot;;
	printf(mysql_error());
	echo&quot;<BR>rows affected:&quot;;
	printf(mysql_affected_rows());
	echo&quot;<BR>&quot;;
	printf(&quot;Last inserted record has id %d\n&quot;, mysql_insert_id());
	$new_cid = mysql_insert_id();
	$lastID = mysql_query(&quot;SELECT max(Media.CompanyID) as CID FROM Media&quot;);
	echo&quot;<BR>Max of CID:&quot;;
	printf(mysql_result($lastID,0,&quot;CID&quot;));
	$state = &quot;CreateContact&quot;;
	/*Contact_Form($state,$blank,$new_cid);*/
}

It works only about a quarter of the time.

Usually it gives the following errors, but the row is in fact created:

error:Duplicate entry '!test' for key 2
rows affected:-1
Last inserted record has id 0
Max of CID:157

Also it seems more likely to work when I run the PHP page from Netscape instead of Explorer. I don't know if that's just coincidence.

What am I doing wrong???
 
You might want to change your approach to error checking and add more of it:
Code:
mysql_connect($hostname,$user,$password) [COLOR=red]OR die(&quot;Cannot connect to server: &quot;.mysql_error())[/color];
mysql_select_db(&quot;HIV&quot;) [COLOR=red]OR die(&quot;Cannot select database: &quot;.mysql_error())[/color];

# do the same for the query
$SQL = &quot;INSERT .....&quot;;
$result = mysql_query($SQL) OR die(&quot;Invalid query: &quot;$SQL.&quot; MySQL said: &quot; . mysql_error());

Find an example where you encounter the error and where the SQL is printed out. That will help.
 
thanks for the quick replies.

I also connect to this database via Access and ODBC.
I confirm that the records are in fact created by opening the table in Access.
 
The table has an autoincrement type: int(11) named CompanyID as the PK.

I see that CompanyID is also indexed. Since the field is assigned as the PK is it implicitly indexed or am I correct in also indexing the field.

Also Company is Unique.
 
Are you sure you're seeing the data that was just entered? The error message appears to me that you have a column with a unique index. And you're trying to insert a duplicate record in that column.

Just to ask the dumb question, are you sure you're not seeing the previously-existing record in the table?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I removed the Unique index from the &quot;Company&quot; field.

Now the query is creating 2 rows!

That explains the errors since it was trying to make 2 rows with the same company name.

But why is it making 2 rows????

The function is saying that only 1 row is being made:

rows affected:1
Last inserted record has id 182
Max of CompanyID:182

But the id numbers are 2 higher than before the function was called.
 
After more frustration I discovered that the form that calls the function was submiting twice.

After more frustration I discovered that the problem was indeed only happening in Explorer and not Netscape.

After more frustration I discovered that the privacy features of my firewall (which inserts a bit of code at the begining and end of every webpage) was somehow causing the form to submit twice.

I've sent them an email asking for a workaround.

I'm just learning html and I can't figure out why thier code is doing this. Here it is if anyone has any suggestions:

Code:
<HTML>
<head>

<!-- ZoneLabs Privacy Insertion -->
<script language='javascript' src='[URL unfurl="true"]http://127.0.0.1:2051/js.cgi?pcaw&r=11538'></script>[/URL]


html resumes...


...and then at the end

</HTML>
<!-- ZoneLabs Popup Blocking Insertion -->
<script language='javascript'>postamble();</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top