簡體   English   中英

如何使用php將記錄插入兩個mysql表中?

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

我在數據庫中必須有兩個表,一個是“ test1”,第二個是“ test2”。 測試1的字段為“ id”,“ survey_no”,“姓氏”,對於測試字段,我具有“ id”,“ survey_no”,“地址”,test1和test2的ID為主鍵並設置為auto -increment ..我在這里想要的是,我在test1的“ survey_no”中插入的內容也應該插入在“ test2的survey_no”中插入..有人可以幫助我嗎..請

我這里有插入代碼,稱為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>

到目前為止,我知道你想要這個

$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