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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing 2 records on insert

Status
Not open for further replies.

jkappl

Technical User
Oct 17, 2003
2
US
I have one insert statement which is supposed to produce one record in the database, but instead it produces two. My program is simple in that I open the database, do one insert, and close the database.



The two records are identical except for the auto-incremented integer which is a primary key, and one of the columns containing a unix timestamp integer value inserted as simply an integer. I obtain the value before trying INSERT and try INSERT only once (verified by before and after print values) .

It is a mystery how the constant value for the time can change between the two records while I cannot locate where the second record is being written.

Please Help ....
 
An insert statement doesn't necessarily insert only one record. It can insert any number.

For example, in a table "fubar" with two columns "foo" and "bar", this query:

INSERT INTO fubar (foo, bar) VALUES (1, 'snafu'), (2, 'fubar')

will insert two records.

Could something like that be happening in your query?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Your thought is the common way that one statement might write two records. This not what is happening.

It almost seems that the SQL interpreter is playing with me. I can use the same insert statement in a different (much smaller file) and it works as it should. Same statement, cut and pasted to a different file, but now it works.

I was looking for any experience that you might have that suggests that the interpreter can do this sort of thing. And how one keeps the code clean so as to avoid this sort of anomaly.

Here is the code.


$link = mysql_connect("localhost", "peak-boo", "jude01")
or die("Could not connect: " . mysql_error());
// print ("Connected successfully");

mysql_select_db("peakboo_FirstTry")
or die("Unable to select database");
// print ("Database Selected");

/* test variables */

$custom = 'no-affil';
$owners_name = 'John Doe';
$owners_address = '21 tester street, Columbus, Ohio, 34567, status = confirmed';
$option_name1 = 'bookcode';
$option_selection1 = 'UIMC';
$option_name2 = 'customer_number';
$option_selection2 = '1061193344';
$time_xx = getdate();
$sold_time = $time_xx[0];
$activation_key = 'AAAA-BBBB-CCCC-DDDD';
$payer_email = 'jdoe@peak-books.com';
$tempxx = '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';
for( $lci = 0; $lci < 15; $lci++){
$str_acr = $str_acr.$tempxx;
}
echo '<br>before insert','$sold_time_1 = ',$sold_time;
$query = &quot;INSERT INTO ready_for_pickup &quot;;
$query .= &quot;($option_name1,custom,sold,$option_name2,owners_name,owners_address,activation_key,email,str_acr) &quot;;
$query .= &quot;VALUES('$option_selection1','$custom','$sold_time','$option_selection2','$owners_name','$owners_address','$activation_key','$payer_email','$str_acr')&quot;;

mysql_query($query)
or die(&quot;<br>Insert Failed&quot;);
// print (&quot;Entry Complete&quot;);
// printf (&quot;Last inserted record has id %d\n&quot;, mysql_insert_id());

echo '<br>after insert','$sold_time_2 = ',$sold_time;


mysql_close($link);

-------------------------------------
Thanks for answering !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top