1

我有一个名为(order.php)的php订单表格,当用户单击(提交按钮“下一步”)时,它会将他带到另一个名为(confirm-order.php)的页面

(confirm-order.php) 显示用户使用 $_POST[] 从 (order.php) 提交的信息,并将这些信息中的每一个分配给一个变量。

显示在 (confirm-order.php) 纯文本上的数据,例如:

$itemName = $_POST['itemName'];
<?php echo $itemName; ?>

在页面末尾有一个表单只包含一个元素(提交按钮)

如何仅将 $itemName 数据插入 mysql 数据库(单击提交按钮并且表单操作将我带到确认页面后)?

我知道如何将数据插入 mysql,但它不适用于 isset() 函数

我必须先在表单中编写 isset 函数吗?在它下面是mysql数据库代码?

order.php 页面:

<form class="form-horizontal well" action="confirm-order.php" method="POST">
    <fieldset>
      <legend>Personal Shopper Order Form</legend>
      <div class="control-group">
        <label class="control-label" for="select01">Choose a plan</label>
        <div class="controls">
          <select id="select01" name="plan">
            <option>Lite Plan $0 per order</option>
          </select>
        </div>
      </div>

      <div class="control-group">
        <label class="control-label" for="itemName">Item Name</label>
        <div class="controls">
          <input type="text" class="input-xlarge" id="itemName" name="itemName">
          <p class="help-block">Item name exapmle: iPad3 White 32GB wifi & 3G.</p>
        </div>
      </div>
       <div class="control-group">
        <label class="control-label" for="itemID">Item ID</label>
        <div class="controls">
          <input type="text" class="input-xlarge" id="itemID" name="itemID">
          <p class="help-block">example: Ebay Item ID, Amazon Item ID.</p>
        </div><br>
        <div class="control-group">
        <label class="control-label" for="itemURL">Item URL</label>
        <div class="controls">
          <input type="text" class="input-xxlarge" id="itemURL" name="itemURL">
          <p class="help-block">Direct web link to the item.</p>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="textarea">Item Details</label>
        <div class="controls">
          <textarea class="input-xlarge" id="textarea" name="itemDetails" rows="6"></textarea>
          <p class="help-block">Item details (name, color, specifications etc...)</p>
        </div>
      </div>
      <li id="li_3" data-pricefield="money_simple" data-pricevalue="0">
       <div class="input-prepend input-append">
        <label class="control-label" for="element_3_1">Item Price</label>
        <div class="controls">
        <span class="add-on">$</span>
          <input id="element_3_1" data-price-value="10.00"  name="element_3" type="text" class="element text large">
          <p class="help-block">Item exact price on the US online store.</p>
        </div>
      </div>
     </li>
     <li id="li_7" data-pricefield="money_simple" data-pricevalue="0">
       <div class="input-prepend input-append">
        <label class="control-label" for="element_7_1">Local Shipping Cost</label>
        <div class="controls">
        <span class="add-on">$</span>
          <input id="element_7_1" data-price-value="10.00"  name="element_7" type="text" class="element text large">
        </div>
        <p class="help-block">Local shipping fee from the US Store to Sky2ship (if applicable).</p>
      </div>
     </li>
     <li id="li_8" data-pricefield="radio" data-pricevalue="0">
     <div class="control-group">
      <div class="controls">
      <p class="help-block">Order Processing Service Fee.</p>
        <label class="radio">($0) Standard 2-3 days
        <input id="element_8_1" data-pricedef="00.00" name="element_8" class="element radio" type="radio" value="$0 Standard 2-3 Day">
        </label>
        <label class="radio">($10) Express 1 day
        <input id="element_8_2" data-pricedef="10.00" name="element_8" class="element radio" type="radio" value="$10 Express Same Day">

        </label>
      </div>
      </div>
      </li>
      <legend>Personal Information & Shipping Address</legend>
     <div class="control-group">
        <label class="control-label" for="input04">Full Name</label>
        <div class="controls">
          <input type="text" class="input-medium" id="fullName" name="fullName">
          <p class="help-block">First & last name.</p>
        </div>
      </div>
      <div class="control-group">
      <div class="controls">
        <label class="radio">Male
        <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
        </label>
        <label class="radio">
        <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">Female

        </label>
      </div>
      </div>
      <div class="input-prepend">
        <label class="control-label" for="prependedInput">Email Address</label>
        <div class="controls">
        <span class="add-on">@</span>
          <input type="text" class="span2" id="prependedInput" name="Email">
          <p class="help-block">Your email address.</p>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="input06">Address</label>
        <div class="controls">
          <input type="text" class="input-xxlarge" id="input06" name="streetAddress" placeholder="Street Address">
          <p class="help-block">Your shipping address.</p>
        </div>
      </div>
       <div class="control-group">
        <div class="controls controls-row">
          <input type="text" class="span2" id="City" name="City" placeholder="City">
          <input type="text" class="span3" id="State" name="State" placeholder="State / Province">

        </div>
      </div>
      <div class="control-group">
        <div class="controls controls-row">
          <input type="text" class="span2" id="PostalCode" name="PostalCode" placeholder="Postal Code">
          <input type="text" class="span3" id="Phone" name="Phone" placeholder="Phone Number">
        </div>
      </div>

      <div class="control-group">
        <label class="control-label" for="select01">Country</label>
        <div class="controls">
          <select id="select02" name="Country">
            <option>IRAQ</option>
            <option>JORDON</option>
          </select>
        </div>
      </div>
       <li class="total_payment" align="right" data-basetotal="0">

            <span>

                <h3 class="alert-success">&#36;<var>0</var></h3>

                <h5>Total</h5>

            </span>

        </li>   
      <div class="control-group">
        <label class="control-label" for="optionsCheckbox">Read & Agree</label>
        <div class="controls">
          <label class="checkbox">
            <input type="checkbox" id="optionsCheckbox" value="option1">
            <a href="#">I agree to the site's Terms of Service & Privacy Policy.</a>
          </label>
        </div>
      </div>

      <div class="form-actions">
        <button type="submit" class="btn btn-primary">Confirm Order</button>
        <button type="reset" class="btn">Cancel Order</button>
      </div>
    </fieldset>
  </form>

