2

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>
6
  • 3
    Warning : mysql_query is deprecated ! Commented May 14, 2015 at 3:58
  • Error 2 : id="submit" cant be more than 1 Commented May 14, 2015 at 4:02
  • Yes, you should have unique id Commented May 14, 2015 at 4:04
  • @jQuery i'm sorry for that, i forgot to change that one..maybe i just called it submit1... Commented May 14, 2015 at 4:41
  • i know that mysql is depracated but i will change to mysql but first i need to finish this first before i go to mysqli..can you help me with it? Commented May 14, 2015 at 4:42

1 Answer 1

4

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')");
Sign up to request clarification or add additional context in comments.

7 Comments

i have one last question, it is related to this..how can i insert the records from test1 to test 2?
you want to add all record of test1 in test 2?
something like that,is it possible even if test1 and test2 has different fields?
yes it is possible fetching from test1 and insert into test2
$result = mysql_query("SELECT * fron test1"); while($row=mysql_fetch_array($result)){ $survey_no=$row['survey']; mysql_query("INSERT INTO test2(survey_no) VALUES ('$survey_no')"); } you will achieve like this..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.