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.