简体   繁体   中英

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:

<!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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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