簡體   English   中英

表單提交后將日期存儲到MySQL數據庫中

[英]Store date into MySQL database after form submit

這是我的代碼,以前很少,但是這里是主要代碼。

當我單擊“ 完成訂單”時,我的數據庫將amazon_code記錄在db中,但是我也試圖記錄gift_pingift_card_name這兩個數據。 當按下Complete Order時,我需要將這兩個數據直接添加到db中。 請幫忙

 <?php if (isset($this->data['amazon'])) { ?> <input type="hidden" id="payment_method" value="amazon" /> <div class="row"> <div class="col-md-12"> <div class="form-group" id="group-amazon_code"> <label class="col-xs-12 form-control-label">Gift Card Number: <span class="muted small">(this is the code under the scratch off area)</span> </label> <div class="col-md-12"> <input type="text" autocomplete="yes" value="" class="form-control" name="amazon_code" id="amazon_code" placeholder="Amazon Gift card # (under scratch off)" /> <label for="amazon_code" class="error">Error Text</label> </div> </div> <div class="form-group" id="group-amazon_code"> <label class="col-xs-12 form-control-label">PIN</label> <div class="col-md-12"> <input type="text" autocomplete="yes" value="" class="form-control" name="gift_pin" id="gift_pin" placeholder="Enter PIN #" /> <label for="gift_pin" class="error">Error Text</label> </div> </div> <div class="form-group"> <label for="human" class="col-sm-4 control-label">Gift Card Name:</label> <div class="col-sm-8"> <select class="form-control" name="gift_card_name"> <option <?php if($this->data['gift_card_name'] == 'PayPal') echo 'selected="selected"'; ?> value="PayPal">PayPal</option> <option <?php if($this->data['gift_card_name'] == 'eBay') echo 'selected="selected"'; ?> value="eBay">eBay</option> <option <?php if($this->data['gift_card_name'] == 'Google') echo 'selected="selected"'; ?> value="Google">Google</option> <option <?php if($this->data['gift_card_name'] == 'iTunes') echo 'selected="selected"'; ?> value="iTunes">iTunes</option> <option <?php if($this->data['gift_card_name'] == 'BestBuy') echo 'selected="selected"'; ?> value="BestBuy">BestBuy</option> <option <?php if($this->data['gift_card_name'] == 'Hotels.com') echo 'selected="selected"'; ?> value="Hotels.com">Hotels.com</option> <option <?php if($this->data['gift_card_name'] == 'Bass Pro') echo 'selected="selected"'; ?> value="Bass Pro">Bass Pro</option> </select> </div> </div> </div> </div> <?php } // End of Amazon?> <div class="row"> <div class="col-xs-12"> <div class="alert alert-info"> <div class="row"> <div class="col-xs-6"> <div class="panel"> <div class="panel-body"> <h4 class="text-center">Credits</h4> <h2 class="credits_text credits text-center"></h2> </div> </div> </div> <div class="col-xs-6"> <div class="panel"> <div class="panel-body"> <h4 class="text-center">Order Total</h4> <h2 class="price_text text-success text-center"></h2> </div> </div> </div> </div> </div> </div> </div> </div> <!-- STEP 3 --> </div> <div class="clearfix"></div> </div> <div class="clearfix"></div> </div> </div> <div class="clearfix block"></div> <div class="row"> <div class="col-sm-4 col-xs-6"> <a href="#" class="btn btn-block btn-default btn-lg prev-btn">Previous Step</a> </div> <div class="col-sm-8 col-xs-6"> <a href="#" class=" btn btn-block btn-lg btn-success next-btn">Next Step</a> <a href="#hasErrors" class=" btn btn-block btn-lg btn-primary finish-btn" data-submit="<?php echo (isset($this->data['amazon'])) ? 'amazonprocess' : 'process'; ?>"> <span class="fa fa-shopping-cart" aria-hidden="true"></span> Complete Order </a> </div> </div> </form> </div> 

if(isset($_POST['gift_pin']) && isset($_POST['gift_card_name'])){

$giftPin = $_POST['gift_pin']; //Sanitize it too.
$giftCardName = $_POST['gift_card_name']; //Sanitize it too.



/*
    *  INCASE YOU MISSED IT: SANITIZE YOUR INPUTS. OR ELSE PEOPLE CAN USE
    *  SQL INJECTION TO STEAL ALL OF YOUR DATA, AND MAKE YOU LOOK LIKE A 
    *  FOOL. PREPARED STATEMENTS SHOULD BE USED WHEREVER POSSIBLE, Thanks
    */


//Add in all your other stuff you want in the database too.
$query = "INSERT INTO ultraCoolTable (gift_pin, gift_card_name) VALUES (" . $giftPin . ", '" . $giftCardName . "');";

$result = mysqli_query($connection, $query);

//Normal Stuff you'd do here, redirect maybe, check if it was added maybe. Whatever you want.

}else{
  //Something wasn't filled in, you should ideally not allow them to click  the button by adding the required attribute to all the fields necessary but hey-ho... better redirect them or something (:
}

同樣,您的標題寫錯了,給人一種錯覺,那就是您希望將其添加到數據庫的日期。 對於任何想添加日期的人,請在您的php腳本中從microseconds(true);向您的數據庫添加一個BIGINT microseconds(true); 方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM