简体   繁体   English

如何使用php将记录插入两个mysql表中?

[英]How to insert record into two mysql table using php?

I have to two tables in my database one is "test1" and the second is "test2". 我在数据库中必须有两个表,一个是“ test1”,第二个是“ 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.. 测试1的字段为“ id”,“ survey_no”,“姓氏”,对于测试字段,我具有“ id”,“ survey_no”,“地址”,test1和test2的ID为主键并设置为auto -increment ..我在这里想要的是,我在test1的“ survey_no”中插入的内容也应该插入在“ test2的survey_no”中插入..有人可以帮助我吗..请

I have here the code for insertion called sample.php 我这里有插入代码,称为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>

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')");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM