Sunday 15 February 2015

php - Save Inputs into database & class errors - building coupon system -



php - Save Inputs into database & class errors - building coupon system -

okay, i'm building (trying) coupon code scheme admin panel, want accomplish allow admin manually create coupons setting coupon_code , coupon_discount (1% 100%). when submit, stored in 2 different tables of 2 dbs.

table coupons:

coupon_id int(10) not null auto increment - primary key coupon_code varchar(15) not null - unique discount decimal(3,2) expire datetime -null count int(10) -null

i checked lots of examples on net , poorly written class: class productdiscount { static public function validate($coupon_code); private $_coupon_code; // string private $_coupon_discount; // integer?? private $_expire; // null cause unlimited private $_count; // null private function __construct(); //for line got error public function getcouponcode(); // homecoming string public function getcoupondiscount(); // homecoming public function getcount(); // returns null unlimited public function getexpiredate();// null public function usediscount(); // apply discount public function usealldiscount(); // invalidate discount future utilize

coupons.php - new coupon creation

on admin side, totally lost how pass coupon_code , coupon_discount database...how utilize functions wrote in class...this did:

<?php if($_server['request_method'] == 'post') { $coupon_code = $_post['coupon_code']; $coupon_discount = $_post['coupon_discount']; //insert db admin $connect = new mysqli("localhost","-----","-------","--------"); $stmt = $connect->prepare("insert `coupons` (`coupon_code`, `coupon_discount`) values (?,?)"); $stmt->bind_param('si', $coupon_code, $coupon_discount); $stmt->execute(); $connect->close(); } ?>

i'm getting undefined index errors both coupon_code , coupon_discount..

if require_once class file here, i'm getting non-abstract method productdiscount::create() must contain body error.

<form class="form-horizontal" role="form" action="createcoupon.php"> <div class="form-group"> <label class="col-sm-2 control-label">coupon code</label> <div class="col-sm-2"> <input type="text" class="form-control" name="coupon_code" id="couponcode" placeholder="e.g save10"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">coupon discount</label> <div class="col-sm-2"> <input type="number" class="form-control" name="coupon_discount" id="coupondiscount" placeholder="e.g 10 10%"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-3"> <input type="submit" style="background-color:#7575dd; padding:0 !important; color:white;" name="create_coupon" value="create coupon" class="btn btn-default"/> </div> </div> </form>

here createcoupon.php:

<?php //connect 1st db $coupon_code = $_post['coupon-code']; $coupon_discount = $_post['coupon-discount']; //the prepared stmt insert db1 //connect 2nd tb $coupon_code = $_post['coupon-code']; $coupon_discount = $_post['coupon-discount']; //prepared stmt insert db2 header("location: http://example.com/admin/coupons"); ?>

it's 4am , been hours i'm trying larn these.

so my question is: "how can save necessary form info table in right way , save coupon table?"

(edited , specified current problem only, can reopen?)

(commented long/unnecessary parts clarify)

thank much time!

okay far, managed solve it. using bootstrap table.

with "add coupon" button:

<button data-toggle='modal' data-target='#mymodal' type='button' class='btn btn-primary' style="margin: 20px;" name='add-btn' id='add-btn'>add coupon</button>

data-toggle='modal' , data-target='#mymodal' link modal div button

select statement coupon table , pull info bootstrap table.

<div style="padding: 20px;"> <?php $con = new mysqli("------","--------","--------","-----"); $query = "select * coupons expire > now() or expire null or expire = '0000-00-00 00:00:00'"; if ($result = $con->query($query)) { echo "<table border='1' class='table table-hover' data-toggle='table'> <tr> <th>coupon code</th> <th>amount</th> <th>expiry</th> <th></th> </tr>"; while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>" . $row['coupon_code'] . "</td>"; echo "<td>" . $row['coupon_discount'] . "</td>"; echo "<td>" . $row['expire'] . "</td>"; echo "<td><button data-toggle='modal' data-target='#mymodal' type='button' class='btn btn-primary' name='submit-btn' data-id='" . $row["coupon_id"] ."'>edit</button></td>"; echo "</tr>"; } echo "</table>"; } mysqli_close($con); ?>

these modal divs , form post inputs , save table.

<!-- modal --> <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">close</span></button> <h4 class="modal-title" id="mymodallabel">coupon information</h4> </div> <div class="modal-footer"> <form action='createcoupon.php' method='post'> <input type='hidden' name='coupon_id' id='couponid' value='0'/> <input type="text" class="form-control" style="margin-bottom:10px;" name="coupon_code" id="couponcode" placeholder="e.g save10"> <input type="number" class="form-control" style="margin-bottom:10px;" name="coupon_discount" id="coupondiscount" placeholder="e.g 10 10%"> <input type="text" class="form-control" style="margin-bottom:10px;" name="coupon_expire" id="couponexpire" placeholder="e.g 01/01/2015"> <input class="btn btn-primary" type='submit' name='submit-btn' value='save' /> </form> </div> </div> </div> </div> </div>

this javascript

<script> // function added each edit button. sets id button "data-id" value. // finds parent (td) of button, goes each sibling right class name // , changes modal input value inner text of td var editfunction = function() { $('#couponid').val($(this).attr('data-id')); var parent = $(this).parent(); $('#couponcode').val($(parent).siblings('.coupon_code').text()); $('#coupondiscount').val($(parent).siblings('.coupon_discount').text()); $('#couponexpire').val($(parent).siblings('.coupon_expire').text()); }; // called on add together button // sets id 0 (to insert record on form submit) // sets other inputs blank values function addnewcoupon() { $('#couponid').val('0'); $('#couponcode').val(''); $('#coupondiscount').val(''); $('#couponexpire').val(''); } // when page has loaded... // attach edit function phone call click event of each edit button // attach add together new coupon click event of add together button $(document).ready(function() { $('button[name="submit-btn"]').click(editfunction); $('#add-btn').click(function() { addnewcoupon(); }); }); </script>

then on createcoupon.php did this:

// check connection $coupon_id = intval($_post['coupon_id']); $coupon_code = $_post['coupon_code']; $coupon_discount = $_post['coupon_discount'];

and after mysqli connection inserted/updated (in if else statement, "if 'add coupon' or 'edit coupon') them.

i solved in end, if have ideas it, please share me can improve.

i hope these codes can useful someone!

thanks time.

php mysql

No comments:

Post a Comment