如何以第一种形式插入数据并以第二种形式更新相同的数据,依此类推

时间:2018-05-14 20:45:35

标签: php html session

我想问一下,如果我想在第一次点击按钮(在第一页上)插入数据,然后在其余表格上用新字段更新数据库,该怎么办。

这是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显示为未定义的变量。

1 个答案:

答案 0 :(得分:0)

最后,我有一个解决方案。 在session变量的帮助下,我可以在每个网页上移动id