How to insert record into two mysql table using php?


Problem :

I have to two tables in my database one is "test1" and the second is "test2". and the field for test 1 is "id","survey_no","lastname" and for the field of test i have "id","survey_no","address" the id for test1 and test2 are primary key and set to auto-increment..what i want here is that what ever i insert in "survey_no" in test1 should also be insert inserted in "survey_no in test2"..can somebody please help me with it..please..

I have here the code for insertion called sample.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form method="POST" class="signin" action="" name="add" id="form1">
<fieldset class="textbox">
    <label class="province_id">
        <span>province id</span>
            <input id="province" name="survey_no" type="number" value="" autocomplete="on" placeholder="survey_no">
    </label>
    <label class="municipality">
        <span>Municipality</span>
            <input id="municipality" name="lastname" value="" type="text" autocomplete="on" placeholder="lastname">
    </label>
    <br />
    <br />
    <button id="submit" type="submit" name="submit">Save</button>
    <button id="submit" type="reset" name="reset">Reset</button> 
    </fieldset>


    <?php
    $conn=mysql_connect('localhost','root','');
            if(!$conn)
            {
                die('could not connect:' .mysql_error());
            }
            mysql_select_db("sample",$conn);

    if(isset($_POST['submit']))
        {

            $survey_no=$_POST['survey_no'];
            $lastname=$_POST['lastname'];

            $result = mysql_query("INSERT INTO `test1`(survey_no,lastname) 
     VALUES ('$survey_no','$lastname')");

     if($result)
    {
    echo "<script type=\"text/javascript\">".
    "alert('Saved!');".
    "</script>";
    }
    else{
        echo "<script type=\"text/javascript\">".
    "alert('Failed!');".
    "</script>";
        } 
}

?>

</form>


</body>
</html>


Solution :

so far i understand you want this

$result = mysql_query("INSERT INTO `test1`(survey_no,lastname) 
     VALUES ('$survey_no','$lastname')");


$result1 = mysql_query("INSERT INTO `test2`(survey_no) 
     VALUES ('$survey_no')");

Mysql Tutorials

Mysql Howto..

How to arrange this data in columns?

How to copy an entire row from one table to another in mysql with the second table having a modified field

How can be datetime formatted for mysql?

How to store windows directory links in MySQL tables?

How to display a students rank out of hundred students using PHP & MySQL?

How to run a function in a transaction with MySQL and SQLite?

How do I convert a velocity array [x, y, z] into a string to be stored in a database?

How to fetch the first row a user has save on a table in php-mysql

how to setup a mysql table for use with UPDATE WHERE statement

How to practically check whether server can handle many MySQL connections simultaneously

How to design subtype in mysql workbench?

How to generate Email alert by comparing dates? [closed]

How to import data from external mysql db into a Symfony2 Project?

How can I “tell” MySQL to return the maximum value for each category and some other value associated with that max value?

How to convert DateTime to a number in MySQL?

How to use an MySQL association table to return categories not currently assigned to an entry

How to join tables on varchar fields in MySQL

How to convert a class object to generic object type?

How do I make search results for a PHP / MySQL query link to data in a single row?

How to use PHP to loop through MySQL result set with records grouped by decade?

How to store data pairs in a mysql database?

How to select mysql rows based off time increment adjustments (vb mysql)

How to combine two PDO statements in one MySQL query?

How to manually build mysql cache

How to decrypt password in mysql

how to make Mysqldatabase converter in c#

How to assign default value in mysql Storedprocedure

How do profile pictures work?

How to convert time in seconds to HH:MM:SS format in MySQL?

How to insert in MYSQL different data for same ID