确认-order.php 页面:

    <?php

$itemName = $_POST['itemName'];
$plan = $_POST['plan'];
$itemID = $_POST['itemID'];
$itemPrice = $_POST['element_3'];
$processService = $_POST['element_8'];
$itemDetails = $_POST['itemDetails'];
$streetAddress = $_POST['streetAddress'];
$City = $_POST['City'];
$State = $_POST['State'];
$PostalCode = $_POST['PostalCode'];
$Phone = $_POST['Phone'];
$Country = $_POST['Country'];
$fullName = $_POST['fullName'];
$Email = $_POST['Email'];
$itemURL = $_POST['itemURL'];
$itemLocalShipCost = $_POST['element_7'];

?>


 <?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "000000";
$db_name = "dbname";
    if (isset($_POST['submit'])) {
  $db_connect = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
  // Check connection
         if (mysqli_connect_errno())
  {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql ="INSERT INTO lite_order (lite_plan, lite_item_name)
        VALUES
  ('$plan','$item')";

          if (!mysqli_query($db_connect,$sql))
  {
          die('Error: ' . mysqli_error($db_connect));
  }
          echo "1 record added";
    }


?>
<address>
              <strong>Shipping Address.</strong><br>
              <?php echo $streetAddress; ?><br>
              <?php echo $City; ?>, <?php echo $State; ?>, <?php echo $PostalCode; ?><br><?php echo $Country; ?><br>
              <abbr title="Phone">P:</abbr><?php echo $Phone; ?>
            </address>
            <address>
              <strong><?php echo $fullName; ?></strong><br>
              <a href="mailto:#"><?php echo $Email; ?></a>
            </address>

            <table class="table">
              <thead>
                <tr>
                  <th>Plan</th>
                  <th>Item Name</th>
                  <th>Item ID</th>
                  <th>Local Shipping Cost</th>
                  <th>Item Price</th>
                  <th>Order Processing Fee</th>
                </tr>
              </thead>
              <tbody>
                <tr class="success">
                  <td><?php echo $plan; ?></td>
                  <td><?php echo $itemName; ?></td>
                  <td><?php echo $itemID; ?></td>
                  <td><?php echo "$" . $itemLocalShipCost; ?></td>
                  <td><?php echo "$" . $itemPrice; ?></td>
                  <td><?php echo $processService; ?></td
                ></tr>
              </tbody>
            </table>
            <strong>Item URL</strong><p class="alert alert-info"><?php echo $itemURL; ?></p>
            <pre class="pre-scrollable"><?php echo $itemDetails; ?></pre>
            <p>Your Total <h3 class="question"><?php echo "$" . $orderTotal; ?></h3></p>
                         <div class="form-actions"><form action="pending-order.php" method="post" name="confirmed-order">
                          <button type="submit" name="submit" class="btn btn-primary">Submit Order</button>
                          <button type="button" class="btn">Previous</button></form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

单击提交按钮后在哪里插入mysql数据库代码以将所有变量插入数据库?在哪里放置 isset() 函数?我试过了,它没有在我的表中插入任何数据。

4

7 回答 7

2

我可以贡献两件具体的事情。

首先,isset测试 null... 与空不同。如果您有一个提交为的表单字段,则将一个局部变量设置为该发布的值,然后使用isset;对其进行测试 isset将返回true,因为存在的值与根本没有在页面加载中注册的变量不同。

其次...任何东西都可以发布到您的表单中(想想邪恶的自主韩国黑客机器人)。此外,有很多方法可以在没有激活提交按钮本身的情况下提交表单,因此无法保证您甚至会在vars中看到提交键。$_POST您需要在处理脚本中定义的是“默认操作”。我的意思是这是一种非常基本且安全的行为(例如重定向到“出现问题”页面),默认情况下会启动,因此解决它的唯一方法是提交正确设置所有预期值的正确表单。

如果您这样做,您可以忽略提交按钮本身的值,而是专注于 POST 的内容。我收到了我希望收到的所有东西吗?都是正确的格式吗?用户的身份验证是否正确?只有在对所有这些问题进行了满意的测试后,您才会从默认行为切换到表单处理行为,在这种行为中,发布的数据可以插入到您的数据库中。

使用您的 3 页结构的示例:参考: filter vars

第 1 页:

<form action=./page2 method=POST>
<input type=text value=1234 name=numericValue />
<input type=text value="dummytext" name=stringValue />
<input type=submit value=submit name=submit />
</form>

第2页:

<?php
$args = array('numericValue' => FILTER_VALIDATE_INT
             ,'stringValue' => FILTER_SANITIZE_STRING);

$clean_data = filter_input_array(INPUT_POST,$args);

if (is_array($clean_data))
{
  $_SESSION["saved_clean_data"] = $clean_data;
}
else 
{
  Header(<something wrong page>);
  die();
}
?>
<form action=./page3 method=POST>
<input type=submit name=submit value=No />
<input type=submit name=submit value=Yes />
</form>

第 3 页:

<?php
if ($_POST["submit"] === "Yes")
{
   $cleanNum = $_SESSION["saved_clean_data"]["numericValue"];
   $cleanStr = $_SESSION["saved_clean_data"]["stringValue"];
   // DB insert Query, use advice from michi about PDO
   // parameterize your queries to help prevent sql injection
}
else
{
  Header(<somewhere for declined submits>);
  die();
}
?>
于 2013-04-19T12:41:32.073 回答
2

编辑:一个简单的例子

<form>,验证和插入一个文件,说form.php

<? // check if FORM has been posted

$posted = isset($_POST['submit']);

 if ($posted) { // form has been posted...

    // validate input

    if (!isset($_POST['item']) || strlen(trim($_POST['item'])) == 0)
        $error['item'] = "please insert an item-name!";

    if (!isset($_POST['price']) || !is_numeric($_POST['price']))
        $error['price'] = "please enter a valid price!";


    // ready for input?

    if (!isset($error)) { // no $error --> go insert!

        // I'll do the db-operation with PDO and a prepared statement.
        // this is cool, easy and safe. LEARN IT!

        $sql = "INSERT INTO table (item,price) VALUES (:item,:price)";

        $insert = $db->prepare($sql);
        $insert->execute(array(
            ':item' => $_POST['item'], 
            ':price' => $_POST['price']
            ));
    } // $error
 } // submit
?>

现在,在<body>同一页面中...

<? // check whether to display confirmation or form...

if ($posted && !isset($error)) { 

    // form was sent AND no error --> confirm
?>
<h1>Confirmed!</h1>
<p>Your data has been sent, thank you very much!</p>
<a href="somepage.php">go to somepage</a>
<?

} else {

    // form not sent or errors --> display form
?>

<h1>Please enter data</h1>

<? // display error-message, if there's one:
if (isset($error)) {
    $output = "";
    foreach ($error as $field => $msg) 
        $output .= (strlen($output) > 0?', ':'') . "[$field]: $msg";
    echo "<p>There were errors: $output</p>";
} // $error
?>

<form method="post">
    <!-- if the form has been sent, bring back the field's value from $_POST -->
    <p>item-name: <input type="text" name="item" 
        value="<?=($posted?$_POST['item']:'')?>" /></p>
    <p>price: <input type="text" name="price" 
        value="<?=($posted?$_POST['price']:'')?>" /></p>
    <p><input type="submit" name="submit" value="submit" /></p>
</form>

<?
} // submit & $error
?>

请参阅使用 aternary-operator设置 -elements 的value-attribute <input>

(<condition>?<what to do if true>:<what to do if false>)
于 2013-04-19T12:28:06.867 回答
0

创建一个表单并将变量存储在隐藏字段中,然后在表单中创建此提交按钮

因此,单击此表单将存储信息。请参阅此处的示例

<form class="form-horizontal well" action="confirm-order.php" method="POST">
   <input type="hidden" value="<?php echo $itemName; ?>" />
   <input type="submit" value="Confirm Order" />
</form>
于 2013-04-19T13:05:38.127 回答
0

那么我们可以通过以下方式做到这一点

  1. 您将所有数据存储在会话中,并在确认页面中使用,然后在数据插入页面中使用。如果用户更新或取消订单,请记住更新或删除它。

  2. 您可以使用 javascript 和 HTML 动态创建确认订单页面,当用户单击确认按钮时,只有我们将其发布到 PHP 页面。这也将减少服务器调用。

  3. 另一种方法是再次发送收集的发布值并将其作为隐藏字段保留在确认页面中,并在单击确认时发布。
于 2013-04-19T12:43:27.697 回答
0

尝试

<button type="submit" class="btn btn-primary" NAME="submit">Confirm Order</button>

并使用

IF (isset($_POST['submit]) {
$itemName = $_POST['itemName'];
$plan = $_POST['plan'];
$itemID = $_POST['itemID'];
$itemPrice = $_POST['element_3'];
$processService = $_POST['element_8'];
$itemDetails = $_POST['itemDetails'];
$streetAddress = $_POST['streetAddress'];
$City = $_POST['City'];
$State = $_POST['State'];
$PostalCode = $_POST['PostalCode'];
$Phone = $_POST['Phone'];
$Country = $_POST['Country'];
$fullName = $_POST['fullName'];
$Email = $_POST['Email'];
$itemURL = $_POST['itemURL'];
$itemLocalShipCost = $_POST['element_7'];

// 你的 mysql 在此处插入代码

}

编辑 1:更改<button type="submit" class="btn btn-primary">Confirm Order</button><input type="submit" class="btn btn-primary" value="Confirm Order">

于 2013-04-19T12:43:58.227 回答
0

那么有几种方法可以做到这一点:

  1. 存储上一页的所有数据,即变量order.php中的数据: 说明在会话中设置它将使您能够从站点的任何位置访问相同的变量,直到用户的会话。意味着在会话中存储它之后,您可以在页面中访问它。怎么做:在这个页面的顶部,而不是在顶部设置变量,写以下内容:$SESSION[]
    pending-order.php

    $SESSION['itemName'] = $_POST['itemName']

    然后使用:

    echo $SESSION['itemName']

    然后在pending-order.php你可以像这样为变量赋值:

    $itemName = $SESSION['itemName']

    现在您可以将变量存储在数据库中。

  2. 将隐藏字段放入confirm-order.php页面表单中: 说明:
    表单中创建隐藏输入字段confirm-order.php并设置变量中的值。这样,当您单击提交按钮时,您可以pending-order.php像在 上一样访问它们confirm-order.php
    怎么做:只需将变量放在隐藏输入的 value 属性中,如下所示:

    <form action="pending-order.php" method="post" name="confirmed-order">
    <input type="hidden" value="<?php $itemID ?>" id="someID">
    </form>

于 2013-04-19T12:26:06.917 回答
0

isset() 函数在输入字段类型为 submit.like 时工作

<input type="submit" value="Confirm Order" />

所以更新代码表格

<div class="form-actions">
   <button type="submit" class="btn btn-primary">Confirm Order</button>
   <button type="reset" class="btn">Cancel Order</button>
</div>

 <div class="form-actions">
   <input type="submit" class="btn btn-primary" value="Confirm Order" />
   <input class="btn" type="reset" value="Cancel Order" />
 </div>
于 2018-01-01T19:06:35.880 回答