簡體   English   中英

如何以第一種形式插入數據並以第二種形式更新相同數據等等

[英]How to insert data in first form and update the same data in second form and so on

我想問一下,如果要在第一次單擊按鈕(位於第一頁)上插入數據,然后在其余表單上使用新字段更新數據庫時該怎么辦?

這是index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<body>

 <div class="wrapperDiv">
    <form action="form-page2.php" method="post" id="form">
        <table>
          <tr>
            <th colspan="3" scope="row">
              <h3 style="border-bottom:1px solid #000;">Form 1 </h3>
            <div align="center"></div></th>
          </tr>
          <tr>
            <th scope="row"><div align="right">Name</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="text" name="name" class="required" value="" /></td>
          </tr>
          <tr>
            <th scope="row"><div align="right">Email</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="text" name="email" class="required" value="" /></td>
          </tr>
          <tr>
            <th scope="row"><div align="right">Mobile</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="text" name="mobile" value="" /></td>
          </tr>
          <tr>
            <th scope="row">&nbsp;</th>
            <td>&nbsp;</td>
            <td><input type="submit" name="submitBtn1" id="submitBtn1" value="Next" /></td>
          </tr>
        </table>
    </form>
</div><!-- end of .wrapperDiv -->

</body>
</html>

我在第二個表單中插入了第一頁的數據,但是現在當用戶填寫第二個表單時,應該在同一字段中更新數據。 我要在其中執行更新查詢的第二頁:

<?php
if (isset($_POST['submitBtn1'])) {
    $servername = "localhost";
    $username   = "root";
    $password   = "";
    $database   = "phpmultipage";

    $conn = mysqli_connect($servername, $username, $password, $database);

    if (!$conn) {
        die("connection failed: " . mysqli_connect_error());
    } else {
        echo 'you are on';
    }

    $name   = $_POST['name'];
    $email  = $_POST['email'];
    $mobile = $_POST['mobile'];

    $sql = "INSERT into detail (name,email,contact) VALUES ('$name','$email','$mobile')";

    if (mysqli_query($conn, $sql)) {
        echo 'record added. Your ID:' . mysqli_insert_id($conn);
        $id = mysqli_insert_id($conn);
        echo $id;
    } else {
        echo 'error';
    }
    mysqli_close($conn);
}
?>

<!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>
<title>Form 2</title>

</head>

<body>

<div class="wrapperDiv">
    <form action="form-page3.php" method="post" id="form">
        <?php
        if(!isset($_POST['submitBtn1'])) header('location: index.php');
        foreach($_POST as $key => $value) {
            if($key != 'submitBtn1') {
            ?>
        <input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>" />
            <?php   
            }
        }
        ?>
        <table width="500">
          <tr>
            <th colspan="3" scope="row"><h3>Form 2</h3></th>
          </tr>
          <tr>
            <th scope="row"><div align="right">Password</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="password" id="password" name="password" value="" /></td>
          </tr>
          <tr>
            <th scope="row"><div align="right">Confirm Password</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="password" name="confirm_password" id="confirm_password" value="" /></td>
          </tr>
          <tr>
            <th scope="row">&nbsp;</th>
            <td>&nbsp;</td>
            <td><input type="submit" name="submitBtn3" id="submitBtn3" value="Next" /></td>
          </tr>
        </table>
    </form>
  </div>
</body>
</html>

為了執行此操作,我在第3頁上寫道:

<?php
if (isset($_POST['submitBtn3'])) {
    $servername = "localhost";
    $username   = "root";
    $password   = "";
    $database   = "phpmultipage";

    $con = mysqli_connect($servername, $username, $password, $database);

    if (!$con) {
        die("connection failed: " . mysqli_connect_error());
    } else {
        echo 'you are on';

    }

    $password         = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $sqli             = "UPDATE detail SET password='$password',cnf_pass='$confirm_password' WHERE user_id=$id";

    if (mysqli_query($con, $sqli)) {
        echo 'record added';
    } else {
        echo 'error';
    }
    mysqli_close($con);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Form 3 </title>

</head>

<body>
    <div class="wrapperDiv">
    <form action="process-complete.php" method="post" id="form">
        <?php
        if(!isset($_POST['submitBtn3'])) header('location: form-page2.php');
        foreach($_POST as $key => $value) {
            if($key != 'submitBtn3') {
            ?>
        <input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>" />
            <?php   
            }
        }
        ?>
        <table width="500">
          <tr>
            <th colspan="3" scope="row"><h3>Form 3</h3></th>
          </tr>
          <tr>
            <th scope="row"><div align="right">City</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="text" name="city" value="" class="required" /></td>
          </tr>
          <tr>
            <th scope="row"><div align="right">Street</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="text" name="street" value="" class="required" /></td>
          </tr>
          <tr>
            <th scope="row"><div align="right">District</div></th>
            <td><div align="center"><strong>:</strong></div></td>
            <td><input type="text" name="district" value="" class="required" /></td>
          </tr>
          <tr>
            <th scope="row">&nbsp;</th>
            <td>&nbsp;</td>
            <td><input type="submit" name="submitBtn2" id="submitBtn2" value="Save" /></td>
          </tr>
        </table>
    </form>
  </div>

</body>
</html>

這里$ id顯示為未定義變量。

終於,我找到了解決方案。 借助session變量,我可以在每個網頁上移動id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM