I have two tables in my database.Users table and contact table,i want to use the ID that was generated from Users table to contact table.Below is the script for Users and contact.

<?php
    // If the values are posted, insert them into the database.
    if (isset($_POST['username']) && isset($_POST['password'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        $query = "INSERT INTO `Users` (ID,username, password, email) VALUES (NULL,'$username', '$password', '$email')";
        $result = mysql_query($query);
        $LAST_INSERTED = mysql_insert_id ($connection );
        if($result){
            echo  "User Created Successfully."; 
        }
    }
    ?>


    <?php

         if(isset($_POST['submit']))
        {
       require('config.php');

        if(empty($_POST['firstname'])) { // if no firstname has been supplied
        echo"<p>Please Enter a firstname</p>";
        } else {
        $firstname=$_POST['firstname']; // else assign it a variable
        }

        if(empty($_POST['lastname'])) { // if no lastname has been supplied
        echo"<p>Please Enter a lastname</p>";
        } else {
        $lastname=$_POST['lastname']; // else assign it a variable
        }


        if(empty($_POST['sex'])) { // if no sex has been supplied
        echo"<p>Please Enter a sex</p>";
        } else {
        $sex=$_POST['sex']; // else assign it a variable
        }


        if(empty($_POST['phone'])) { // if no phone has been supplied
        echo"<p>Please Enter a phone</p>";
        } else {
        $phone=$_POST['phone']; // else assign it a variable
        }


        if(empty($_POST['address'])) { // if no address has been supplied
        echo"<p>Please Enter a address</p>";
        } else {
        $address=$_POST['address']; // else assign it a variable
        }  


        if(empty($_POST['country'])) { // if no country has been supplied
        echo"<p>Please Enter a country</p>";
        } else {
        $country=$_POST['country']; // else assign it a variable
        }


        if(empty($_POST['city'])) { // if no city has been supplied
        echo"<p>Please Enter a city</p>";
        } else {
        $city=$_POST['city']; // else assign it a variable
        }


        if(empty($_POST['state'])) { // if no state has been supplied
        echo"<p>Please Enter a state";
        } else {
        $state=$_POST['state']; // else assign it a variable
        }


        if(empty($_POST['zip'])) { // if no zip has been supplied
        echo"<p>Please Enter a zip</p>";
        } else {
        $zip=$_POST['zip']; // else assign it a variable
        }


        if(empty($_POST['month'])) { // if no month has been supplied
        echo"<p>Please Enter a month</p>";
        } else {
        $month=$_POST['month']; // else assign it a variable
        }


        if(empty($_POST['day'])) { // if no day has been supplied
        echo"<p>Please Enter a day</p>";
        } else {
        $day=$_POST['day']; // else assign it a variable
        }


        if(empty($_POST['year'])) { // if no year has been supplied
        echo"<p>Please Enter a year</p>";
        } else {
        $year=$_POST['year']; // else assign it a variable
        }


        if(empty($_POST['ethnic'])) { // if no ethnic has been supplied
        echo"<p>Please Enter a ethnic</p>";
        } else {
        $ethnic=$_POST['ethnic']; // else assign it a variable
        }




                                //We protect the variables
                                $firstname = mysql_real_escape_string($_POST['firstname']);
                                $lastname = mysql_real_escape_string($_POST['lastname']);
                                $sex = mysql_real_escape_string($_POST['sex']);
                                $phone = mysql_real_escape_string($_POST['phone']);
                                $address = mysql_real_escape_string($_POST['address']);
                                $country = mysql_real_escape_string($_POST['country']);
                                $city = mysql_real_escape_string($_POST['city']);
                                $state = mysql_real_escape_string($_POST['state']);
                                $zip = mysql_real_escape_string($_POST['zip']);
                                $month = mysql_real_escape_string($_POST['month']);
                                $day = mysql_real_escape_string($_POST['day']);
                                $year = mysql_real_escape_string($_POST['year']);
                                $ethnic = mysql_real_escape_string($_POST['etnic']);



// use a query to insert the data into the database.
 //We save the informations to the databse





        $sql="INSERT INTO contact(c_id,ID,firstname, lastname,sex,phone,address,country,city,state,zip,month,day,year,ethnic)
        VALUES(NULL,$LAST_INSERTED(),'$firstname','$lastname','$sex','$phone','$address', '$country','$city','$state','$zip','$month','$day','$year','$ethnic')";

        $result=mysql_query($sql);
        if($result){
        echo "successfull";
        }
        else
        {
        echo "failed";
        }
        }
        ?>

On line 143 change $LAST_INSERTED() to $LAST_INSERTED

If you change:

$result=mysql_query($sql);

to:

$result = mysql_query($sql) or die(mysql_error()); 

You get a hint at what's possibly wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.