本文介绍了如何使用order_id作为发票号码并将任何变量转换为全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我多次尝试过,但已经被拖了很长时间,好心的提到哪种方法更好,S_SESSION或GET方法将某些变量用作全局变量。以及如何
使用 $ _ SESSION ,你必须在你的php代码启动前运行 session_start(); :
<?php
session_start();
if(isset($ _ POST ['order']))
{
$ user_id = $ _SESSION ['id'];
$ date = date('Y-m-d H:i:s');
$ username = $ _SESSION ['username'];
mysql_query(INSERT INTO tborder(user_id,`date`,username)VALUES('$ user_id','$ date','$ username'))或die(mysql_error());
$ i = mysql_insert_id();
if($ i> 0)
for($ l = 0; $ l< count($ _ POST ['product_id']); $ l ++)
{
$ product_id = $ _POST ['product_id'] [$ l];
$ quantity = $ _POST ['quantity'] [$ l];
$ price = $ _POST ['price'] [$ l];
$ discount = $ _POST ['discount'] [$ l];
$ amount = $ _POST ['amount'] [$ l];
$ username = $ _SESSION ['username'];
$ user_id = $ _SESSION ['id'];
if(!isset($ _ SESSION ['product_cart']))
$ _SESSION ['product_cart'] = [];
if(!isset($ _ SESSION ['product_cart'] ['products']))
$ _SESSION ['product_cart'] ['products'] = [];
//将产品数组保存到$ _SESSION中
$ _SESSION ['product_cart'] ['products'] [] = [
'id'=> $ PRODUCT_ID;
'quantity'=> $数量;
'price'=> $价格;
'discount'=> $折扣;
'amount'=> $量;
'username'=> $的用户名;
'user_id'=> $ USER_ID;
];
//我不明白
是什么查询mysql_query(INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id)VALUES(' $ i','$ product_id','$ quantity','$ price','$ discount','$ amount','$ username','$ user_id'))或die(mysql_error());
}
//在循环结束后重定向,
//除非它只处理第一个数组元素
header('location:order。 PHP');
}
//在你不确定你会留在这个页面之前,不要包含html模板
include('header.php');
I tried multiple times but have been stucked for long time, kindly mention that which method is better S_SESSION or GET Method to used some variable as global. And how to
$date = date('Y-m-d H:i:s');convert this into SESSION[] ,or as global to access on other pages. Other thing is that how to acces
$iwhich is used as order_id , i wanna to used this as my invoice number. How can it is possible The code is give below
<?php include ('header.php'); if(isset($_POST['order'])) { $user_id = $_SESSION['id']; $date = date('Y-m-d H:i:s'); $username =$_SESSION['username']; mysql_query("INSERT INTO tborder(user_id,`date`,username) VALUES('$user_id','$date','$username')") or die(mysql_error()); $i = mysql_insert_id(); if($i >0) for($l=0;$l<count($_POST['product_id']);$l++) { $product_id = $_POST['product_id'][$l]; $quantity = $_POST['quantity'][$l]; $price = $_POST['price'][$l]; $discount = $_POST['discount'][$l]; $amount = $_POST['amount'][$l]; $username = $_SESSION['username']; $user_id = $_SESSION['id']; mysql_query("INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id) VALUES('$i','$product_id','$quantity','$price','$discount','$amount','$username','$user_id')")or die(mysql_error()); header('location:order.php'); } } ?> <form action="" method="post"> <div class="row"> <div class="col-md-6"> <div class="panel panel-default"> <div id="Tabs" role="tabpanel"> <!--- Nav Tabs --> <ul class="nav nav-tabs" role="tablist"> <?php $q =mysql_query("SELECT * FROM tbcategory"); while($row =mysql_fetch_object($q)) { ?> <li class=""> <a href="#<?=$row->id?>" aria-controls="personal" role="tab" data-toggle="tab"><?=$row->category_name?></a> </li> <?php } ?> </ul> <!--Tab Panes--> <div class="tab-content" style="padding-top: 20px"> <?php $q=mysql_query("SELECT * FROM tbcategory"); while($row = mysql_fetch_object($q)) { ?> <div role="tabpanel" class="tab-pane" id="<?=$row->id?>"> <?php $p =mysql_query("SELECT * FROM tbproduct WHERE cat_id = '$row->id'"); while($r = mysql_fetch_object($p)) { ?> <div class="col-md-4"> <a class="datavalue" href="#" data-id="<?=$r->id?>" data-name="<?=$r->product_name?>" data-price="<?=$r->unit_price ?>"> <img width="100" height="100" src="product/<?=$r->image?>"> <h5><?=$r->product_name?></h5> </a> </div> <?php } ?> </div> <?php } ?> </div> </div> </div> </div> <div class="col-md-6"> <table class="table"> <center><h2>MY PRODUCT</h2></center> <tr> <?php date_default_timezone_set('Asia/Karachi'); $currenttime = date('h:i A'); ?> <td> <label>Inv Date : </label> <?= date('d-M-Y'); ?> </td> <td> <label>Inv Time : </label> <?=$currenttime ?> </td> </tr> <tr> <td> <label>Inovice No.</label> ???? </td> <td> <label>User Name</label> <?=$_SESSION['username'] ?> </td> </tr> </table> <table class="table table-bordered table-hover"> <thead> <tr> <th>ProductName</th> <th>Quantity</th> <th> Price </th> <th>Discount</th> <th>Amount</th> <th>X</th> </tr> </thead> <tbody class="details"> </tbody> <tfoot> <tr> <td> SubTotal : <input type="text" name="subtotal" class="form-control subtotal"> Cash Receive: <input type="text" name="pay" class="form-control pay"> Pay Back : <input type="text" name="payback" class="form-control payback"> </td> </tr> </tfoot> </table> <input type="submit" name="order" value="Order" class="btn btn-primary"> </div> </div> </form> <?php include ('footer.php') ;?>解决方案To use $_SESSION, you have to run session_start(); before your php code starts:
<?php session_start(); if(isset($_POST['order'])) { $user_id = $_SESSION['id']; $date = date('Y-m-d H:i:s'); $username = $_SESSION['username']; mysql_query("INSERT INTO tborder(user_id,`date`,username) VALUES('$user_id','$date','$username')") or die(mysql_error()); $i = mysql_insert_id(); if($i >0) for($l=0;$l<count($_POST['product_id']);$l++) { $product_id = $_POST['product_id'][$l]; $quantity = $_POST['quantity'][$l]; $price = $_POST['price'][$l]; $discount = $_POST['discount'][$l]; $amount = $_POST['amount'][$l]; $username = $_SESSION['username']; $user_id = $_SESSION['id']; if(!isset($_SESSION['product_cart'])) $_SESSION['product_cart'] = []; if(!isset($_SESSION['product_cart']['products'])) $_SESSION['product_cart']['products'] = []; // save product to cart products array in $_SESSION $_SESSION['product_cart']['products'][] = [ 'id' => $product_id; 'quantity' => $quantity; 'price' => $price; 'discount' => $discount; 'amount' => $amount; 'username' => $username; 'user_id' => $user_id; ]; // I don't get what is this query for mysql_query("INSERT INTO tborderdetail(order_id,product_id,quantity,price,discount,amount,username,user_id) VALUES('$i','$product_id','$quantity','$price','$discount','$amount','$username','$user_id')")or die(mysql_error()); } // redirect after loop ends, // unless it'll process only first array element header('location:order.php'); } // Do not include html templates before you not sure you'll stay on this page include('header.php');
这篇关于如何使用order_id作为发票号码并将任何变量转换为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!