0

I want to insert values into 3 different tables with auto_increment of primary key.
My code below creates 2 records for the first table and 1 each for the second and third AND a Failure 'duplicate entry xxxx for key PRIMARY' for the second and third. The ADD RECORD does allocate same unique ID for the three tables (excepting the failures)

Can someone point out to me where the duplication is occuring ?

<?php 
require_once 'dbconnect.php';
if 	(isset($_POST['ID']) &&
	isset($_POST['Name']) &&
	isset($_POST['Tons']) &&
	isset($_POST['Built']) &&
	isset($_POST['Builder']) &&
	isset($_POST['Number']) &&
	isset($_POST['Launched']) &&
	isset($_POST['Modified']) &&
	isset($_POST['Name2']) &&
	isset($_POST['Name3']) &&
	isset($_POST['Name4'])&&
	isset ($_POST['Events']) &&
	isset ($_POST['Fate']))	
{		
	$id	 = get_post('ID');	
	$name  	 = get_post('Name');
	$tons	 = get_post('Tons');
	$built   = get_post('Built');
	$builder = get_post('Builder');
	$number  = get_post('Number');
	$launched= get_post('Launched');
	$modified= get_post('Modified');
	$name2	 = get_post('Name2');
	$name3   = get_post('Name3');
	$name4   = get_post('Name4');
	$events	 = get_post('Events');
	$fate	 = get_post('Fate');
	
$query 	= "INSERT INTO launch VALUES" ."(NULL, '$name', '$tons', '$built', '$builder','$number','$launched','$modified')";
$result	= mysql_query($query);
$insertID=mysql_insert_id();
if (!mysql_query($query))
		echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";		
$query = "INSERT INTO names VALUES" ."('$insertID','$name2','$name3','$name4','$modified')";	
$result	= mysql_query($query);
if (!mysql_query($query))
		echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";
$query = "INSERT INTO fate VALUES" ."('$insertID', '$events','$fate','$modified')";	
$result	= mysql_query($query);
if (!mysql_query($query))
		echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";
}                                                                                     
echo <<<_END
<form action="lchnamesfate.php" method="post">
<pre>
    ID       <input type="text" name="ID" />
    Name     <input type="text" name="Name" />
    Tons     <input type="text" name="Tons" />
    Built    <input type="text" name="Built" />
    Builder  <input type="text" name="Builder" />
    Number   <input type="text" name="Number" />
    Launched <input type="text" name="Launched" />
    Modified <input type="text" name="Modified" />
    Name2    <input type="text" name="Name2" />
    Name3    <input type="text" name="Name3" />
    Name4    <input type="text" name="Name4" />
    Events   <input type="text" name= "Events" />
    Fate     <input type="text" name= "Fate" />
<br/>
         <input type="submit" value="ADD RECORD" /></pre>
</form>
_END;

mysql_close($db_server);


function get_post($var)
{
	return mysql_real_escape_string($_POST[$var]);
}
?>

Many thanks.

3
Contributors
4
Replies
7
Views
5 Years
Discussion Span
Last Post by diafol
0
$result	= mysql_query($query);
$insertID=mysql_insert_id();
if (!mysql_query($query)) //I think this runs the query again

Use mysql_affected_rows() If no records inserted it will return -1
Otherwise, if you're just inserting one record at a time, you'll expect the value to be 1.

0

the name&fat table with the insertid must have contain the new id you are inserting on the other 2 tables. or drop all the records on the tables (name&fat) and try again.

0

Thanks ardav, that eliminates the duplication. However, I did try just if(!$query)
and that too worked. A fluke or valid alternative ?

0
if(!$query)

just checks to see if $query is NOT false

So if $query is equal to anything that cannot be construed as false, it's equivalent to 'true' when using a conditional like that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.