dsiuz86842 2015-05-14 03:53
浏览 87
已采纳

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

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>
  • 写回答

1条回答 默认 最新

  • doujiu8826 2015-05-14 04:39
    关注

    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')");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179