gpt4 book ai didi

php - 设置操作后,html 发布表单不会发送数据

转载 作者:行者123 更新时间:2023-11-28 03:22:34 25 4
gpt4 key购买 nike

action="" 没有设置时,表单将发送输入的数据并将其存储在数据库中。

但是,当我使用 action="order.php" 重定向表单后,它不会将数据发送到数据库。

我已经删除了数据库信息我没有遗漏

这是我的 PHP 代码:

<?php 
session_start();

?>
<?php
include('inc/header.php');
?>

<?php
include('inc/nav.php');
?>


<div>

<?php

if (!empty($_POST)) {

$db = new PDO('mysql:host=localhost;dbname=;charset=utf8', '', '');

$stmt = $db->prepare("INSERT INTO user (first_name, last_name, email, address_line_1, address_line_2, city, postcode)
values (?,?,?,?,?,?,?)");


$first_name = $_POST["firstname"];
$last_name = $_POST["lastname"];
$email = $_POST["email"];
$address_line_1 = $_POST["address1"];
$address_line_2 = $_POST["address2"];
$city = $_POST["city"];
$postcode = $_POST["postcode"];

$stmt->bindParam(1, $first_name);
$stmt->bindParam(2, $last_name);
$stmt->bindParam(3, $email);
$stmt->bindParam(4, $address_line_1);
$stmt->bindParam(5, $address_line_2);
$stmt->bindParam(6, $city);
$stmt->bindParam(7, $postcode);

$success = $stmt->execute();

$orderplaced = 'order.php';

if ($success) {

}
else {

print "not successful";
}
}
?>

HTML:

    <form action="order.php" method="post">
<fieldset>
<legend> <h2>Checkout</h2></legend>
<ul class="checkout">

<li><label>First Name: <input type="text" name="firstname" placeholder="Enter your first name" required/></label></li>

<li><label>Last Name: <input type="text" name="lastname" placeholder="Enter your last name" required/></label></li>

<li><label>Email: <input type="text" name="email" placeholder="Enter your email address" required/></label></li>

<li><label>Address Line 1: <input type="text" name="address1" placeholder="Enter your address" required/></label></li>

<li><label>Address Line 2: <input type="text" name="address2" placeholder="Enter your address" required/></label></li>

<li><label>City: <input type="text" name="city" placeholder="Enter your city" required/></label></li>

<li><label>Postcode: <input type="text" name="postcode" maxlength="10" placeholder="Enter your postcode" required/></label></li>

<li><input type="submit" name="submit"/> </li>
</ul>
</fieldset>
</form>

</div>

<?php
include('inc/footer.php');
?>

最佳答案

你有两个选择:
1)去掉表单的action,让表单自己提交,这样提交的数据会保存在DB中,然后重定向到order.php

<form action="" method="post">

然后就成功了

if ($success) { 
header('location: /order.php');
}

2) 将所有提交的 php 部分复制到 order.php 并让表单保持原样

关于php - 设置操作后,html 发布表单不会发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23547389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com