简体   繁体   English

如何以第一种形式插入数据并以第二种形式更新相同数据等等

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

I want to ask, what if I want to insert data on first button click ( which is on the first page ) and then update the database with new fields on the rest forms. 我想问一下,如果要在第一次单击按钮(位于第一页)上插入数据,然后在其余表单上使用新字段更新数据库时该怎么办?

Here is the index.php: 这是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>

I inserted the data of first page in the second form which is done but now when user fill the second form the data should be updated in the same field. 我在第二个表单中插入了第一页的数据,但是现在当用户填写第二个表单时,应该在同一字段中更新数据。 Second Page where I want to perform the update query: 我要在其中执行更新查询的第二页:

<?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>

And to perform this action I wrote this on page 3: 为了执行此操作,我在第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>

Here $id shows as undefined variable. 这里$ id显示为未定义变量。

Finally, I have got a solution. 终于,我找到了解决方案。 With the help of session variable, I am able to move the id on each of my web pages. 借助session变量,我可以在每个网页上移动id

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

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