inserting data into mysql database using php

后端 未结 7 603
生来不讨喜
生来不讨喜 2020-12-22 01:24

I have a php order form named (order.php) and when the user clicks the (submit button \"Next Step\") it takes him to another page called (confirm-order.php)

The (con

相关标签:
7条回答
  • 2020-12-22 01:34

    EDIT: a simple example

    do <form>, validation and inserting in one file, say 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
    ?>
    

    Now, in the <body> of the same page...

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

    See the use of a ternary-operator for setting the value-attribute of the <input>-elements:

    (<condition>?<what to do if true>:<what to do if false>)
    
    0 讨论(0)
  • 2020-12-22 01:40

    Well we can do this in the following ways

    1. You store all the data in session and use it in confirmation page and then on data insertion page. Do remember to update or delete it if user updates or cancel the order.

    2. You can dynamically create the confirm order page using javascript and HTML and when user clicks confirm button then only we post it to the PHP page. This will also reduce a server call.

    3. One other ways is to again send the collected posted values and keep it as hidden fields in the confirmation page and post it when clicked confirm.
    0 讨论(0)
  • 2020-12-22 01:40

    Well there are couple of ways about doing this:

    1. Store all the data from the previous page i.e. from order.php in the $SESSION[] variables:
      Explaination: Setting it in Session will enable you to access the same variable from anywhere in the site until the session of the user. Means that after you store it in session you can access it in pending-order.php page.
      How to do it: In this page at the top, instead of setting the variables at top write the following:

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

      then echo it using:

      echo $SESSION['itemName']

      and then in the pending-order.php you can assign a value to a variable like so:

      $itemName = $SESSION['itemName']

      and now you can store the variable in the db.

    2. Put hidden fields inside the form of confirm-order.php page:
      Explaination: Create hidden input fields in confirm-order.php form and set the values that are in the variables. This way when you click the submit button you can access them in pending-order.php in the same way you are doing on confirm-order.php.
      How to do it: Simply put the variables in value attribute of the hidden input like so:

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

    0 讨论(0)
  • 2020-12-22 01:40

    isset() function work when the input field type is submit.like

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

    so update the code form

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

    to

     <div class="form-actions">
       <input type="submit" class="btn btn-primary" value="Confirm Order" />
       <input class="btn" type="reset" value="Cancel Order" />
     </div>
    
    0 讨论(0)
  • 2020-12-22 01:47

    Try

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

    And use

    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'];
    

    // your mysql INSERT codes here

    }
    

    EDIT 1: change <button type="submit" class="btn btn-primary">Confirm Order</button> TO <input type="submit" class="btn btn-primary" value="Confirm Order">

    0 讨论(0)
  • 2020-12-22 01:52

    create a form and store variables in hidden fields , then create this submit button in the form

    So clicking this form will store the info. See the exmple here

    <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>
    
    0 讨论(0)
提交回复
热议问题